在for循环中动态创建按钮以在scrollview中使用

时间:2013-10-02 17:11:51

标签: android button dynamic nullpointerexception scrollview

我有动态创建按钮的问题。我使用了How can I dynamically create a button in Android?提供的帮助 虽然确实有很大帮助,但它并不完全符合我的具体情况。我试图在滚动视图内创建一个按钮数组。这些按钮基本上是基于sqlite数据库中查询的答案而动态创建的。我还没有实现数据库,但我只是使用带有set变量的for循环来创建按钮。当代码在此时运行时,我正在接收Null Pointer Exception ....

    myButton[index].setText("Button # ");

以下是我一直在为这个项目工作的代码。

        @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_aquarium);

    //test creating of dynamic buttons
    Button[] myButton = new Button[4];
    LinearLayout scrViewButLay = (LinearLayout) findViewById(R.id.scrollViewLinLay);
    for(int index = 0; index < 4; index++){
        Log.i("ForTag", "Inside for loop");
        Log.i("ForTag", "button length is "+myButton.length);
        myButton[index].setText("Button # ");//null ptr exception error
        Log.i("ForTag", "After set text");
        scrViewButLay.addView(myButton[index]);
        Log.i("ForTag", "After adding to view");
    }
}

这是空指针异常错误

    10-02 12:07:11.373: D/ActivityThread(16944): setTargetHeapUtilization:0.25
    10-02 12:07:11.373: D/ActivityThread(16944): setTargetHeapIdealFree:8388608
    10-02 12:07:11.373: D/ActivityThread(16944): setTargetHeapConcurrentStart:2097152
    10-02 12:07:11.443: I/ForTag(16944): Inside for loop
    10-02 12:07:11.443: I/ForTag(16944): button length is 4
    10-02 12:07:11.443: W/dalvikvm(16944): threadid=1: thread exiting with uncaught exception         (group=0x41c5a438)
    10-02 12:07:11.443: E/AndroidRuntime(16944): FATAL EXCEPTION: main
    10-02 12:07:11.443: E/AndroidRuntime(16944): java.lang.RuntimeException: Unable to start         activity         ComponentInfo{com.aquariumdatabase.seanrsolution/com.aquariumdatabase.seanrsolution.MainAquariumActiv        ity}: java.lang.NullPointerException
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2098)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.ActivityThread.access$600(ActivityThread.java:138)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.os.Handler.dispatchMessage(Handler.java:99)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at android.os.Looper.loop(Looper.java:137)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.ActivityThread.main(ActivityThread.java:4905)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         java.lang.reflect.Method.invokeNative(Native Method)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         java.lang.reflect.Method.invoke(Method.java:511)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at dalvik.system.NativeStart.main(Native         Method)
    10-02 12:07:11.443: E/AndroidRuntime(16944): Caused by: java.lang.NullPointerException
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         com.aquariumdatabase.seanrsolution.MainAquariumActivity.onCreate(MainAquariumActivity.java:23)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.Activity.performCreate(Activity.java:5240)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
    10-02 12:07:11.443: E/AndroidRuntime(16944):    ... 11 more

1 个答案:

答案 0 :(得分:2)

您正在初始化一个数组以存储您的按钮。您仍然需要初始化该数组中的每个按钮。

for(int index = 0; index < 4; index++) {
    myButton[index] = new Button(this); //initialize the button here
    myButton[index].setText("Button # "); 
}