Android创建动态数组按钮,但调用该函数时应用程序崩溃

时间:2013-12-01 08:06:05

标签: java android eclipse

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);
    }

创建动态数组按钮,但调用函数时应用程序崩溃

这是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)

2 个答案:

答案 0 :(得分:0)

在函数

中添加此代码后解决了问题
myButtons[i] = new Button(this); 

答案 1 :(得分:0)

当您尝试将null用作对象时,

NullPointerException会触发,例如:

 null.setText("BUS STATION"); 

在你的情况下,这个异常触发那是因为你创建了类型为Button

的null引用数组

在这一行

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

你实际上有一个由myButtons引用的数组对象,其中包含9个null个引用的索引

myButtons[i]null,其中0 <= i <9

您必须强制myButton[i]引用Button对象

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

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

之后