我有一个显示游戏屏幕的 Activity 。 UI 元素之一是分数框。分数框基本上是一个四行的表格布局。所有行都是相同的,这里是代表一行的 xml。
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/farkle_image"
android:src="@drawable/icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/name_text"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Name" />
<TextView
android:id="@+id/temp_score_text"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Temp Score" />
<TextView
android:id="@+id/score_text"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Score" />
</TableRow>
每一行都有 imageview 、一个代表玩家姓名的 textview 和另外两个代表不同分数的 textview 。
这是我在 Activity 使用的布局中所做的操作:
<TableLayout
android:id="@+id/score_box"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/score_button">
<include
layout="@layout/score_box_row"
android:id="@+id/row_1" />
<include
layout="@layout/score_box_row"
android:id="@+id/row_2"
android:layout_below="@+id/row_1" />
<include
layout="@layout/score_box_row"
android:id="@+id/row_3"
android:layout_below="@+id/row_2" />
<include
android:id="@+id/row_4"
layout="@layout/score_box_row"
android:layout_below="@+id/row_3" />
</TableLayout>
该游戏可以与 2 到 4 名玩家一起玩,因此我的想法是将没有相应玩家的行的可见性设置为 View.GONE 。
唯一的问题是我无法“找到”这些行。 findViewById() 返回 null。该方法在 setContentView() 之后立即从 onCreate() 调用
private void initializeArrays() {
TableLayout table = (TableLayout) findViewById(R.id.score_box);
Log.w(getClass().getName(), "Child Count: " + table.getChildCount());
rows = ArrayList<TableRow>();
rows.add((TableRow) findViewById(R.id.row_1)); //Line 95
rows.add((TableRow) findViewById(R.id.row_2));
rows.add((TableRow) findViewById(R.id.row_3));
rows.add((TableRow) findViewById(R.id.row_4));
letters[0] = R.drawable.f;
letters[1] = R.drawable.a;
letters[2] = R.drawable.r;
letters[3] = R.drawable.k;
letters[4] = R.drawable.l;
letters[5] = R.drawable.e;
}
我总是在运行时得到一个NullPointerException。 TableRows 在那里是因为 table.getChildCount() 返回 4。我不确定出了什么问题。
这是 android 的错误还是 eclipse 的错误?
编辑:根据要求,这里是堆栈:
06-14 15:59:40.437: WARN/dalvikvm(1587): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): FATAL EXCEPTION: main
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mikgonz.farkle/com.mikgonz.farkle.ui.GameScreen}: java.lang.NullPointerException
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at android.os.Looper.loop(Looper.java:130)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at android.app.ActivityThread.main(ActivityThread.java:3835)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at java.lang.reflect.Method.invoke(Method.java:507)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at dalvik.system.NativeStart.main(Native Method)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): Caused by: java.lang.NullPointerException
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at com.mikgonz.farkle.ui.GameScreen.initializeArrays(GameScreen.java:95)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at com.mikgonz.farkle.ui.GameScreen.onCreate(GameScreen.java:48)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
06-14 15:59:40.441: ERROR/AndroidRuntime(1587): ... 11 more
最佳答案
首先,看起来应该是这样
rows = new ArrayList<TableRow>();
我很惊讶该行竟然能编译(如果确实如此)。
此外,这将是一个愚蠢的错误,请确保 score_box_row
布局文件实际存在并正确定义布局。
关于java - 使用 <include> android 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6347535/