我在当天回想起安卓平板电脑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“标准”膨胀对象或布局,但我无法确定问题出在哪里。
这是logcat:
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>