2013-04-13 95 views
0

我必須以編程方式將按鈕添加到線性佈局。但程序部隊關閉。這裏是代碼我想:將按鈕添加到LinearLayout時出錯

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_remove_contact); 

     LinearLayout ll = (LinearLayout) findViewById(R.id.vertic); 
     Button button = new Button(this.getApplicationContext()); 
     LinearLayout.LayoutParams params = (LayoutParams) button.getLayoutParams(); 
     params.weight = 1; 
     button.setText("MR. ABC"); 
     button.setLayoutParams(params); 
     ll.addView(button, params); 

    } 

這裏是logcat的:

04-13 22:16:44.484: D/dalvikvm(12596): GC_EXTERNAL_ALLOC freed 70K, 50% free 2698K/5379K, external 2013K/2108K, paused 53ms 
04-13 22:16:46.000: D/AndroidRuntime(12596): Shutting down VM 
04-13 22:16:46.000: W/dalvikvm(12596): threadid=1: thread exiting with uncaught exception (group=0x40015578) 
04-13 22:16:46.011: E/AndroidRuntime(12596): FATAL EXCEPTION: main 
04-13 22:16:46.011: E/AndroidRuntime(12596): java.lang.RuntimeException: Unable to start activity ComponentInfo{maju.project.tourguide/maju.project.tourguide.RemoveContact}: java.lang.NullPointerException 
04-13 22:16:46.011: E/AndroidRuntime(12596): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at android.os.Looper.loop(Looper.java:123) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at android.app.ActivityThread.main(ActivityThread.java:3687) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at java.lang.reflect.Method.invokeNative(Native Method) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at java.lang.reflect.Method.invoke(Method.java:507) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at dalvik.system.NativeStart.main(Native Method) 
04-13 22:16:46.011: E/AndroidRuntime(12596): Caused by: java.lang.NullPointerException 
04-13 22:16:46.011: E/AndroidRuntime(12596): at maju.project.tourguide.RemoveContact.onCreate(RemoveContact.java:31) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-13 22:16:46.011: E/AndroidRuntime(12596): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
04-13 22:16:46.011: E/AndroidRuntime(12596): ... 11 more 

任何人都可以請註明什麼問題?在此先感謝

下面是XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/car_wallpaper" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:id="@+id/vertic" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Your Emergency Contacts" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#FFFF" /> 

</LinearLayout> 
+2

什麼是31行? – codeMagic

+1

'activity_remove_contact.xml'是否有一個LinearLayout,其id爲'vertic'? – Sam

+0

把'activity_remove_contact.xml'放在這裏,這樣我們就可以看到它了:)還有,你試過用'this'改變'this.getApplicationContext()'? – Andres

回答

3

在這裏:

Button button = new Button(this.getApplicationContext()); //<<< 

你逝去的Application上下文創建動態按鈕是無效的情況下,因爲您要添加按鈕到目前Activity佈局,所以你將需要通過當前Activity上下文創建按鈕。改變你的代碼:

 Button button = new Button(this); 
    LinearLayout.LayoutParams params = 
      new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
             LinearLayout.LayoutParams.WRAP_CONTENT); 
    params.weight = 1; 
    button.setText("MR. ABC"); 
    button.setLayoutParams(params); 
    .... 

OR

Button button = new Button(Your_Current_Activity.this); 
..... 

,並且確保你有一個佈局vertic ID在當前活動的佈局,你逝去的setContentView

+0

現在它解決了這個問題。謝謝! –

0

原因NullPointerException異常是由於此行:

LinearLayout.LayoutParams params = (LayoutParams) button.getLayoutParams(); 

在這裏你得到params = null,因此在下一行你會得到異常。

如果你想在你的按鈕中使用LayoutParams,那麼你首先需要初始化它。這是你修改的代碼:

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_remove_contact); 

    LinearLayout ll = (LinearLayout) findViewById(R.id.vertic); 
    Button button = new Button(this.getApplicationContext()); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    params.weight = 1; 
    button.setText("MR. ABC"); 
    button.setLayoutParams(params); 
    ll.addView(button, params); 
}