簡體   English   中英

我的應用程序在模擬器或Android手機上打開時不斷崩潰

[英]My app keeps crashing when opening on the emulator or android phone

package com.ebonybutler.cexample3;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo; 
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(Main.this, Second.class));

        }
    });
}

}

每次我嘗試運行它,錯誤

'抱歉! 應用程序example3(processcom.ebonybutler.cexample3)意外停止。 請再試一次。'

我無法弄清楚在啟動時是什么讓它崩潰,因為所有的java文件似乎都很好或者至少沒有任何錯誤。 請幫忙! 我已經在下面的logcat中添加了信息,但我正在嘗試解釋它對我說的內容。

11-02 09:29:10.261: E/AndroidRuntime(276): FATAL EXCEPTION: main
11-02 09:29:10.261: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ebonybutler.cexample3/com.ebonybutler.cexample3.Main}: java.lang.NullPointerException
11-02 09:29:10.261: E/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-02 09:29:10.261: E/AndroidRuntime(276):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-02 09:29:10.261: E/AndroidRuntime(276):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-02 09:29:10.261: E/AndroidRuntime(276):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-02 09:29:10.261: E/AndroidRuntime(276):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-02 09:29:10.261: E/AndroidRuntime(276):  at android.os.Looper.loop(Looper.java:123)
11-02 09:29:10.261: E/AndroidRuntime(276):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-02 09:29:10.261: E/AndroidRuntime(276):  at java.lang.reflect.Method.invokeNative(Native Method)
11-02 09:29:10.261: E/AndroidRuntime(276):  at java.lang.reflect.Method.invoke(Method.java:521)
11-02 09:29:10.261: E/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-02 09:29:10.261: E/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-02 09:29:10.261: E/AndroidRuntime(276):  at dalvik.system.NativeStart.main(Native Method)
11-02 09:29:10.261: E/AndroidRuntime(276): Caused by: java.lang.NullPointerException
11-02 09:29:10.261: E/AndroidRuntime(276):  at com.ebonybutler.cexample3.Main.onCreate(Main.java:21)
11-02 09:29:10.261: E/AndroidRuntime(276):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-02 09:29:10.261: E/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-02 09:29:10.261: E/AndroidRuntime(276):  ... 11 more

看起來你有一個空指針問題 - 在Main.java,第21行。

你的onCreate方法有一個NullPointerException(第21行)。 常見的錯誤通常是在實際設置布局之前通過findViewById獲取指向視圖的指針。 這可能是你的問題嗎? 如果您無法解決問題,請發布onCreate方法的內容(包括行號)。

按照我的計算,當您為按鈕設置監聽器時會發生這種情況。 你確定這個按鈕在你的布局的xml文件中仍然有ID按鈕1嗎? 如果您的布局中現在更長時間存在該ID,則會發生此錯誤

Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() { ... // <-- probably occurring here

編輯

所以在代碼方面,這就是我所說的。 我說,也許你曾經在你的main.xml文件中有一個id為“button1”的Button。 (見下文)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
    >
    <LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <Button android:id="@+id/button1 android:layout_height="wrap_content" android:layout_width="wrap_content" ...></Button>

但也許現在,您可能已經更改了布局xml中的id而沒有更改您的代碼(請參閱下面的'newid')

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
    >
    <LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
        <Button android:id="@+id/newid" android:layout_height="wrap_content" android:layout_width="wrap_content" ...></Button>

在這種情況下, findViewById將返回null,因為main.xml文件中不再存在此類id。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM