Android告诉我应用程序已经停止了,但我的代码中没有错误。
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TableLayout;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
for(int i = 0; i < 6; i++) {
TableLayout tl = (TableLayout) findViewById(R.id.T);
for(int j = 0; j < 6; j++) {
ImageView img = (ImageView) tl.getChildAt(j);
img.setImageResource(R.drawable.w);
}
}
}
}
Logcat说:
03-03 19:25:43.550: WARN/dalvikvm(487): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): FATAL EXCEPTION: main
03-03 19:25:43.580: ERROR/AndroidRuntime(487): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.HelloAndroid}: java.lang.ClassCastException: android.widget.TableRow
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.os.Looper.loop(Looper.java:123)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at java.lang.reflect.Method.invoke(Method.java:507)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at dalvik.system.NativeStart.main(Native Method)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): Caused by: java.lang.ClassCastException: android.widget.TableRow
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:17)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): ... 11 more
编辑:问题在于布局:
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
for(int i = 0; i < 7; i++) {
TableLayout tl = (TableLayout) findViewById(R.id.T);
TableRow tr = (TableRow) tl.getChildAt(i);
for(int j = 0; j < 7; j++) {
ImageView img = (ImageView) tr.getChildAt(j);
img.setImageResource(R.drawable.w);
}
}
}
}
答案 0 :(得分:3)
查看Logcat以查看将查明故障位置的堆栈跟踪。
我的猜测是findViewById()或getChildAt()返回null,即你的布局不是你想象的那样。
答案 1 :(得分:0)
TableLayout tl = (TableLayout) findViewById(R.id.T);
:
findViewById(R.id.T)
的结果很可能不是TableLayout类型(但不是null)。
尝试打印出findViewById(R.id.T).getClass().getName();
这就是我告诉我的:
Caused by: java.lang.ClassCastException: android.widget.TableRow
at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:17)
答案 2 :(得分:0)
由于 layout.xml 编码错误,我收到此错误。而不是......
<?xml version="1.0" encoding="iso-8859-1"?>
......应该是我的情况:
<?xml version="1.0" encoding="utf-8"?>
很难想出这个。