2

我有一个ActivityFragments三个ButtonsFragmentOne, FragmentTwo,FragmentThree类。按下时Button1它必须显示FragmentOne和隐藏FragmentTwoFragmentThree。如果Button2按下它必须显示FragmentTwo和隐藏FragmentOneFragmentThree。同样,如果Button3按下它必须显示FragmentThree和隐藏FragmentTwoFragmentOne。我的代码无法显示NullPointerException E/AndroidRuntime(362): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:656)

日志猫:

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)

你也可以在这里找到

这是代码: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>

4

1 回答 1

2

你有一个错字:

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

最后一行应该是f3 =....

阿里。

于 2013-04-08T05:02:48.853 回答