2

私はActivityFragments3つのクラスButtonsFragmentOne、、、クラスを持っています。を押すと、 と が表示および非表示になります。が押された場合、表示および非表示にする必要があります。同様に、が押された場合は、表示と非表示を切り替える必要があります。コードが表示されませんでしたFragmentTwoFragmentThreeButton1FragmentOneFragmentTwoFragmentThreeButton2FragmentTwoFragmentOneFragmentThreeButton3FragmentThreeFragmentTwoFragmentOneNullPointerException 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 に答える