我正在尝试为 Android 开发一个记分板应用程序。我已经在我的 XML 布局中定义了所有元素(例如 TextFields、按钮等),这些元素在默认情况下是隐藏的,并在提示用户提供玩家数量时设置为“可见”。我的问题如下:
当用户输入高于 4 时,findViewById 方法会抛出 NullPointer 异常,即使找到它所指向的 View 并在较低输入下正常返回。这是代码示例,其中出现问题以及之前的 onCreate() 方法。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_rssitem_detail);
//Stop rotation screen
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
container = (LinearLayout) findViewById(R.id.container);
// Write everything here inside the container.
DialogFragment newFragment = new StartDialogFragment();
newFragment.show(getSupportFragmentManager(), "startDialog");
}
此方法随后将 if(...) 检查 num>0 、 num>1 等,直到 num>7。findViewById(R.id.player1).setVisibility(1) ;
如果输入为 5 或更高,则会立即出现问题。注意:它适用于所有较低的输入。
public void createPlayers(int num){
if(num>0) {
findViewById(R.id.player1).setVisibility(View.VISIBLE) ;
p1= ((Button)findViewById(R.id.Plus1));
p1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView t= ((TextView) findViewById(R.id.score1));
scoreUp(t);
}
});
m1= ((Button)findViewById(R.id.Minus1));
m1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView t= ((TextView) findViewById(R.id.score1));
scoreDown(t);
}
});
}
这是我的 XML 布局示例。这是ONE player的元素部分。这段代码实际上是重复的,以创建另外 7 个。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/player"
>
<LinearLayout
android:id="@+id/player1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="invisible"
>
<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/player1"
android:layout_weight="0.5"
android:background="@android:color/transparent"
android:textStyle="bold"
android:textSize="14pt"
android:typeface="sans"
android:textColor="@android:color/white"
android:shadowColor="#ffd803"
android:shadowRadius="15.0"
android:layout_gravity="left"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal"
>
<Button
android:id="@+id/Plus1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/plusbutton"
android:layout_weight=".125"
/>
<TextView
android:id="@+id/score1"
android:text="@string/score1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="center"
android:textStyle="bold"
android:textSize="14pt"
android:typeface="sans"
android:textColor="#36ff00"
android:shadowColor="#ffd803"
android:shadowRadius="15.0"
/>
<Button
android:id="@+id/Minus1"
android:text="@string/minusbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".125"
></Button>
</LinearLayout>
</LinearLayout>
这是 LogCat 输出文件:
07-13 18:31:42.913: D/View(26377): onTouchEvent: viewFlags: 0x18244001
07-13 18:31:42.913: D/View(26377): onTouchEvent: isFocusable: true, isFocusableInTouchMode: true, isFocused: true; focusTaken: false
07-13 18:31:45.856: D/View(26377): onTouchEvent: viewFlags: 0x18004001
07-13 18:31:45.856: D/View(26377): onTouchEvent: isFocusable: true, isFocusableInTouchMode: false, isFocused: false; focusTaken: false
07-13 18:31:45.876: I/System.out(26377): this is the given input: 5
07-13 18:31:45.926: D/AndroidRuntime(26377): Shutting down VM
07-13 18:31:45.926: W/dalvikvm(26377): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
07-13 18:31:45.926: E/AndroidRuntime(26377): FATAL EXCEPTION: main
07-13 18:31:45.926: E/AndroidRuntime(26377): java.lang.NullPointerException
07-13 18:31:45.926: E/AndroidRuntime(26377): at keke.koko.PooCounter2.createPlayers(PooCounter2.java:40)
07-13 18:31:45.926: E/AndroidRuntime(26377): at keke.koko.PooCounter2.onDialogPositiveClick(PooCounter2.java:246)
07-13 18:31:45.926: E/AndroidRuntime(26377): at keke.koko.StartDialogFragment$1.onClick(StartDialogFragment.java:28)
07-13 18:31:45.926: E/AndroidRuntime(26377): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
07-13 18:31:45.926: E/AndroidRuntime(26377): at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 18:31:45.926: E/AndroidRuntime(26377): at android.os.Looper.loop(Looper.java:150)
07-13 18:31:45.926: E/AndroidRuntime(26377): at android.app.ActivityThread.main(ActivityThread.java:4385)
07-13 18:31:45.926: E/AndroidRuntime(26377): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 18:31:45.926: E/AndroidRuntime(26377): at java.lang.reflect.Method.invoke(Method.java:507)
07-13 18:31:45.926: E/AndroidRuntime(26377): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
07-13 18:31:45.926: E/AndroidRuntime(26377): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
07-13 18:31:45.926: E/AndroidRuntime(26377): at dalvik.system.NativeStart.main(Native Method)