将侦听器添加到ActionBar项,onOptionsItemSelected NullPointerException

时间:2013-07-09 16:57:11

标签: java android nullpointerexception actionbarsherlock android-actionbar

* 已回答!:* 事实证明,即使您有一个图标,也需要拥有该项目的标题。

我目前正在我的应用程序中实施(Sherlock)Action Bar。我目前有一个屏幕按钮刷新我的屏幕视图,我想将此按钮移动到我的操作栏中的项目。 onOptionsItemSelected的日志未显示在LogCat中,但显示了创建的菜单。

我不知道为什么我得到空指针,并且无法在错误日志中找到null发生的位置。当我点击操作栏中的刷新按钮时,该应用程序才会崩溃,并且在此之前完成所有其他操作。

public boolean onCreateOptionsMenu(Menu menu) {
        Log.i("Created Menu", "Yes");
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.main, menu);


        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        Log.i("Got into onOptionsItemSelected", "Yes");
        switch(item.getItemId()){

        case R.id.action_refresh:

                new getFleetList().execute(getFleetListURL);
                progressBar.setVisibility(ProgressBar.VISIBLE);

        return true;

    }

StackTrace:

07-09 12:52:27.594: W/dalvikvm(23570): threadid=1: thread exiting with uncaught exception (group=0x4132e930)
07-09 12:52:27.601: E/AndroidRuntime(23570): FATAL EXCEPTION: main
07-09 12:52:27.601: E/AndroidRuntime(23570): java.lang.NullPointerException
07-09 12:52:27.601: E/AndroidRuntime(23570):    at com.android.internal.view.menu.MenuItemImpl.toString(MenuItemImpl.java:496)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at java.lang.StringBuilder.append(StringBuilder.java:202)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:201)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at android.view.View.performClick(View.java:4204)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at android.view.View$PerformClick.run(View.java:17355)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at android.os.Handler.handleCallback(Handler.java:725)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at android.os.Looper.loop(Looper.java:137)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at android.app.ActivityThread.main(ActivityThread.java:5041)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at java.lang.reflect.Method.invokeNative(Native Method)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at java.lang.reflect.Method.invoke(Method.java:511)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-09 12:52:27.601: E/AndroidRuntime(23570):    at dalvik.system.NativeStart.main(Native Method)

编辑,XML文件:

菜单XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >




     <item
        android:id="@+id/action_refresh"
        android:icon="@drawable/ic_action_refresh"
        android:orderInCategory="1"
        android:showAsAction="always"

        />


</menu>

1 个答案:

答案 0 :(得分:1)

您使用的是这些导入

吗?
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

否则你会收到错误。

试试这个

@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {

    com.actionbarsherlock.view.MenuInflater inflate = getSupportMenuInflater();
    inflate.inflate(R.menu.main, menu);

    return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.action_refresh:
            //do whatever you want
        break;
        default:
            return super.onOptionsItemSelected(item);
    }
}

**** **** UPDATE

<item
        android:id="@+id/action_legalnotice"
        android:orderInCategory="100"
        android:title="Legal Notices"/>
     <item
        android:id="@+id/action_refresh"
        android:icon="@drawable/ic_action_refresh"
        android:orderInCategory="1"
        android:showAsAction="always"


android:title="Your title here please"

        />

请参阅此链接了解详情:Null pointer exception showing in action bar sherlock library