2013-02-13 129 views
0

我試圖實現一個dialog框以在單擊按鈕時出現。我跟着一個例子來得到下面的代碼,因爲我以前從來沒有用過dialogs。我在Results課程的2個獨立位置獲得NPE。我在兩個地點下面的代碼中都有評論。對話框上的NullPointerException - Android

非常感謝您的幫助。

Java代碼:

public class Results extends Activity { 

    Button detailsBtn; 
    final Context context = this; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.resultsmain); 

     detailsBtn = (Button)findViewById(R.id.detailsBtn); 
     detailsBtn.setText("Details"); 

     detailsBtn.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 

       final Dialog dialog = new Dialog(context); 
       dialog.setContentView(R.layout.resultsdetailsdisplay); 
       dialog.setTitle("Detailssss"); 

       TextView title = (TextView)findViewById(R.id.title); 
       title.setText("TITLE - TESTING"); //NULL POINTER EXCEPTION 

       Button close = (Button)findViewById(R.id.close); 

       close.setOnClickListener(new OnClickListener() { //NULL POINTER EXCEPTION 
        public void onClick(View arg0) { 
         dialog.dismiss(); 
        } 
       }); 
      } 
     }); 
    } 
} 

resultsdetailsdisplay.xml:

<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/scroll" > 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="12sp" 
     android:layout_centerHorizontal="true" 
     android:gravity="center" /> 
</RelativeLayout> 

logcat的輸出

02-13 18:36:39.900: E/AndroidRuntime(767): java.lang.NullPointerException 
02-13 18:36:39.900: E/AndroidRuntime(767): at matt.lyons.bibletrivia.Results$4.onClick(Results.java:85) 
02-13 18:36:39.900: E/AndroidRuntime(767): at android.view.View.performClick(View.java:4084) 
02-13 18:36:39.900: E/AndroidRuntime(767): at android.view.View$PerformClick.run(View.java:16966) 
02-13 18:36:39.900: E/AndroidRuntime(767): at android.os.Handler.handleCallback(Handler.java:615) 
02-13 18:36:39.900: E/AndroidRuntime(767): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-13 18:36:39.900: E/AndroidRuntime(767): at android.os.Looper.loop(Looper.java:137) 
02-13 18:36:39.900: E/AndroidRuntime(767): at android.app.ActivityThread.main(ActivityThread.java:4745) 
02-13 18:36:39.900: E/AndroidRuntime(767): at java.lang.reflect.Method.invokeNative(Native Method) 
02-13 18:36:39.900: E/AndroidRuntime(767): at java.lang.reflect.Method.invoke(Method.java:511) 
02-13 18:36:39.900: E/AndroidRuntime(767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
02-13 18:36:39.900: E/AndroidRuntime(767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
02-13 18:36:39.900: E/AndroidRuntime(767): at dalvik.system.NativeStart.main(Native Method) 
+1

包含異常堆棧以獲得更好的答案。 – Krylez 2013-02-13 23:24:09

+0

哦,是的,忘了。我編輯它。 – Matt 2013-02-13 23:38:39

回答

3

使用對話框對象:

TextView title = (TextView) dialog.findViewById(R.id.title); 

Button close = (Button) dialog.findViewById(R.id.close); 

作爲指定herefindViewById - 查找所識別通過從XML這是在處理的id屬性的圖的onCreate(捆綁)。

您的對話框正在加載,因此只有對話框上下文知道這些視圖。

+0

你能更具體嗎?我不明白你指示我做什麼。 – Matt 2013-02-13 23:36:02

+0

注意'對話框'。對象引用,而不是使用非限定調用findViewById我使用對話框對象,因爲它'知道'你的兩個視圖對象。 – ggenglish 2013-02-13 23:37:29

+0

哦,對不起,我以前看不到它! :)我實現了你的代碼,兩個NPE都不見了。謝謝!當按鈕被按下時沒有任何反應,但顯然這是一個不同的問題。再次感謝。 – Matt 2013-02-13 23:46:13

0

這兩個問題都是由相同的方法,findViewById(R.id.something)引起的。由於您嘗試引用外部課程的視圖(即OnClickListener),因此您無法直接使用findViewById。相反,你可以使用例如,

Results.this.findViewById(R.id.title); 

希望它的工作!

+0

相同的NPE出現在相同的行上。 – Matt 2013-02-13 23:44:02

1

TextView tv = (TextView)dialog.findViewById(R.id.title);

Button close = (Button)dialog.findViewById(R.id.close);

在這裏,在上面的XML你沒有創建button也。

+0

您能否詳細說明您的答案 – Freakyuser 2013-02-14 10:13:27

+0

resultsdetailsdisplay.xml不包含任何按鈕元素,並且在試圖訪問對話框的R.id.close的代碼中。得到它了? – PavanBhushan 2013-02-14 10:40:36