在过去的几个小时里,我一直在弄清楚为什么我的应用程序在加载时没有错误消息而崩溃。
基本上,当我通过 USB 或模拟器运行它时,屏幕每半秒左右刷新一次。看日志,好像是在循环运行onCreate。我不知道为什么!最终,会出现一堆“频道已无法恢复,将被处置”的消息。这条消息也经常出现:
07-15 13:59:22.334:错误/AbstractCompatWrapper(381):AbstructCompatWrapper 的输入无效 07-15 13:59:22.334:错误/CompatUtils(381):调用中的异常:NullPointerException
如果您愿意,我可以发布完整的日志,但我在那里找不到任何有用的东西。
最后,我发现删除对“fillActionBar”(一种实例化操作栏的方法)的调用可以“解决”问题。但是,我希望能够使用操作栏!我做错了什么?
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultBox = (TextView) findViewById(R.id.readout_values);
// commenting out the following line fixes the problem,
// but removes the action bar.
this.actionBar = fillActionBar();
try {
specialHttpClient = new SpecialHttpClient(
"username", "password");
} catch (Exception e) {
Dbg.loge(this.getClass().getName(), "Could not instantiate client", e);
}
}
private ActionBar fillActionBar() {
ActionBar bar = getActionBar();// getSupportActionBar();
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_dropdown_item,
//R.layout.sherlock_spinner_item,
pages
);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ActionBar.OnNavigationListener navListener = new ActionBar.OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Intent i = null;
switch(itemPosition) {
case 0:
i = new Intent(context, RecorderActivity.class);
break;
case 1:
i = new Intent(context, TrackerActivity.class);
break;
}
startActivity(i);
return true;
}
};
bar.setListNavigationCallbacks(spinnerAdapter, navListener);
return bar;
}
**编辑**
问题似乎是即使没有按下按钮也会调用 startActivity(i) 。将该调用移至第二个菜单项似乎只能解决问题 - 但为什么会发生这种情况?