简体   繁体   English

FragmentManager给了我NPE

[英]FragmentManager gives me NPE

I'm checking fragments for the first time and I created a LinearLayout with a FrameLayout within and for my layout-sw600dp a LinearLayout with 2 FrameLayouts. 我是第一次检查片段,我创建了一个带有FrameLayout的LinearLayout,并且为我的layout-sw600dp创建了一个带有2个FrameLayouts的LinearLayout。

The one has an id of "main" and the other is "details". 一个ID为“ main”,另一个ID为“ details”。

The error I get is : 我得到的错误是:

 Caused by: java.lang.NullPointerException
        at android.app.BackStackRecord.doAddOp(BackStackRecord.java:395)
        at android.app.BackStackRecord.add(BackStackRecord.java:385)
        at mes.fallstudio.tvfall.MainActivity.onCreate(MainActivity.java:28)

where line 28 is the last line here: 第28行是这里的最后一行:

public class MainActivity extends AppCompatActivity {
private android.support.v4.app.Fragment mainFragment;
private android.app.Fragment detailsFragment;
private Boolean isDualPane = false;

private android.support.v4.app.FragmentManager fm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    mainFragment = fragmentManager.findFragmentById(R.id.main);

    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.main, mainFragment).commit();


}
}

XML layout files : XML布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">


<FrameLayout
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

<FrameLayout
    android:id="@+id/details"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

this is for layout-sw600dp and the same without the 2nd FrameLayout ( details) for the xml file for the layout folder. 这是用于layout-sw600dp的,不包含第二个FrameLayout(详细信息)的布局文件夹的xml文件。

I'm extending AppCompatActivity if this helps. 如果有帮助,我正在扩展AppCompatActivity。 Thanks you 谢谢

AppCompatActivity uses getSupportFragmentManager() instead of getFragmentManager() . AppCompatActivity使用getSupportFragmentManager()而不是getFragmentManager()

Reference: https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html 参考: https : //developer.android.com/reference/android/support/v7/app/AppCompatActivity.html

EDIT: 编辑:

In your code you try to get mainFragment from the XML layout. 在您的代码中,您尝试从XML布局中获取mainFragment However, you do not define it there. 但是,您未在此处定义它。 You can either instantiate mainFragment in your code: 您可以在代码中实例化mainFragment

mainFragment = new MainFragment();

Or define it in your XML: 或在您的XML中定义它:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

<fragment android:name="mes.fallstudio.tvfall.MainFragment"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<FrameLayout
    android:id="@+id/details"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

In the latter case you don't need to use the FragmentManager at all in your code unless you need to manipulate the fragments at run time. 在后一种情况下,除非在运行时需要操作片段,否则根本不需要在代码中使用FragmentManager

In both cases you also need the actual fragment: 在这两种情况下,您还需要实际的片段:

public class MainFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.main_fragment, container, false);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM