从OnCreate中的另一个类添加数组

时间:2018-10-23 21:12:15

标签: java android

需要通过Oncreate方法将Buttons []数组添加到Linearlayout中

我尝试输入“ for”,但出现错误。

这就是我能做的

班级按钮

public class ButtonFace {
 public Button[] numButtonFace;
 public void setNumButtonFace(){
 numButtonFace = new Button[10];
 String[] buttonFace = new String[10];
 buttonFace[0] = "A";
 buttonFace[1] = "B";
 buttonFace[2] = "H";
 buttonFace[3] = "F";
 buttonFace[4] = "C";
 buttonFace[5] = "E";
 buttonFace[6] = "D";
 buttonFace[7] = "S";
 buttonFace[8] = "W";
 buttonFace[9] = "Q";
 for (int i =0; i<numButtonFace.length; i++){
         numButtonFace[i].setText(buttonFace[i]);
    }
}

OnCreate

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout top = (LinearLayout) findViewById(R.id.top);
    ButtonFace face = new ButtonFace();
            for (int i=0; i<10;i++){
        top.addView(face.numButtonFace[i]);
    }

}

错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.vagin.myapplication, PID: 3587
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vagin.myapplication/com.example.vagin.myapplication.MainActivity}: java.lang.NullPointerException
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
                      at android.app.ActivityThread.access$800(ActivityThread.java:135)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:136)
                      at android.app.ActivityThread.main(ActivityThread.java:5019)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                      at dalvik.system.NativeStart.main(Native Method)
                   Caused by: java.lang.NullPointerException
                      at setButton.ButtonFace.setNumButtonFace(ButtonFace.java:25)
                      at com.example.vagin.myapplication.MainActivity.onCreate(MainActivity.java:28)
                      at android.app.Activity.performCreate(Activity.java:5231)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:136) 
                      at android.app.ActivityThread.main(ActivityThread.java:5019) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:515) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                      at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

尝试此代码

while (i < 10) {
    face.numButtonFace[i] = new Button(this);
    face.numButtonFace[i].setText(face.but[i]);
}

答案 1 :(得分:0)

您只需要在ButtonFace中声明数组:

public class ButtonFace {

 // This only declare the variable
 public Button[] numButtonFace;

 public void setNumButtonFace() {
   // You're only initialize the value for the first time here
   numButtonFace = new Button[10];
   String[] buttonFace = new String[10];

  ...
}

但是您尝试使用以下方法访问onCreate内的numButtonFace项:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout top = (LinearLayout) findViewById(R.id.top);
    ButtonFace face = new ButtonFace();

    for (int i=0; i<10;i++){
        top.addView(face.numButtonFace[i]);
        //                  ^
        //     You're trying to access the item of non-exist variable
    }

}

因此,在实例化对象时,更改ButtonFace以初始化numButtonFace的大小:

public class ButtonFace {

 // This only declare the variable
 public Button[] numButtonFace;

 // initialize the size
 numButtonFace = new Button[10];

 public void setNumButtonFace() {
   ...

   String[] buttonFace = new String[10];

  ...
}

然后,您需要修复以下代码:

for (int i =0; i<numButtonFace.length; i++){
         numButtonFace[i].setText(buttonFace[i]);
}

因为设置文本时需要首先创建按钮。您可以使用以下方法修复它:

for (int i =0; i<numButtonFace.length; i++){
  numButtonFace[i] = new Button(context);
  numButtonFace[i].setText(buttonFace[i]);
}

由于ButtonFace中没有上下文,因此需要对其进行修改以将上下文用作其构造函数参数:

public class ButtonFace {

 // This only declare the variable
 public Button[] numButtonFace;

 // initialize the size
 numButtonFace = new Button[10];

 private Context context;

 public ButtonFace(Context context) {
   this.context = context;
 }

 public void setNumButtonFace() {
   ...

   String[] buttonFace = new String[10];

   ...
  for (int i =0; i<numButtonFace.length; i++){
     numButtonFace[i] = new Button(context);
     numButtonFace[i].setText(buttonFace[i]);
  }
}

现在,您可以像这样使用ButtonFace:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout top = (LinearLayout) findViewById(R.id.top);

    // Create ButtonFace with a context
    ButtonFace face = new ButtonFace(this);

    for (int i=0; i< face.numButtonFace.length; i++){
        top.addView(face.numButtonFace[i]);
    }

}