在for循环内动态创建用于滚动视图的按钮 [英] Creating Buttons Dynamically inside a for loop for use in a scrollview

查看:88
本文介绍了在for循环内动态创建用于滚动视图的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在动态创建按钮方面遇到问题。我使用了提供的帮助,如何在其中动态创建按钮Android吗?
虽然确实有很大帮助,但它并不能完全适合我的具体情况。我试图在滚动视图内创建按钮数组。这些按钮基本上是基于sqlite数据库中查询的答案而动态创建的。我还没有实现数据库,但是我只是使用带有set变量的for循环来创建按钮。当代码在这一点上运行时,我收到一个空指针异常...。

I have an issue with creating buttons dynamically. I used the help provided by How can I dynamically create a button in Android? Although is does help tremendously it isn't exactly working with my specific situation. I am trying to create an array of buttons inside of a scroll view. These buttons will basically be created on the fly based off the answer to the query from a sqlite database. I havent implemented the database as of yet but im just using a for loop with a set variable to create the buttons. I am recieving a Null Pointer Exception when the code ran at this point....

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

这是我一直为此项目使用的代码。

Here is the code I have been working with for this project.

        @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


推荐答案

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

You are initializing an array to store your buttons in. You still need to initialize each individual button within that array.

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

这篇关于在for循环内动态创建用于滚动视图的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆