简体   繁体   中英

Android Create Dynamic Array Button, but application crash when call the function

Button[] myButtons = new Button[9];
    public void CreateButton(String OrderStr){
        TableLayout tablelayout = (TableLayout) findViewById(R.id.widget101);

        // Create a LinearLayout element
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        for(int i=0; i<=5; i++){

        final int ButtonNum = i; //use on inside other function
        // Add Buttons

        myButtons[i].setText("BUS STATION");
        myButtons[i].setLayoutParams(new LayoutParams(160, 128));
        myButtons[i].setTextSize(TypedValue.COMPLEX_UNIT_SP, 25); 
        myButtons[i].setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View arg0) {
                Toast.makeText(getApplicationContext(), "Long Clicked " ,
                      Toast.LENGTH_SHORT).show();
                myButtons[ButtonNum].getBackground().setColorFilter(0xff3399ff, PorterDuff.Mode.MULTIPLY);
                return true;    // <- set to true
            }
        });

        myButtons[i].setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myButtons[ButtonNum].getBackground().setColorFilter(0xffff9999, PorterDuff.Mode.MULTIPLY);
            }
        });

        linearLayout.addView(myButtons[i]);

        }
        // Add the LinearLayout element to the ScrollView
        tablelayout.addView(linearLayout);
    }

Create Dynamic Array Button, but application crash when call the function

this is the Log Cat

12-01 16:04:59.378: E/AndroidRuntime(3066): FATAL EXCEPTION: main
12-01 16:04:59.378: E/AndroidRuntime(3066): java.lang.NullPointerException
12-01 16:04:59.378: E/AndroidRuntime(3066):     at com.example.abc2.MainActivity.CreateButton(MainActivity.java:103)
12-01 16:04:59.378: E/AndroidRuntime(3066):     at com.example.abc2.MainActivity$1.onClick(MainActivity.java:69)
12-01 16:04:59.378: E/AndroidRuntime(3066):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
12-01 16:04:59.378: E/AndroidRuntime(3066):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 16:04:59.378: E/AndroidRuntime(3066):     at android.os.Looper.loop(Looper.java:123)
12-01 16:04:59.378: E/AndroidRuntime(3066):     at android.app.ActivityThread.main(ActivityThread.java:3647)
12-01 16:04:59.378: E/AndroidRuntime(3066):     at java.lang.reflect.Method.invokeNative(Native Method)
12-01 16:04:59.378: E/AndroidRuntime(3066):     at java.lang.reflect.Method.invoke(Method.java:507)
12-01 16:04:59.378: E/AndroidRuntime(3066):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-01 16:04:59.378: E/AndroidRuntime(3066):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-01 16:04:59.378: E/AndroidRuntime(3066):     at dalvik.system.NativeStart.main(Native Method)

在funtcion中添加此代码后解决的问题

myButtons[i] = new Button(this); 

NullPointerException fires when you try to use null as an object eg:

 null.setText("BUS STATION"); 

In your case this exception fires That is because you've created Array of null reference of type Button

in this line

final Button myButtons[] = new Button[9]; 

you actually have an array object referenced by myButtons that contains 9 indexes of null references

myButtons[i] is null where 0<=i <9

you must force myButton[i] to refere to a Button Object

myButton[i]=new Button(this);// this refers to current context

after line final Button myButtons[] = new Button[9];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM