1

尝试加载片段时出现 NullPointerException。

堆栈跟踪在这里:

08-07 00:43:11.780: E/AndroidRuntime(1366): FATAL EXCEPTION: main
08-07 00:43:11.780: E/AndroidRuntime(1366): java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.ioto.atractr_v2/ca.ioto.atractr_v2.Atractr}: java.lang.NullPointerException
08-07 00:43:11.780: E/AndroidRuntime(1366):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at android.os.Looper.loop(Looper.java:137)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at java.lang.reflect.Method.invokeNative(Native Method)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at java.lang.reflect.Method.invoke(Method.java:511)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at dalvik.system.NativeStart.main(Native Method)
08-07 00:43:11.780: E/AndroidRuntime(1366): Caused by: java.lang.NullPointerException
08-07 00:43:11.780: E/AndroidRuntime(1366):     at android.app.BackStackRecord.run(BackStackRecord.java:661)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at android.app.Activity.performStart(Activity.java:5113)
08-07 00:43:11.780: E/AndroidRuntime(1366):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
08-07 00:43:11.780: E/AndroidRuntime(1366):     ... 11 more

我所有的 XML 都是正常声明的,我的起始活动的 XML 看起来像这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Atractr" >

<fragment
    android:id="@+id/fragment1"
    android:name="ca.ioto.atractr_v2.FindNearestDevicesFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:layout="@layout/findnearestdevices_fragment" />

<fragment
    android:id="@+id/fragment2"
    android:name="ca.ioto.atractr_v2.ChatFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fragment1"
    android:layout_alignTop="@+id/fragment1"
    tools:layout="@layout/chat_fragment" />

</RelativeLayout>

我的onCreate活动方法如下所示:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_atractr);
    if (!chordStarted) {
        initializeChord();
    }
    newFragment = (FindNearestDevicesFragment)getFragmentManager().findFragmentById(R.id.fragment1);
    chatFragment = (ChatFragment)getFragmentManager().findFragmentById(R.id.fragment2);
    changeFragment(FINDNEARESTDEVICES);
}

我没有看到我在哪里得到 NullPointerException (或者为什么它被抛出)。

编辑:请求了更多代码,所以我添加了更多代码:

initializeChord方法:

private void initializeChord(){
    chordMan = ChordManager.getInstance(this);
    chordMan.setTempDirectory("/storage/emulated/0/Download");
    chordMan.setHandleEventLooper(getMainLooper());
    ArrayList<Integer> interfaces = (ArrayList<Integer>) chordMan.getAvailableInterfaceTypes();
    int result = chordMan.start(interfaces.get(0), chordManListener);
    if (result == ChordManager.ERROR_FAILED) {
        Utils.singleButtonAlert("Error","An error occured when starting Chord", this);
        return;
    }
    chordStarted = true;
}

changeFragment方法:

   private void changeFragment(int changeToFragment)
{
    fragTransaction = getFragmentManager().beginTransaction();
    switch (changeToFragment){

    case FINDNEARESTDEVICES:
        currentFragment = changeToFragment;
        fragTransaction.show(newFragment);
        fragTransaction.hide(chatFragment);
        break;
    case CHAT:
        currentFragment = changeToFragment;
        fragTransaction.show(chatFragment);
        fragTransaction.hide(newFragment);
        break;
    }
    fragTransaction.commit();
}

Atractr 类的一些类成员:

private ChordManager chordMan;
private IChordChannel commChannel;
private FindNearestDevicesFragment newFragment;
private ChatFragment chatFragment;
private FragmentTransaction fragTransaction;
private static boolean chordStarted = false;
public final static int FINDNEARESTDEVICES = 1000;
public final static int CHAT = 1001;
private static int currentFragment = FINDNEARESTDEVICES; 
4

1 回答 1

2

函数显示和隐藏现有片段上的作品

隐藏现有片段。这仅与视图已添加到容器的片段相关

显示以前隐藏的片段。这仅与视图已添加到容器的片段相关

但是片段与活动有不同的生命周期,当活动的 onCreate 被调用时,它们可能还没有被创建和附加。

当您调用您的 changeFragment 或 changeFragment 内部时检查 null。

如果您希望在开始时隐藏其中一个片段,请考虑在 onAttached() 方法中从片段进行回调调用,或者手动添加片段而不是 xml。

我的首选选项是手动添加片段。

于 2013-08-07T15:01:40.683 回答