那天我为 android tablet v3 制作了一个小应用程序。它工作得很好,直到我决定在 android v4 上测试它。问题来自放置在片段布局顶部操作栏的搜索框。在 android 3.x 上,它加载得很好并且像一个魅力一样工作,但在 android 4.x 上并非如此,每次我加载应用程序崩溃的片段时。
I/System.out(8863): v3: android.widget.EditText@40a34fa8
I/System.out(28353): v4: null
我知道我可能不会在某个地方为 android 4.x“标准”膨胀对象或布局,但我就是不知道问题出在哪里。
这是日志猫:
01-24 12:31:41.527: I/System.out(28353): v4: null
01-24 12:31:41.527: D/AndroidRuntime(28353): Shutting down VM
01-24 12:31:41.527: W/dalvikvm(28353): threadid=1: thread exiting with uncaught exception (group=0x40bc41f8)
01-24 12:31:41.535: E/AndroidRuntime(28353): FATAL EXCEPTION: main
01-24 12:31:41.535: E/AndroidRuntime(28353): java.lang.NullPointerException
01-24 12:31:41.535: E/AndroidRuntime(28353): at prob_test665.prob_test.FragmentListMenuSEARCH.ifcSEARCH(FragmentListMenuSEARCH.java:154)
01-24 12:31:41.535: E/AndroidRuntime(28353): at prob_test665.prob_test.FragmentListMenuSEARCH.onPrepareOptionsMenu(FragmentListMenuSEARCH.java:115)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.app.FragmentManagerImpl.dispatchPrepareOptionsMenu(FragmentManager.java:1865)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.app.Activity.onPreparePanel(Activity.java:2463)
01-24 12:31:41.535: E/AndroidRuntime(28353): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:438)
01-24 12:31:41.535: E/AndroidRuntime(28353): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:761)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.app.Activity.invalidateOptionsMenu(Activity.java:2552)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.app.FragmentManagerImpl.invalidateOptionsMenu(FragmentManager.java:1914)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.app.Fragment.setHasOptionsMenu(Fragment.java:898)
01-24 12:31:41.535: E/AndroidRuntime(28353): at prob_test665.prob_test.FragmentListMenuSEARCH.onCreateView(FragmentListMenuSEARCH.java:57)
01-24 12:31:41.535: E/AndroidRuntime(28353): at prob_test665.prob_test.ccc_frament.onCreateView(ccc_frament.java:68)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.app.BackStackRecord.run(BackStackRecord.java:622)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.os.Handler.handleCallback(Handler.java:605)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.os.Looper.loop(Looper.java:137)
01-24 12:31:41.535: E/AndroidRuntime(28353): at android.app.ActivityThread.main(ActivityThread.java:4514)
01-24 12:31:41.535: E/AndroidRuntime(28353): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 12:31:41.535: E/AndroidRuntime(28353): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 12:31:41.535: E/AndroidRuntime(28353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-24 12:31:41.535: E/AndroidRuntime(28353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-24 12:31:41.535: E/AndroidRuntime(28353): at dalvik.system.NativeStart.main(Native Method)
这是代码片段
在 prob_test665.prob_test.FragmentListMenuSEARCH.ifcSEARCH(FragmentListMenuSEARCH.java:154)
EditText text_search = (EditText)getActivity().findViewById(R.id.text_search);
那是我加载片段时应用程序崩溃的那一行。试图用上下文替换getActivity,对话框..但没有运气。
有两段代码来自我做的一个测试,只是为了让它更清楚一点:
在此先感谢您的时间。
编辑:忘记了酒吧的 xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item_search" android:actionLayout="@layout/search_box" android:showAsAction="always" android:visible="false"></item>
<item android:title="test55" android:showAsAction="always">
<menu>
<item
android:id="@+id/subitem_m_sinc"
android:title="@string/item_m_sinc2"
android:titleCondensed="@string/item_m_sinc1"
android:icon="@drawable/ic_launcher"/>
<item
android:id="@+id/subitem_m_ord"
android:title="@string/item_me_ord"/>
<item
android:id="@+id/subitem_m_ccc"
android:title="@string/item_m_ccc"/>
<item
android:id="@+id/subitem_m_config"
android:title="@string/item_m_config"/>
</menu>
</item>