我在表格行中設置了相對布局,當運行下面的代碼時出現 Logcat:錯誤,如何處理這個問題?
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#e3e1e1">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/tPr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<GridView
android:id="@+id/tApps"
android:layout_width="wrap_content"
android:layout_height="258dp"
android:background="#e3e1e1"
android:gravity="center"
android:horizontalSpacing="2dp"
android:numColumns="3"
android:verticalSpacing="2dp"
android:visibility="gone">
</GridView>
</RelativeLayout>
</TableRow>
</TableLayout>
Logcat:
02-20 17:34:34.261: E/AndroidRuntime(1125): FATAL EXCEPTION: main
02-20 17:34:34.261: E/AndroidRuntime(1125): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.coverflow/com.example.coverflow.CoverFlowExample}: java.lang.ClassCastException: android.widget.ProgressBar
02-20 17:34:34.261: E/AndroidRuntime(1125): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-20 17:34:34.261: E/AndroidRuntime(1125): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-20 17:34:34.261: E/AndroidRuntime(1125): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-20 17:34:34.261: E/AndroidRuntime(1125): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-20 17:34:34.261: E/AndroidRuntime(1125): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 17:34:34.261: E/AndroidRuntime(1125): at android.os.Looper.loop(Looper.java:123)
02-20 17:34:34.261: E/AndroidRuntime(1125): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-20 17:34:34.261: E/AndroidRuntime(1125): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 17:34:34.261: E/AndroidRuntime(1125): at java.lang.reflect.Method.invoke(Method.java:521)
02-20 17:34:34.261: E/AndroidRuntime(1125): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-20 17:34:34.261: E/AndroidRuntime(1125): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-20 17:34:34.261: E/AndroidRuntime(1125): at dalvik.system.NativeStart.main(Native Method)
02-20 17:34:34.261: E/AndroidRuntime(1125): Caused by: java.lang.ClassCastException: android.widget.ProgressBar
02-20 17:34:34.261: E/AndroidRuntime(1125): at com.example.coverflow.CoverFlowExample.onCreate(CoverFlowExample.java:41)
Line 41 是: top = (GridView)findViewById(R.id.tApps);
以前我也遇見過這個錯誤,由於不同的 View,在有 GridView 的地方,會擾亂一個 ProgressBar。應該清除你的項目。
Project-->Clean...