空指针异常错误

发布于 2024-09-14 15:15:19 字数 3055 浏览 5 评论 0原文

Logcat

08-17 06:37:22.264: ERROR/AndroidRuntime(1090): java.lang.RuntimeException: Unable to start activity ComponentInfo{one.two/one.two.Booking}: java.lang.NullPointerException
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.os.Looper.loop(Looper.java:123)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.ActivityThread.main(ActivityThread.java:3948)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at java.lang.reflect.Method.invokeNative(Native Method)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at java.lang.reflect.Method.invoke(Method.java:521)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at dalvik.system.NativeStart.main(Native Method)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): Caused by: java.lang.NullPointerException
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at one.two.Booking.onCreate(Booking.java:50)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)

Booking.java

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        db = new DBAdapter(this);
        this.setContentView(R.layout.booking);
        txtArrivalTime = (TextView) findViewById(R.id.txtArrivalTime); // Member assignment
        txtFerry = (TextView) findViewById(R.id.txtFerry);
        txtStatus = (TextView) findViewById(R.id.txtStatus);
        txtDestination = (TextView) findViewById(R.id.txtDestination);
        colourSpinner = (Spinner) findViewById(R.id.myspinner); // Member assignment
        Button btnBook = (Button) findViewById(R.id.btnBook);
        db.open();
        fillData();
        db.close();
        colourSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
        b = this.getIntent().getExtras();
        s = b.getString("DEFAULTTEXT");      

        btnBook.setOnClickListener(new MyOnClickListener());


    }

第 50 行是 s = b.getString("DEFAULTTEXT");

谢谢。

Logcat

08-17 06:37:22.264: ERROR/AndroidRuntime(1090): java.lang.RuntimeException: Unable to start activity ComponentInfo{one.two/one.two.Booking}: java.lang.NullPointerException
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.os.Looper.loop(Looper.java:123)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.ActivityThread.main(ActivityThread.java:3948)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at java.lang.reflect.Method.invokeNative(Native Method)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at java.lang.reflect.Method.invoke(Method.java:521)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at dalvik.system.NativeStart.main(Native Method)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090): Caused by: java.lang.NullPointerException
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at one.two.Booking.onCreate(Booking.java:50)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-17 06:37:22.264: ERROR/AndroidRuntime(1090):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)

Booking.java

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        db = new DBAdapter(this);
        this.setContentView(R.layout.booking);
        txtArrivalTime = (TextView) findViewById(R.id.txtArrivalTime); // Member assignment
        txtFerry = (TextView) findViewById(R.id.txtFerry);
        txtStatus = (TextView) findViewById(R.id.txtStatus);
        txtDestination = (TextView) findViewById(R.id.txtDestination);
        colourSpinner = (Spinner) findViewById(R.id.myspinner); // Member assignment
        Button btnBook = (Button) findViewById(R.id.btnBook);
        db.open();
        fillData();
        db.close();
        colourSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
        b = this.getIntent().getExtras();
        s = b.getString("DEFAULTTEXT");      

        btnBook.setOnClickListener(new MyOnClickListener());


    }

Line 50 is s = b.getString("DEFAULTTEXT");

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小红帽 2024-09-21 15:15:19

而不是这样:

b = this.getIntent().getExtras();
s = b.getString("DEFAULTTEXT"); 

使用 getStringExtra(String)

s = this.getIntent().getStringExtra("DEFAULTTEXT");

从 getExtras() 获得的包为 null,因此出现异常

Instead of this:

b = this.getIntent().getExtras();
s = b.getString("DEFAULTTEXT"); 

Use getStringExtra(String)

s = this.getIntent().getStringExtra("DEFAULTTEXT");

The bundle you get from getExtras() is null, hence the exception

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文