4

我在动态创建按钮时遇到问题。我使用了如何在 Android 中动态创建按钮? 尽管确实有很大帮助,但它并不完全适合我的具体情况。我正在尝试在滚动视图中创建一组按钮。这些按钮基本上是根据来自 sqlite 数据库的查询的答案动态创建的。到目前为止,我还没有实现数据库,但我只是使用带有 set 变量的 for 循环来创建按钮。当代码此时运行时,我收到空指针异常......

    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
4

1 回答 1

2

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

for(int index = 0; index < 4; index++) {
    myButton[index] = new Button(this); //initialize the button here
    myButton[index].setText("Button # "); 
}
于 2013-10-02T17:20:35.190 回答