致命异常 - java.lang.ClassCastException:android.widget.TextView

时间:2013-09-19 15:48:02

标签: java android casting fatal-error

我遇到了一个问题,即由于类转发异常而导致致命异常。我做了一些研究,发现了这个:

http://docs.oracle.com/javase/7/docs/api/java/lang/ClassCastException.html

然而,这是我第一次遇到这样的问题 - 而且我不确定它究竟是如何解决的。

09-19 15:41:48.711: E/AndroidRuntime(430): FATAL EXCEPTION: main
09-19 15:41:48.711: E/AndroidRuntime(430): java.lang.ClassCastException: android.widget.TextView
09-19 15:41:48.711: E/AndroidRuntime(430):  at com.project.example.ssettings.UpdateActivity$NetworkTask.setProgressImgView(UpdateActivity.java:365)
09-19 15:41:48.711: E/AndroidRuntime(430):  at com.project.example.ssettings.UpdateActivity$NetworkTask.onProgressUpdate(UpdateActivity.java:352)
09-19 15:41:48.711: E/AndroidRuntime(430):  at com.project.example.ssettings.UpdateActivity$NetworkTask.onProgressUpdate(UpdateActivity.java:1)
09-19 15:41:48.711: E/AndroidRuntime(430):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432)
09-19 15:41:48.711: E/AndroidRuntime(430):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 15:41:48.711: E/AndroidRuntime(430):  at android.os.Looper.loop(Looper.java:123)
09-19 15:41:48.711: E/AndroidRuntime(430):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-19 15:41:48.711: E/AndroidRuntime(430):  at java.lang.reflect.Method.invokeNative(Native Method)
09-19 15:41:48.711: E/AndroidRuntime(430):  at java.lang.reflect.Method.invoke(Method.java:507)
09-19 15:41:48.711: E/AndroidRuntime(430):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-19 15:41:48.711: E/AndroidRuntime(430):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-19 15:41:48.711: E/AndroidRuntime(430):  at dalvik.system.NativeStart.main(Native Method)

代码:

    @Override
        protected void onProgressUpdate(Integer... progress) {
            // Call function to update image view
            setProgressImgView(progress[0], progress[1], progress[2],
                    progress[3], progress[4]);
        }

        private void setProgressImgView(Integer imgViewId1, Integer imgViewId2,
                Integer imgViewId3, Integer imgViewId4, Integer imgViewId5) {
            // update image view with the updating dots
            // Reset view layout in case orientation while updating
            setContentView(R.layout.updating);
            mProgressImageview1 = (ImageView) findViewById(R.id.loading_empty1);
            mProgressImageview2 = (ImageView) findViewById(R.id.loading_empty2);
            mProgressImageview3 = (ImageView) findViewById(R.id.loading_empty3);
            mProgressImageview4 = (ImageView) findViewById(R.id.loading_empty4);
            mProgressImageview5 = (ImageView) findViewById(R.id.loading_empty5);
            mProgressImageview1.setImageResource(imgViewId1);
            mProgressImageview2.setImageResource(imgViewId2);
            mProgressImageview3.setImageResource(imgViewId3);
            mProgressImageview4.setImageResource(imgViewId4);
            mProgressImageview5.setImageResource(imgViewId5);

        }

2 个答案:

答案 0 :(得分:0)

这只是意味着您的(ImageView)某个演员试图将TextView投射到ImageView,这是错误的。

两者都继承自android.view.View(因此都可以转换为View),但不能将其转换为另一种。

答案 1 :(得分:0)

我做的前两个假设是因为你的代码没有上下文:

  1. 看起来您的代码来自AsyncTask,对吧?
  2. 您正在使用AsyncTask作为内部类,对吧? (因为否则你不应该打电话给setContentView()
  3. 我猜您已在活动的setContentView()方法中调用了onCreate()。无需调用它两次(并且在AsyncTask中处理方向更改有点错误。)

    您应该将5个ImageView作为成员变量,只需在setProgressImgView()中使用这些变量,而无需再次调用setContentView()