1

I run the following code:

   public class MainActivity extends ActionBarActivity  
    {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
public static final String PREFS_NAME = "MyPrefsFile";
static SharedPreferences mPrefs;
SharedPreferences.Editor shPrefEditor;
public int BlackBackground=0, SleepScreen=1;
static MenuInflater inflater;
public ActionBar ab;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);  

    mPrefs = getSharedPreferences(PREFS_NAME, 0);
    shPrefEditor = mPrefs.edit();
    BlackBackground = mPrefs.getInt("BlackBackground", 0);
    inflater = getMenuInflater();
...

The simulator is crash (NullPointerException) on:

inflater = getMenuInflater();

Anyone know why? And how can I fix it?

RafaelJan
  • 3,118
  • 1
  • 28
  • 46

1 Answers1

0

To solve this problem, getMenuInflater() need to move from onCreate to onCreateOptionsMenu(). It seems that there are some devices (Nexus 5 for example) that cause crash if getMenuInflater() is called from onCreate(), and in addition onCreateOptionsMenu() is really the right location.

RafaelJan
  • 3,118
  • 1
  • 28
  • 46