android - fragment 管理器上的空指针异常

标签 android android-fragments android-button android-fragmentactivity

我有一个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)

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;
    }
    }

布局文件:ma​​in.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/

相关文章:

android - 如何使用 Espresso 为 RecyclerView 中的项目设置 IdlingResource?

javascript - 如何从fragment中的webview调用JavaScript?

android - fragment 中的 startActivityForResult 不工作

android - 添加按钮的最佳方式是什么?

android - Android 中的 MDM 工作流程

java - 注册后按返回键进入登录界面

android - 如何以编程方式断开与所选路线的连接?

android - android studio的HierarchyViewer工具如何知道android屏幕 View 的属性

Android:如何使按钮标题居中?

java - 如何让按钮永久不可点击