This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
我有一个带有三个ActivityFragmentsButtonsFragmentOneFragmentTwo类的FragmentThree。按下Button1时,必须显示FragmentOne并隐藏FragmentTwoFragmentThree。如果按下Button2,则必须显示FragmentTwo并隐藏FragmentOneFragmentThree。同样,如果按下Button3,则必须显示FragmentThree并隐藏FragmentTwoFragmentOne。我的代码无法显示NullPointerException E/AndroidRuntime(362): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:656)

Logcat:

04-08 08:10:31.830: E/AndroidRuntime(362): FATAL EXCEPTION: main
04-08 08:10:31.830: E/AndroidRuntime(362): java.lang.NullPointerException
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:656)
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.os.Handler.handleCallback(Handler.java:587)
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.os.Handler.dispatchMessage(Handler.java:92)
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.os.Looper.loop(Looper.java:123)
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.app.ActivityThread.main(ActivityThread.java:4627)
04-08 08:10:31.830: E/AndroidRuntime(362):      at java.lang.reflect.Method.invokeNative(Native Method)
04-08 08:10:31.830: E/AndroidRuntime(362):      at java.lang.reflect.Method.invoke(Method.java:521)
04-08 08:10:31.830: E/AndroidRuntime(362):      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-08 08:10:31.830: E/AndroidRuntime(362):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-08 08:10:31.830: E/AndroidRuntime(362):      at dalvik.system.NativeStart.main(Native Method)


You can find it here too

这是代码:MainActivity.java

 public class MainActivity extends FragmentActivity implements OnClickListener {
    Button b1, b2, b3;
    Fragment f1, f2, f3;
    FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        manager = getSupportFragmentManager();

        f1 = manager.findFragmentById(R.id.first);
        f2 = manager.findFragmentById(R.id.second);
        f2 = manager.findFragmentById(R.id.third);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            if(b1.isPressed()){
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            transaction.hide(f2);
            transaction.hide(f3);
            transaction.show(f1);
            transaction.commit();
            }
            break;
        case R.id.button2:
            if(b2.isPressed()){
            FragmentTransaction transaction1 = getSupportFragmentManager()
                    .beginTransaction();
            transaction1.hide(f1);
            transaction1.hide(f3);
            transaction1.show(f2);
            transaction1.commit();
            }
            break;
        case R.id.button3:
            if(b3.isPressed()){
            FragmentTransaction transaction2 = getSupportFragmentManager()
                    .beginTransaction();
            transaction2.hide(f1);
            transaction2.hide(f2);
            transaction2.show(f3);
            transaction2.commit();
            }
            break;
        }

    }

    }


    public class FirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.first, container, false);
        return view;
    }
    }
    public class SecondFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.second, container, false);
        return view;
    }
    }
    public class Third extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.third, container, false);
        return view;
    }
    }


布局文件:main.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=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Button 1" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button1"
    android:layout_toRightOf="@+id/button1"
    android:text="Button 2" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button2"
    android:layout_toRightOf="@+id/button2"
    android:text="Button 3" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/first"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.fragmentworks.FirstFragment" />

    <fragment
        android:id="@+id/second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.fragmentworks.SecondFragment" />

    <fragment
        android:id="@+id/third"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.fragmentworks.Third" />
</LinearLayout>

最佳答案

您有错字:

f1 = manager.findFragmentById(R.id.first);
f2 = manager.findFragmentById(R.id.second);
f2 = manager.findFragmentById(R.id.third);


最后一行应为f3 =....

阿里

关于android - fragment 管理器上的空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15870542/

10-12 03:11