This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                                去年关闭。
            
                    
希望我不会错过任何愚蠢的事情。我能够创建一个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函数,以便可以看到它。那实际上不是那里。我的最终目标是生成垂直EditTexts的LinearLayout。似乎这很容易,所以我确定我很愚蠢,并且很容易丢失一些东西。

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


请问我在想什么或做错了什么?

最佳答案

这是因为您没有初始化所有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);
    }

09-25 21:57