gpt4 book ai didi

android - 在 for 循环中动态创建按钮以在 ScrollView 中使用

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:52 25 4
gpt4 key购买 nike

我在动态创建按钮时遇到问题。我使用了 How can I dynamically create a button in Android? 提供的帮助尽管确实有很大帮助,但它并不完全适合我的具体情况。我正在尝试在 ScrollView 中创建一个按钮数组。这些按钮基本上是根据来自 sqlite 数据库的查询答案动态创建的。我还没有实现数据库,但我只是使用带有设置变量的 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

最佳答案

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

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

关于android - 在 for 循环中动态创建按钮以在 ScrollView 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19142764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com