[英]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.