gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:07:49 25 4
gpt4 key购买 nike

<分区>

希望我没有漏掉一些愚蠢的东西。我能够创建一个单个 EditText 并将其添加到LinearLayout;但是,当我尝试对 EditTexts 的 array 执行此操作时,我在 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 函数只是为了能看到它。那不是它实际所在的位置。我的最终目标是生成垂直 EditText 的 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) 

请问我遗漏了什么或做错了什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com