Java SetLayoutParams空指针执行选项

Java SetLayoutParams空指针执行选项,java,android,android-layout,android-fragments,Java,Android,Android Layout,Android Fragments,当我试图调试我的应用程序时,我得到了一个空指针验证。我认为这是因为在我的片段onCreateView()方法中,容器为null,当调用android.view.view.setLayoutParams(android.view.ViewGroup$LayoutParams)时,有一个exeption。我不知道为什么容器为空。我将片段添加到XML文件中的活动中,当我以“运行模式”运行应用程序时,片段将显示在活动中。我只在尝试调试时看到此错误 活动: @Override protected void

当我试图调试我的应用程序时,我得到了一个空指针验证。我认为这是因为在我的片段onCreateView()方法中,容器为null,当调用android.view.view.setLayoutParams(android.view.ViewGroup$LayoutParams)时,有一个exeption。我不知道为什么容器为空。我将片段添加到XML文件中的活动中,当我以“运行模式”运行应用程序时,片段将显示在活动中。我只在尝试调试时看到此错误

活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
   // moviePosterFragment = new MoviePosterFragment();
    moviePosterFragment = (MoviePosterFragment)getSupportFragmentManager().findFragmentById(R.id.fragment);

   FetchMovies(1);

}
活动xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout

android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
    android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>


<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment"
android:name="com.example.user.popmovie.fragments.MoviePosterFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout="@layout/fragment_main" />


</android.support.design.widget.CoordinatorLayout>
片段XML:

<FrameLayout
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:id = "@+id/fragment"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:showIn="@layout/activity_main"


tools:context=".MoviePosterFragment">


<!-- Add a RecyclerView to fragment layout-->
<android.support.v7.widget.RecyclerView
    android:id="@+id/pop_movies_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</FrameLayout>

崩溃发生在活动的onCreate方法中,请共享该代码。rootView.setLayoutParams(新建ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT))引发异常;??这在你的代码中被注释掉了。。。如果需要,请编辑您的帖子,以包含未注释掉的有问题的代码行。请尝试清理并编译..调试之前..@Kevinze该代码已注释掉,我正在尝试手动设置布局参数,查看错误是否消失,但没有luck@cutcut哦,好的。我真的不知道是什么原因导致了错误,但也许您可以尝试将onCreateView中的大部分代码转移到onActivityCreated中,看看这是否有帮助,因为您正在调用getActivity()。
<FrameLayout
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:id = "@+id/fragment"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:showIn="@layout/activity_main"


tools:context=".MoviePosterFragment">


<!-- Add a RecyclerView to fragment layout-->
<android.support.v7.widget.RecyclerView
    android:id="@+id/pop_movies_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</FrameLayout>
03-06 20:33:20.675 27556-27556/? E/Zygote: MountEmulatedStorage()
03-06 20:33:20.675 27556-27556/? E/Zygote: v2
03-06 20:33:20.695 27556-27556/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime: FATAL EXCEPTION: main
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime: Process: com.example.user.popmovie, PID: 27556
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.popmovie/com.example.user.popmovie.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:177)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5951)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:928)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:110)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at com.example.user.popmovie.activities.MainActivity.onCreate(MainActivity.java:38)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6289)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) 
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:177) 
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449) 
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5951) 
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
03-06 20:34:57.049 27556-27556/com.example.user.popmovie E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)