1

Actually iam making an app which displays popular movies ( popular movie app project of udacity course) and i have two activites main activity and detail activity but when i tried to make my app run on a tablet it crashes and can't display even the main activity on launching the app and i dont know what is missing .... Also , i want to know how can i specify that my app should run on different screen sizes for tablets..

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)
Suzy
  • 99
  • 2
  • 9

2 Answers2

0

I think you are not getting the xml resource reference correctly. try to add "movie_detail_container" in /mainActivity xml too. and then try

Junaid
  • 316
  • 5
  • 23
0

Both your activity_movie_detail.xml and activity_main.xml contains a view with same name "movie_detail_container". When you call

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

It gets confused since the movie_detail_container of activity_movie_detail has not yet been inflated and is therefore null

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
JNS
  • 1
  • 2
  • but i guess both of them need to have same name as i want to inflate the detail frame along with the main one for example , the main activity on left and detail activity on the right ? – Suzy Jan 27 '16 at 14:17