2012-09-17 17 views
0

這是我的動態添加按鈕的代碼。運行時拋出錯誤。任何想法? :)這是怎麼回事?它的投擲錯誤

DatabaseHandler db; 
private RelativeLayout relativeLayout; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_settings); 
    boolean userCounts = db.getUserExistance(); 
    if(userCounts == false){    
     Button button= new Button(this); 
     button.setText("Add password"); 
     relativeLayout.addView(button); 
    } 
    else if(userCounts == true){    
     Button button2 = new Button(this); 
     button2.setText("Change password"); 
     relativeLayout.addView(button2);    
    } 

} 

錯誤日誌:

09-17 11:44:34.658: D/AndroidRuntime(655): Shutting down VM 09-17 11:44:34.658: W/dalvikvm(655): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
09-17 11:44:34.699: E/AndroidRuntime(655): FATAL EXCEPTION: main 
09-17 11:44:34.699: E/AndroidRuntime(655): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.projects.myworldsafe/com.projects.myworldsafe.Settings}: java.lang.NullPointerException 
09-17 11:44:34.699: E/AndroidRuntime(655): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
+2

有問題:) – Lucifer

+2

上傳錯誤日誌約張貼堆棧跟蹤什麼?我們的傳感器不幸正在度假。 –

+0

09-17 11:44:34.658:D/AndroidRuntime(655):關閉VM 09-17 11:44:34.658:W/dalvikvm(655):threadid = 1:線程退出時未捕獲的異常(group = 0x40a13300 ) 09-17 11:44:34.699:E/AndroidRuntime(655):致命異常:主 09-17 11:44:34.699:E/AndroidRuntime(655):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.projects.myworldsafe/com.projects.myworldsafe.Settings}:java.lang.NullPointerException 09-17 11:44:34.699:E/AndroidRuntime(655):\t at android.app.ActivityThread.performLaunchActivity(ActivityThread。 java:2059) –

回答

3

您還沒有初始化relativeLayout。如果要設置relativeLayout編程嘗試:

relativeLayout = new RelativeLayout(this); 

如果它在你的佈局文件首先你需要找到它,如:

relativeLayout = (RelativeLayout) this.findViewById(R.id.your_layout_id); 

DatabaseHandler db

2

同樣的事情,我想你在第一次使用之前忘記初始化dbrelativeLayout

+0

當初始化數據庫類時就解決了這個問題:) DatabaseHandler db = new DatabaseHandler(this);(DatabaseHandler db; --old)??? –

0

據我,我想通是你需要給佈局爲Relative layout

RelativeLayout = (RelativeLayout) this.findViewById(R.id.relativeID); 
0

試試這個,

DatabaseHandler db; 
    private RelativeLayout relativeLayout; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 
     boolean userCounts = db.getUserExistance(); 
     relativeLayout = (RelativeLayout) this.findViewById(R.id.myRelativeLayout); 
     if(userCounts == false){    
      Button button= new Button(this); 
      button.setText("Add password"); 
      relativeLayout.addView(button); 
     } 
     else if(userCounts == true){    
      Button button2 = new Button(this); 
      button2.setText("Change password"); 
      relativeLayout.addView(button2);    
     } 

    } 
+0

應用顯示「不幸關閉」對話框 –

+0

您是否在佈局xml中使用了相對佈局,或者您是通過活動中的代碼創建它的? –

+0

如果以編程方式添加,則使用_relativeLayout = new RelativeLayout(this); _替換_relativeLayout =(RelativeLayout)this.findViewById(R.id.myRelativeLayout); _。 –