-1

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

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);

        }
4

2 回答 2

0

这只是意味着您的一个(ImageView)演员试图将 aTextView转换为 a ImageView,这是错误的。

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

于 2013-09-19T15:57:31.577 回答
0

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

  1. 看起来您的代码来自 AsyncTask,对吧?
  2. 您将 AsyncTask 用作内部类,对吗?(因为否则你不应该打电话setContentView()

setContentView()我猜你已经在你的活动方法中调用onCreate()了。无需调用它两次(并且在 AsyncTask 中处理方向更改有点错误)。

您应该将 5 个 ImageViews 作为成员变量,并在您的内部使用这些变量setProgressImgView()而无需再次调用setContentView()

于 2013-09-19T15:57:54.733 回答