2011-11-02 99 views
0
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)); 

     } 
    }); 
} 

} 

每次我嘗試運行它在打開時崩潰,錯誤我的應用程序保持模擬器或Android手機

「對不起!應用程序示例3(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 

回答

0

當我爲你的按鈕設置監聽器時,我的計數發生了。你確定這個按鈕的佈局的xml文件中仍然有ID button1?如果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文件中不再存在此類標識。

+0

通常在那裏我得到了自動覆蓋。這是不對的?它應該是什麼呢?我從來沒有在這之前或在我的任何其他Java文件的問題。感謝您的幫助mjmarsh。 – user1026229

+0

看到我的編輯對我的想法是錯誤的解釋 –

+0

感謝您的幫助mjmarsh!這似乎有幫助!它在仿真器和電話上再次運行。 – user1026229

2

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

+0

如果您無法確定該行的原因是什麼,請發佈該方法的內容,您可能會獲得幫助。 – broschb

+0

感謝您的幫助broschb。我已經回去並在上面的原始文章中發佈了main.java代碼。 – user1026229

0

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

+0

我對創建應用程序和一般使用java相對較新,所以請耐心等待。你知道我怎麼能解決這個問題。既然你從我的Main.java文件中提到了這一行,我將添加一些代碼。 – user1026229

相關問題