从ActionBar更改为Support.v7.ActionBar

时间:2014-01-16 01:15:35

标签: android android-actionbar

我已经将一些代码从ActionBar更改为Support.v7.ActionBar - 在编译工作的正常复杂问题之后我在以下代码中遇到错误,以前的代码正在运行:

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        inflater.inflate(R.menu.main_activity_actions, menu);
        RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();

        /**** App crashes here *****/
        TextView tv_mode = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview_mode);


        tv_mode.setText(modeOutput);
        TextView tv_type = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview_type);

        tv_type.setText(typeOutput);
        return super.onCreateOptionsMenu(menu);
    }

BadgeLayout只是我正在使用的抽屉的相对布局

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="500dp"
     android:layout_height="fill_parent"
     android:layout_gravity="right"
     android:paddingLeft="16dp"
     android:paddingRight="16dp"
     android:paddingTop="16dp" >

<!-- Menu Item Image -->

<!-- Badge Count -->    
<TextView
    android:id="@+id/actionbar_notifcation_textview_mode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:textSize="16sp"
    android:background="#F60"
    android:paddingRight="16dp"
    android:paddingLeft="16dp"
    android:layout_marginRight="15sp"
   />
<TextView
    android:id="@+id/actionbar_notifcation_textview_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/actionbar_notifcation_textview_mode"
    android:textSize="16sp"
    android:background="#09F"
    android:paddingRight="16dp"
    android:paddingLeft="16dp"
    android:layout_marginRight="15sp"
   />
</RelativeLayout>

onCreateOptionsMenu中突出显示的行是345

01-15 21:36:33.889: E/AndroidRuntime(5265): java.lang.NullPointerException
01-15 21:36:33.889: E/AndroidRuntime(5265):     at com.MainActivity.onCreateOptionsMenu(MainActivity.java:345)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.app.Activity.onCreatePanelMenu(Activity.java:2504)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:413)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:775)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:198)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.view.Choreographer.doFrame(Choreographer.java:531)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.os.Handler.handleCallback(Handler.java:730)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.os.Looper.loop(Looper.java:137)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at java.lang.reflect.Method.invoke(Method.java:525)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-15 21:36:33.889: E/AndroidRuntime(5265):     at dalvik.system.NativeStart.main(Native Method)

我无法理解我的抽屉处理是什么,添加Support.v7.actionbar可能会导致此错误...任何想法?

我尝试在违规行周围设置一个try catch,似乎对抽屉TextView findViewById(R.id.actionbar_notifcation_textview_type);的任何引用都会生成此空指针异常

我将support.v7作为库添加到我现有的项目中,想知道这是否相关?

更新:

我删除了对support.v7 ActionBar的所有引用,并更改回正常的ActionBar,一切正常。如此变化导致了这个奇怪的问题......

1 个答案:

答案 0 :(得分:0)

您是否检查badgeLayout是否为空? getActionView()可能会返回null。

看看 MenuItemCompat.getActionView always returns null