android - getMenuInflater导致崩溃

时间:2014-02-02 18:20:46

标签: android crash android-inflate inflate-exception

我运行以下代码:

   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();
...

模拟器崩溃(NullPointerException)on:

inflater = getMenuInflater();

任何人都知道为什么?我该如何解决?

1 个答案:

答案 0 :(得分:0)

要解决此问题,getMenuInflater()需要从onCreate移动到onCreateOptionsMenu()。 似乎有一些设备(例如Nexus 5)如果从onCreate()调用getMenuInflater()导致崩溃,另外onCreateOptionsMenu()实际上是正确的位置。