我想使用intent.putExtra将MainActivity中Gson数据列表中的数据传递给另一个活动,并将其值设置为textView但我的应用程序始终强制关闭
这是我的代码
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);
}
})
);
这是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());
}}
这就是我对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)
答案 0 :(得分:0)
关键点:
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);
希望它有所帮助。