2011-12-07 44 views
-1

當我嘗試在我的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更

+0

它在LogCat中說了什麼? 1.確保meh按鈕在那裏,它確實是一個按鈕,而不是一個imagebutton。 2.確保你的Meh活動添加在Android.manifest文件中 – Alin

+0

我在包中有Meh.java文件,並且我在清單中有Meh.activity。 –

+0

使用'adb logcat'命令獲取有關您的錯誤的堆棧跟蹤和其他詳細信息。 –

回答

0

你可以給你的logcat輸出嗎?如果在添加第二個按鈕後出現問題,那麼是否因爲Meh.java不在您的mainfest文件中?

如果將視圖(右上角)切換到DDMS,然後單擊LogCat,可以找到Logcat輸出。單擊logcat窗口右側的W或E,它會顯示出現的警告和錯誤。你的崩潰將是紅色的,所以請複製粘貼該部分來向我們展示問題。

你的問題是,您聲明

Button aButton; 

調用超級onCreate方法之前。在做其他事情之前,你必須調用super.onCreate(bundleInstanceState)。

你想要做的更像是:

public class HelloWorldActivity extends Activity { 
    Button aButton; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //whatever button listeners you want to create and initialize buttons 
    } 
} 

你提到「還行吧」按鈕處於other.xml。它應該在main.xml中。在main.xml中添加一個id爲'meh'的按鈕。

+0

是:

+0

將整個路徑添加到名稱中,並顯示logcat錯誤消息。 – digerati32

+0

仍然無法正常工作。 –

0

問題是你的內容視圖是main.xml - 而不是other.xml。如果你的'meh'按鈕不在main.xml中,你會得到一個NullPointerException異常。