开发者

NullPointerException when using <include> android

I have an activity that shows a game screen. One of the UI elements is a score box. The score box is basically a Table Layout with four rows. All the rows are the same here is the xml representing one row.

<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>

Each row then has imageview , a textview representing the player name, and two other textviews representing various scores.

Here is what I do in the layout used by the 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>

The game can be played with 2 to 4 players so what I had in mind was to set the Visibility of the Rows without respective player to View.GONE .

The only problem is that I cannot "find" the rows. findViewById() returns null. This method is called from onCreate() right after setContentView()

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;

    }

I always get a NullPointerException at runtime. The TableRows are there because table.getChildCount() returns 4. I'm not sure what is wrong.

Is this a bug with android or a bug with eclipse?

EDIT: As requested here is the stack:

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(15开发者_开发技巧87):     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


First, it looks like that should be

rows = new ArrayList<TableRow>();

I'm surprised that that line compiled at all, if it did.

Also, and this would be a silly error, make sure that the score_box_row layout file actually exists and defines a layout properly.


It looks like your score_box is using android:layout_width="fill_parent" while your main TableLayout is using android:layout_width="match_parent". One of these is likely incorrect, which is preventing your XML from compiling correctly.

I tried your app, compiling against SDK 2.1 and using "fill_parent" for both those values and it worked.


findViewById(R.id.row_1)

is probably returning null. Did you call setContentView() etc, so that that id is actually in the view? You can NOT find any random id with findViewById(), it looks specifically in the current set contentView, or stuff you've added to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜