java.lang.NullPointerException:尝试在空对象上调用虚拟方法'java.lang.String android.os.Bundle.getString(java.lang.String)'

时间:2018-07-23 07:54:44

标签: java android

美好的一天,我是android studio的新手,我目前正在尝试制作电影评论应用程序以开始我的android studio之旅。我试图通过在imagebutton上使用onClick并意图Movie1.java从另一个java类中获取数据,但是我不明白为什么会收到此错误:

    com.example.dynamis.calibremovie E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dynamis.calibremovie, PID: 6109
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dynamis.calibremovie/com.example.dynamis.calibremovie.Movie1}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
    at com.example.dynamis.calibremovie.Movie1.retrieveData(Movie1.java:117)
    at com.example.dynamis.calibremovie.Movie1.onCreate(Movie1.java:108)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

这是我的movie1.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class Movie1 extends AppCompatActivity {

    private String id = "";
    private String title = "";
    private String genre = "";
    private String director = "";
    private String synopsis = "";
    private String year = "";
    private String runTime = "";

    private MovieCollection movieCollection = new MovieCollection();

    // other code here...

    private void retrieveData()
    {
        Bundle MovieData = this.getIntent().getExtras();

        id = MovieData.getString( "id");
        title = MovieData.getString( "title");
        genre = MovieData.getString( "genre");
        director = MovieData.getString( "director");
        synopsis = MovieData.getString( "synopsis");
        year = MovieData.getString( "year");
        runTime = MovieData.getString( "runTime");
    }

    private void displayMovie(String title, String genre, String director, String synopsis, String year, String runTime)
    {
        TextView txtTitle = findViewById(R.id.Title4);

        txtTitle.setText(title);

        TextView txtGenre = findViewById(R.id.Genres4);

        txtGenre.setText(genre);

        TextView txtDirector = findViewById(R.id.Director2);

        txtDirector.setText(director);

        TextView txtSynopsis = findViewById(R.id.Synopsis5);

        txtSynopsis.setText(synopsis);

        TextView txtYear = findViewById(R.id.Year2);

        txtYear.setText(year);

        TextView txtRunTime = findViewById(R.id.Runtime2);

        txtRunTime.setText(runTime);
    }
}

非常感谢您的帮助,谢谢

0 个答案:

没有答案