2011-08-03 26 views
0

我正在嘗試運行一個簡單的Android教程。我已經寫了幾個了,期待源找不到錯誤有點誤導,所以我忽略它,但這是一個堆棧跟蹤和一些代碼。它似乎是一些空指針,但我似乎無法解決它。Android java.land.nullpointer在onClick上找不到異常或來源(View v)

package com.rlee.randomquotes; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 

public class RandomQuotes extends Activity { 
    DBAdapter db = new DBAdapter(this); 
    EditText Quote; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     // Capture our button from layout 
     Button setButton = (Button)findViewById(R.id.go); 
     Button getButton = (Button)findViewById(R.id.genRan); 
     // Register the onClick listener with the implementation above 
     setButton.setOnClickListener(mAddListener); 
     getButton.setOnClickListener(mAddListener); 
    } 

    // Create an anonymous implementation of OnClickListener. 

    private OnClickListener mAddListener = new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
     } 
    }; 

}

和錯誤

08-03 21:08:18.325: ERROR/jdwp(2383): Failed sending reply to debugger: Broken pipe 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383): FATAL EXCEPTION: main 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.rlee.randomquotes/com.rlee.randomquotes.RandomQuotes}: java.lang.NullPointerException 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at android.os.Looper.loop(Looper.java:130) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at java.lang.reflect.Method.invoke(Method.java:507) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at dalvik.system.NativeStart.main(Native Method) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383): Caused by: java.lang.NullPointerException 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at com.rlee.randomquotes.RandomQuotes.onCreate(RandomQuotes.java:23) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  ... 11 more 
08-03 21:17:59.915: ERROR/power(852): Failed setting last user activity: g_error=0 

這是從logcat的正確的堆棧跟蹤?錯誤在哪裏?

下面的main.xml和string.xml是按鈕初始化的地方。我說得對嗎?希望這個html會顯示。

<!-- language-all: lang-html --> <br/><pre><Button 
android:id="@+id/go" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/press" 
/> 
<Button 
android:id="@+id/genRan" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/genRan" 
/></pre> <br/><pre> <string name="press">Press Me!</string> 
<string name="genRan">Generate Random Quote!</string> 
+0

我也有過這樣亂報價教程,但它顯示空指針異常對仿真器按鈕點擊敬酒,如果你想學習數據庫,這是一個更好的教程http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/ –

+0

謝謝,我會做的。但是我會堅持去找出爲什麼我會得到這些錯誤,我喜歡下面的aweser,我會嘗試並更新。 – iSimply

回答

0

確保您的get和set按鈕不爲空。你確定這些是你的按鈕的ID嗎?如果這些都是您的項目,請嘗試Clean,甚至可能會手動刪除R.java生成的文件,因爲有時它會變得混亂。

+1

是的我以前有過同樣的問題。在嘗試setOnClicks之前,先瀏覽代碼並確保這兩個按鈕都不是空的。 – Jack

+0

非常有見地。我會看看,謝謝。 – iSimply

+0

我真的覺得乾淨必須ahve工作,我沒有得到刪除R.java,但由於某些原因,它現在的工作。很想理解這一點。 – iSimply

0

是的,這是來自Logcat的正確StackTrace。最有用的信息是在這些線路

08-03 21:08:37.795: ERROR/AndroidRuntime(2383): Caused by: java.lang.NullPointerException 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at com.rlee.randomquotes.RandomQuotes.onCreate(RandomQuotes.java:23) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
08-03 21:08:37.795: ERROR/AndroidRuntime(2383):  ... 11 more 

的logcat的發現是告訴你,你有你的RandomQuotes班線23 NullPointerExcepotion。這是第23行從您的代碼

setButton.setOnClickListener(mAddListener); 

既然你得到一個NullPointerException,setButton必須爲空在這一點上,這意味着它是不正確的Button setButton = (Button)findViewById(R.id.go);初始化。

檢查您的main.xml文件,以確保您已聲明與屬性android:id="@+id/go"的按鈕。

+0

聽起來不錯,只需編輯您的原始問題以包含XML。這將讓你正確地格式化一切。 – theisenp

+0

謝謝,明白了。下次我會花更多的時間閱讀,而不是很激動的回覆。 – iSimply

0

我注意到u有將對DBAdapter在你的類被初始化。我面對使用數據庫適配器時,類似的空指針異常發出幾天就回來。

基本上,問題可能是數據庫要麼在活動生命週期的某個地方關閉,要麼沒有正確打開 - 在DBAdapter試圖訪問此活動中的數據庫之前。

所以我的建議是 - 在您的應用程序中註釋掉任何數據庫的close()調用並檢查數據庫是否打開&在DBAdapter嘗試訪問它並嘗試再次運行之前初始化。

在我的情況下,問題是,在我的呼喚活動Activity.OnStop()函數,我已經把在調用close()數據庫。現在,調用OnStop()每當活動是由一個子活動隱藏被調用,所以當我在子活動將對DBAdapter試圖訪問數據庫,它擊中了一個錯誤&引發空指針異常

這裏的活動生命週期-http://developer.android.com/guide/topics/fundamentals/activities。HTML#生命週期

+0

最奇怪的事情發生了。第一次我已經註釋掉setButtonListeners,我停止了讓所有的錯誤,然後我看了你的帖子,所以我註釋掉將對DBAdapter,它仍然工作,然後我記得我曾評論了聽衆,所以我註釋掉他們來說,這仍然工作,所以我取消了DBAdapter的註釋,它仍然有效,沒有錯誤。很奇怪。但是我會密切關注數據庫關閉調用。聽起來非常合理,它可能是最重要的。 – iSimply

+0

嗯,這是很荒謬的,現在,我註釋掉了整個教程,並設置它去,而這一切的作品。我只能猜測,乾淨的我沒有更新R.java類。但除此之外,沒有什麼改變。任何人有任何想法? – iSimply