2012-11-20 71 views
0

我創建了一個爲我的LinearLayout,但得到空指針異常。 我不明白什麼去了,因爲我沒有收到任何語法錯誤。Android LinearLayout null例外

這裏是我的java代碼

private OnClickListener onClick() { 
     return new OnClickListener() {   
      public void onClick(View v) { 
      System.out.println("layout 1"); 
      newBtnLayout(); 
      System.out.println("layout 2"); 
      System.out.println(newBtnLayout()); 
      nBtnLayoutList = (LinearLayout)findViewById 
      (R.id.newBtnLayoutList); 
      System.out.println("layout 3"); 
      nEntriesButton = (Button) findViewById(R.id.newEntriesBtn); 
      System.out.println("layout 4"); 
      System.out.println(nBtnLayoutList); 
      nBtnLayoutList.addView(createBtnAddEntries());    
      System.out.println("layout 5");    
      } 
     }; 
    } 

private LinearLayout newBtnLayout(){ 
LinearLayout nBtnLayout = new LinearLayout(this); 
final LayoutParams lparams = new LayoutParams 
(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
nBtnLayout.setOrientation(LinearLayout.VERTICAL); 
nBtnLayout.setLayoutParams(lparams); 
nBtnLayout.setWeightSum(100); 
nBtnLayout.setId(R.id.newBtnLayoutList); 
    return nBtnLayout; 
} 


private Button createBtnAddEntries(){ 
    System.out.println("button 1"); 
    final LayoutParams lparams = new LayoutParams 
    (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, 30); 
    final Button addEntiresBtn = new Button(this); 
    addEntiresBtn.setLayoutParams(lparams); 
    addEntiresBtn.setText("Add Entries"); 
    addEntiresBtn.setId(R.id.newEntriesBtn);   
     return addEntiresBtn; 
}   

這是Logcat輸出

11-20 10:02:02.295: I/System.out(1077): layout 0  
11-20 10:02:02.305: I/System.out(1077): layout 1  
11-20 10:02:02.305: I/System.out(1077): layout 2  
11-20 10:02:02.305: I/System.out(1077): [email protected]  
11-20 10:02:02.315: I/System.out(1077): layout 3  
11-20 10:02:02.346: I/System.out(1077): layout 4  
11-20 10:02:02.346: I/System.out(1077): null  
11-20 10:02:02.346: I/System.out(1077): button 1  
11-20 10:02:02.356: I/System.out(1077): button 2  
11-20 10:02:02.356: I/System.out(1077): button 3  
11-20 10:02:02.365: I/System.out(1077): button 4  
11-20 10:02:02.365: I/System.out(1077): button 5  
11-20 10:02:02.365: I/System.out(1077): button 6  
11-20 10:02:02.365: D/AndroidRuntime(1077): Shutting down VM  
11-20 10:02:02.365: W/dalvikvm(1077): threadid=1: thread exiting with uncaught exception (group=0x40a13300)  
11-20 10:02:02.375: E/AndroidRuntime(1077): FATAL EXCEPTION: main  
11-20 10:02:02.375: E/AndroidRuntime(1077): java.lang.NullPointerException  
11-20 10:02:02.375: E/AndroidRuntime(1077):  at com.daryl.jcu_project1_creditscoreform.Credit_score_form$1.onClick(Credit_score_form.java:66)  
11-20 10:02:02.375: E/AndroidRuntime(1077):  at android.view.View.performClick(View.java:4084)  
11-20 10:02:02.375: E/AndroidRuntime(1077):  at android.view.View$PerformClick.run(View.java:16966)  
11-20 10:02:02.375: E/AndroidRuntime(1077):  at android.os.Handler.handleCallback(Handler.java:615)  
11-20 10:02:02.375: E/AndroidRuntime(1077):  at android.os.Handler.dispatchMessage(Handler.java:92)  
11-20 10:02:02.375: E/AndroidRuntime(1077):  at android.os.Looper.loop(Looper.java:137)  
11-20 10:02:02.375: E/AndroidRuntime(1077):  at android.app.ActivityThread.main(ActivityThread.java:4745)  
11-20 10:02:02.375: E/AndroidRuntime(1077):  at java.lang.reflect.Method.invokeNative(Native Method)  
11-20 10:02:02.375: E/AndroidRuntime(1077):  at java.lang.reflect.Method.invoke(Method.java:511)  
11-20 10:02:02.375: E/AndroidRuntime(1077):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)  
11-20 10:02:02.375: E/AndroidRuntime(1077):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)  
11-20 10:02:02.375: E/AndroidRuntime(1077):  at dalvik.system.NativeStart.main(Native Method)  
11-20 10:07:02.445: I/Process(1077): Sending signal. PID: 1077 SIG: 9 
+0

哪裏是'的setContentView(R.layout.main)創建;'? – user370305

+0

恩我在哪裏添加到? – Daryl

+0

在調用'onClick()'之前,在你的Activity的'onCreate()'中。 – user370305

回答

1

明顯NullPointerException異常。請檢查您的activity_credit_score_from.xml是否實際上包含具有該ID的佈局。如果一切正常,請清理您的項目(Project => Clean)。 Eclipse是一個糟糕的IDE,喜歡受苦的開發人員。

+0

我創建了另一個XML文件來聲明ID,而不是在activity_credit_score_from.xml中。因爲即時通訊無法使用標籤聲明新ID – Daryl

+0

請問您可以發佈activity_credit_score_from.xml的內容嗎?好像有人使用@id而不是@ + id ;-) – razielsarafan

0

沒有在activity_credit_score_from.xml

沒有@id這是我在IDs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

     <item 
      name="newBtnLayoutList" 
      type="id"/> 
     <item 
      name="newEntriesBtn" 
      type="id"/> 
    </resources> 
+0

無論如何,請發佈您的activity_credit_score_from.xml – razielsarafan