无法使用参数以编程方式创建多个EditText

时间:2018-02-08 04:38:51

标签: java android arrays android-edittext android-layoutparams

好的,希望我不会错过一些愚蠢的东西。我能够创建单个 EditText并将其添加到LinearLayout;但是,当我尝试对EditTexts的数组时,我在for循环中的第一行上得到一个NPE。这是工作代码:

LinearLayout p2player1 = (LinearLayout)findViewById(R.id.p2player1);
    EditText p1 = new EditText(this);
    LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(200,LinearLayout.LayoutParams.WRAP_CONTENT);
    p1.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    p1.setInputType(InputType.TYPE_CLASS_NUMBER);
    p1.setId(View.generateViewId());
    p1.setLayoutParams(editParams);
    p2player1.addView(p1,editParams);

一切都很好。

这是数组代码:

LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(200,LinearLayout.LayoutParams.WRAP_CONTENT);
    EditText[] play1 = new EditText[20];
    editParams.setMargins(184,0,0,0);
    for (int i=0;i<21;i++) {
        play1[i].setLayoutParams(editParams);
        play1[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        play1[i].setId(View.generateViewId());
        play1[i].setInputType(InputType.TYPE_CLASS_NUMBER);
        play1[i].setHint("0000");
        if (i>2 && IsOdd(i)) {
            play1[i].setFocusable(false);
        }

        p2player1.addView(play1[i],editParams);
    }
public boolean IsOdd(int n) {
    if ((n % 2) == 0) {
        return false;
    } else { return true; }
}

我包含了IsOdd函数,因此可以看到它。那不是它的实际位置。我的终极目标是生成VerticalLayout的Vertical EditTexts。好像很容易,所以我确定我很蠢,而且很容易丢失一些东西。

这是NPE:

Process: com.dyna.ks.scorekeeper, PID: 5439
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dyna.ks.scorekeeper/com.dyna.ks.scorekeeper.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                                                                       at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
                                                                       at com.dyna.ks.scorekeeper.MainActivity.setupTwoPlayer(MainActivity.java:105)
                                                                       at com.dyna.ks.scorekeeper.MainActivity.onCreate(MainActivity.java:62)
                                                                       at android.app.Activity.performCreate(Activity.java:6955)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
                                                                       at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                       at android.os.Looper.loop(Looper.java:154) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6776) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

请问我错过了什么或做错了什么?

1 个答案:

答案 0 :(得分:1)

这是因为你没有初始化所有的editText。

以下是声明。

EditText[] play1 = new EditText[20];

您需要将每个edittext初始化为play1[i] = new EditText(this);循环。请参阅下面的代码,它将正常工作

for (int i=0;i<21;i++) {
        play1[i] = new EditText(this);
        play1[i].setLayoutParams(editParams);
        play1[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        play1[i].setId(View.generateViewId());
        play1[i].setInputType(InputType.TYPE_CLASS_NUMBER);
        play1[i].setHint("0000");
        if (i>2 && IsOdd(i)) {
            play1[i].setFocusable(false);
        }

        p2player1.addView(play1[i],editParams);
    }