2011-03-03 187 views
0

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); 
      } 
     } 
    } 
} 
+1

checked logcat? – Jimmy 2011-03-03 18:21:40

回答

3

看logcat中看到堆棧跟蹤,這將精確定位故障位置。

我的猜測是findViewById()或getChildAt()返回null,即您的佈局不是您所想的那樣。

+0

你說得對。 – pythonFoo 2011-03-03 18:32:12

0

TableLayout tl = (TableLayout) findViewById(R.id.T);

findViewById(R.id.T)結果是最有可能的類型不是TableLayout(但不爲空)。 嘗試打印出來findViewById(R.id.T).getClass().getName();

這就是這告訴我:

Caused by: java.lang.ClassCastException: android.widget.TableRow 
    at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:17) 
0

我得到這個錯誤的錯誤編碼的原因layout.xml。而不是...

<?xml version="1.0" encoding="iso-8859-1"?> 

...它應該一直在我的情況:

<?xml version="1.0" encoding="utf-8"?> 

一點很難弄清楚這一個。