0

I want to pass data from list of Gson data in MainActivity to another activity using intent.putExtra and set it's value into a textView but my app always force close

this is my code

        mAdapter.setmMoviesLists(movies);
        mRecyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    // TODO Handle item click
                    Movie data = movies.get(position);
                    final Context context = view.getContext();
                    Intent intent = new Intent(MainActivity.this, Child.class);
                    intent.putExtra("movie", new GsonBuilder().create().toJson(data));
                    context.startActivity(intent);
                }
            })
    );

This is code at Child class

public class Child extends AppCompatActivity {

TextView mTitle;
TextView mDescription;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_child);

    Movie data = new GsonBuilder().create().fromJson(this.getIntent().getStringExtra("movie"), Movie.class);
    mTitle = (TextView) findViewById(R.id.title);
    mTitle.setText(data.getTitle().toString());
    mDescription = (TextView) findViewById(R.id.title);
    mDescription.setText(data.getDescription().toString());

}}

and this what i got on logcat

  07-10 18:15:07.441 18326-18326/com.example.nurram.popularmovie E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.nurram.popularmovie, PID: 18326
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nurram.popularmovie/com.example.nurram.popularmovie.Child}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:153)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5451)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                                                                                 at com.example.nurram.popularmovie.Child.onCreate(Child.java:25)
                                                                                 at android.app.Activity.performCreate(Activity.java:6323)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:153) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5451) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 Answers1

0

Key points:

  1. mTitle and mDescription referring to same id i.e. R.id.title
  2. Cross check value of title and description when you click on item

Changes in onItemClick():

Movie data = movies.get(position);
final Context context = view.getContext();
Intent intent = new Intent(MainActivity.this, Child.class);
intent.putExtra("movie", new GsonBuilder().create().toJson(data, Movie.class));
context.startActivity(intent);

Hope it helps.

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42