当我试图调试我的应用程序时,我得到一个空指针执行。我想我的片段onCreateView()方法,容器是null,当调用android.view.View.setLayoutParams(android.view.ViewGroup $ LayoutParams)时,有一个exeption。我不知道为什么容器是null。我将片段添加到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>
片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mlayoutManager = new GridLayoutManager(getActivity(),NUM_COLUMNS);
mRecyclerView = (RecyclerView)rootView.findViewById(R.id.pop_movies_recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mlayoutManager);
//set adapter with empty data
GridAdapter = new GridViewAdapter(getContext(),movieItemArray,mRecyclerView);
mRecyclerView.setAdapter(GridAdapter);
// mProgressWheel = (ProgressWheel) rootView.findViewById(android.R.id.progress);
endlessScroll();
Activity act = getActivity();
int a =0;
// rootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
if (container == null) {
Log.i("in movieposterfragment", "onCreateView(): container = null");
}
else
{
Log.i("in movieposterfragment", "onCreateView(): container != null");
}
return rootView;
}
片段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>
堆栈跟踪
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)