Android选择ABS项目时崩溃

时间:2013-07-30 19:23:18

标签: android actionbarsherlock

无论出于何种原因,当我点击其中一个按钮时,我的ActionBar会崩溃。这是optionsSelectMenu的代码:

public boolean onOptionsItemSelected(MenuItem item)
{   
    switch (item.getItemId())
    {
    case R.id.refresh_button_actionbar:
        if (this.frag instanceof DashboardFragment)
            dashboardFrag.refresh();
        break;
    default: 
        return false;
    }
    return true;
}

这是logcat:

07-30 19:17:52.981: E/AndroidRuntime(1358): FATAL EXCEPTION: main
07-30 19:17:52.981: E/AndroidRuntime(1358): java.lang.NullPointerException
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.view.menu.MenuItemImpl.toString(MenuItemImpl.java:496)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at java.lang.StringBuilder.append(StringBuilder.java:202)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:201)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at android.view.View.performClick(View.java:4204)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at android.view.View$PerformClick.run(View.java:17355)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at android.os.Handler.handleCallback(Handler.java:725)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at android.os.Looper.loop(Looper.java:137)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at java.lang.reflect.Method.invokeNative(Native Method)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at java.lang.reflect.Method.invoke(Method.java:511)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-30 19:17:52.981: E/AndroidRuntime(1358):     at dalvik.system.NativeStart.main(Native Method)

我做错了什么想法?我认为这与片段比较有关。另外,需要注意的是,当我将代码设置为“return true”时(因为它只会返回true),它将重新启动活动。这是预期的行为吗?如果是这样,我怎么能绕过它?

以下是我的导入:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.actifio.android.R;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

1 个答案:

答案 0 :(得分:1)

您正在使用Android的Menu和MenuInflater,但应该使用ActionBarSherlock附带的类。检查这是否是他们的导入:

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;

检查此thread中给出的答案以获得详细说明。您的问题应该得到解决。下次确定是否已经问过这类问题。

修改

我想出了一个调整(不是很干净但很有效)来解决你的问题。希望您的工作区中有actionbarsharlock项目。转到包com.actionbarsharlock.app,然后找到文件SharlockFragmentActivity.java,将其打开并转到第201行(CMD / CTRL + L,然后输入行号),现在注释掉以下行:

try
        {
            if (BuildConfig.DEBUG) Log.d(TAG, "[onMenuItemSelected] featureId: " + featureId + ", item: " + item);
        }catch(Exception e)
        {
            Log.d(TAG,"Exception");
        }

你的问题应该解决。