1

我使用了 Activity 并包含了许多片段。有时应用程序会在下面的 log-cat 中强制停止。谁能告诉我如何解决这个问题。非常感谢!

使用安卓:3.1。测试 Galaxy Tab 7 (GT-P3100)

有了这个 log-cat,我在我的项目中找不到我的代码。

java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage/myActivity}: java.lang.NullPointerException
--------- Stack trace ---------
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
    android.app.ActivityThread.access$600(ActivityThread.java:140)
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
    android.os.Handler.dispatchMessage(Handler.java:99)
    android.os.Looper.loop(Looper.java:137)
    android.app.ActivityThread.main(ActivityThread.java:4895)
    java.lang.reflect.Method.invokeNative(Native Method)
    java.lang.reflect.Method.invoke(Method.java:511)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
    dalvik.system.NativeStart.main(Native Method)
--------- Cause ---------

java.lang.NullPointerException
    android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:676)
    android.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1492)
    android.app.FragmentManagerImpl$3.run(FragmentManager.java:460)
    android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
    android.app.Activity.performStart(Activity.java:5172)
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
    android.app.ActivityThread.access$600(ActivityThread.java:140)
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
    android.os.Handler.dispatchMessage(Handler.java:99)
    android.os.Looper.loop(Looper.java:137)
    android.app.ActivityThread.main(ActivityThread.java:4895)
    java.lang.reflect.Method.invokeNative(Native Method)
    java.lang.reflect.Method.invoke(Method.java:511)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
    dalvik.system.NativeStart.main(Native Method)
-------------------------------

无论如何,我都不知道应用程序强制停止。但是当切换片段如下时,我发布了我的代码:

切换片段:

Activity base = null;
if (e.sender instanceof Activity) {
    base = (Activity) e.sender;
} else if (e.sender instanceof Fragment) {
    base = ((Fragment) e.sender).getActivity();
}

FragmentManager fm = base.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

removeAllInBackStack(fm);
NoteListView note = (NoteListView) base.getFragmentManager().findFragmentByTag(NoteListView.TAG);
if (note == null) {
    note = NoteListView.newInstance();
    ft.replace(R.id.fragDetail, note, NoteListView.TAG);
} else {
    ft.remove(note);
    note = NoteListView.newInstance();
    ft.replace(R.id.fragDetail, note, NoteListView.TAG);
}
ft.addToBackStack(NoteListView.TAG);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();

删除片段:

public void removeAllInBackStack(FragmentManager fm) {
    for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
        fm.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
}

包含片段的布局活动:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/WHITE"
    android:orientation="horizontal" >

    <ListView android:background="@drawable/bg_vnm_menu_none"
        android:id="@+id/lvMenu" android:padding="0dp"
        android:layout_width="50dp"
        android:layout_height="fill_parent" >
    </ListView>

    <FrameLayout 
        android:id="@+id/fragDetail"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    </FrameLayout>

</LinearLayout>
4

0 回答 0