应用程序在启动时以双窗格模式崩溃

时间:2016-01-27 13:18:21

标签: java android xml android-layout user-interface

实际上,我正在制作一个显示热门电影的应用程序(udacity课程的热门电影应用项目),我有两个活动主要活动和细节活动,但当我试图让我的应用程序在平板电脑上运行它崩溃,并且可以'甚至显示启动应用程序的主要活动,我不知道缺少什么.... 此外,我想知道如何指定我的应用程序应该在平板电脑的不同屏幕尺寸上运行..

MainActivity.java

public class MainActivity extends AppCompatActivity {

private static final String DETAILFRAGMENT_TAG ="DFTAG" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

if (findViewById(R.id.movie_detail_container) != null) {
    mTwoPane = true;
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.movie_detail_container, new MovieDetailFragment(),DETAILFRAGMENT_TAG)
                .commit();
        Log.v(LOG_TAG, "CRASH ");
    }
} else {
    mTwoPane = false;
}

MovieDetail.java

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


if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.movie_detail_container, new MovieDetailFragment())
            .commit();
}
}

activity_main.xml中

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/fragment_movie"
 android:name=".MoviesFragment"
 tools:context=".MoviesFragment"
 tools:layout="@android:layout/list_content"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 />

sw600dp \ activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:baselineAligned="false"
 android:divider="?android:attr/dividerHorizontal"
 android:orientation="horizontal">

 <fragment
  android:id="@+id/fragment_movie"
  android:name=".MoviesFragment"
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:layout_weight="3" />

  <FrameLayout
    android:id="@+id/movie_detail_container"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="4" />

activity_movie_detail.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/movie_detail_container"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MovieDetail"
 tools:ignore="MergeRootFrame" />

logcat的

java.lang.RuntimeException: Unable to start activity ComponentInfo{.popularmovies/.popularmovies.MainActivity}:  java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)

2 个答案:

答案 0 :(得分:0)

我认为您没有正确获取xml资源引用。尝试添加&#34; movie_detail_container&#34;在/ mainActivity xml中也是如此。然后尝试

答案 1 :(得分:0)

activity_movie_detail.xmlactivity_main.xml都包含同名“movie_detail_container”的视图。当你打电话

getSupportFragmentManager().beginTransaction()
                .replace(R.id.movie_detail_container, new MovieDetailFragment(),DETAILFRAGMENT_TAG)
                .commit();

由于movie_detail_container的{​​{1}}尚未activity_movie_detail因此inflated

,因此感到困惑