當我嘗試在我的AVD中運行它時,它給了我一個錯誤: 對不起!應用程序Hello World(進程com.duncan.hello.world)意外停止。請再試一次。 我認爲它與添加第二個按鈕的代碼有關,因爲在此之前它工作正常。這是我在我的主要的Java文件中的代碼:試圖添加按鈕時出錯
package com.duncan.hello.world;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.duncan.hello.world.R;
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button aButton;
aButton = (Button) this.findViewById(R.id.button1);
aButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent (HelloWorldActivity.this, OtherActivity.class);
startActivity(i);
}
});
Button newButton;
newButton = (Button) this.findViewById(R.id.meh);
newButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(HelloWorldActivity.this, Meh.class);
startActivity(i);
}
});
}
}
這是other.xml(的佈局,其中MEH是):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="you...are...on...page...2...!!!" />
<Button
android:id="@+id/meh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="meh"/>
<Button
android:id="@+id/p40"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this go to p40" />
</LinearLayout>
這裏是Meh.java:
package com.duncan.hello.world;
import com.duncan.hello.world.R;
import android.app.Activity;
import android.os.Bundle;
public class Meh extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.meh);
}
}
logcat的錯誤: 12-07 14:40:00.840:E/AndroidRuntime(497):致命異常:主
12-07 14:40:00.840:E/AndroidRuntime(497):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.duncan.hello.world/com.duncan.hello.world.HelloWorldActivity}:java.lang.NullPointerException
12-07 14:40:00.840:E/AndroidRuntime(497):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-07 14:40:00.840:E/AndroidRuntime (497):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-07 14:40:00.840:E/AndroidRuntime(497):at android.app.ActivityThread.access $ 2300(ActivityThread .java:125)
12-07 14:40:00.840:E/AndroidRuntime(497):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
12-07 14:40:00.840:E/AndroidRuntime(497):at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 14:40:00.840:E/AndroidRuntime(497):at android.os.Looper.loop(Looper的.java:123)
12-07 14:40:00.840:E/AndroidRuntime(497):在android.app.ActivityThread.main (ActivityThread.java:4627)
12-07 14: 40:00.840:E/AndroidRuntim e(497):at java.lang.reflect.Method.invokeNative(Native Method)
12-07 14:40:00.840:E/AndroidRuntime(497):at java.lang.reflect.Method.invoke Method.java:521)
12-07 14:40:00.840:E/AndroidRuntime(497):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
12-07 14:40:00.840:E/AndroidRuntime(497):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-07 14:40:00.840:E/AndroidRuntime(497):at dalvik.system.NativeStart.main(Native Method)
12-07 14:40:00.840:E/AndroidRuntime(497):顯示java.lang.NullPointerException
12-07 14:40:致00.840:E/AndroidRuntime(497):在玉米.duncan.hello.world.HelloWorldActivity.onCreate(HelloWorldActivity。的java:32)
12-07 14:40:00.840:E/AndroidRuntime(497):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-07 14:40: 00.840:E/AndroidRuntime(497):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-07 14:40:00.840:E/AndroidRuntime(497):... 11更
它在LogCat中說了什麼? 1.確保meh按鈕在那裏,它確實是一個按鈕,而不是一個imagebutton。 2.確保你的Meh活動添加在Android.manifest文件中 – Alin
我在包中有Meh.java文件,並且我在清單中有Meh.activity。 –
使用'adb logcat'命令獲取有關您的錯誤的堆棧跟蹤和其他詳細信息。 –