这是我拥有的 XML:
<ImageView
android:id="@+id/ivRockPic"
android:layout_width="230dp"
android:layout_height="230dp"
android:layout_gravity="center"
android:background="@drawable/image_border" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="100" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:weightSum="100"
android:id="@+id/llAns1"
android:orientation="horizontal">
<TextView
android:id="@+id/tvAns1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:text="Ans1"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/ivAns1"
android:layout_gravity="right"
android:layout_weight="50"
android:layout_width="100dp"
android:layout_height="50dp"
/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:weightSum="100"
android:id="@+id/llAns2"
android:orientation="horizontal">
<TextView
android:id="@+id/tvAns2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Ans2"
android:layout_weight="50"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/ivAns2"
android:layout_gravity="right"
android:layout_width="100dp"
android:layout_weight="50"
android:layout_height="50dp"
/>
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="100" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:weightSum="100"
android:id="@+id/llAns3"
android:orientation="horizontal">
<TextView
android:id="@+id/tvAns3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:text="Ans3"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/ivAns3"
android:layout_gravity="right"
android:layout_width="100dp"
android:layout_weight="50"
android:layout_height="50dp"
/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:weightSum="100"
android:id="@+id/llAns4"
android:orientation="horizontal">
<TextView
android:id="@+id/tvAns4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:text="Ans4"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/ivAns4"
android:layout_gravity="right"
android:layout_width="100dp"
android:layout_weight="50"
android:layout_height="50dp"
/>
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="100" >
<TextView
android:id="@+id/tvAnswer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50" />
<Button
android:id="@+id/bNext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:text="Next" />
</TableRow>
</TableLayout>
只贴了一个按钮。这重复了。这个设置工作得很好。经过测试,在 bean 中初始化,它正在工作。但这是一个问题,当我将 ImageView 移到 TextView 上方时,它会在我获取 ImageView id 的视图的启动行上引发错误。但是当我把它移回来时,它工作正常。有任何想法吗?
错误:
里面的两个方法只是字符串的初始化:
res = getResources();
img = (ImageView)findViewById(R.id.ivRockPic);
tvAnswer = (TextView) findViewById(R.id.tvAnswer);
initializeRussian();
initAnswerList();
next = (Button)findViewById(R.id.bNext);
iViews[0] = (ImageView) findViewById(R.id.ivAns1);//dies
iViews[1] = (ImageView) findViewById(R.id.ivAns2);
iViews[2] = (ImageView) findViewById(R.id.ivAns3);
iViews[3] = (ImageView) findViewById(R.id.ivAns4);
lLayouts[0] = (LinearLayout) findViewById(R.id.llAns1);
lLayouts[1] = (LinearLayout) findViewById(R.id.llAns2);
lLayouts[2] = (LinearLayout) findViewById(R.id.llAns3);
lLayouts[3] = (LinearLayout) findViewById(R.id.llAns4);
tViews[0] = (TextView) findViewById(R.id.tvAns1);
tViews[1] = (TextView) findViewById(R.id.tvAns2);
tViews[2] = (TextView) findViewById(R.id.tvAns3);
tViews[3] = (TextView) findViewById(R.id.tvAns4);
06-29 23:57:09.838: E/AndroidRuntime(5236): Uncaught handler: thread main exiting due to uncaught exception
06-29 23:57:09.869: E/AndroidRuntime(5236): java.lang.RuntimeException: Unable to start activity ComponentInfo{rock.school.rzn.rockquiz/rock.school.rzn.rockquiz.GameScreen}: java.lang.ClassCastException: android.widget.TextView
06-29 23:57:09.869: E/AndroidRuntime(5236): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-29 23:57:09.869: E/AndroidRuntime(5236): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-29 23:57:09.869: E/AndroidRuntime(5236): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-29 23:57:09.869: E/AndroidRuntime(5236): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-29 23:57:09.869: E/AndroidRuntime(5236): at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 23:57:09.869: E/AndroidRuntime(5236): at android.os.Looper.loop(Looper.java:123)
06-29 23:57:09.869: E/AndroidRuntime(5236): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-29 23:57:09.869: E/AndroidRuntime(5236): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 23:57:09.869: E/AndroidRuntime(5236): at java.lang.reflect.Method.invoke(Method.java:521)
06-29 23:57:09.869: E/AndroidRuntime(5236): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-29 23:57:09.869: E/AndroidRuntime(5236): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-29 23:57:09.869: E/AndroidRuntime(5236): at dalvik.system.NativeStart.main(Native Method)
06-29 23:57:09.869: E/AndroidRuntime(5236): Caused by: java.lang.ClassCastException: android.widget.TextView
06-29 23:57:09.869: E/AndroidRuntime(5236): at rock.school.rzn.rockquiz.GameScreen.initialize(GameScreen.java:171)
06-29 23:57:09.869: E/AndroidRuntime(5236): at rock.school.rzn.rockquiz.GameScreen.onCreate(GameScreen.java:47)
06-29 23:57:09.869: E/AndroidRuntime(5236): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-29 23:57:09.869: E/AndroidRuntime(5236): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-29 23:57:09.869: E/AndroidRuntime(5236): ... 11 more