3

我是 Android 开发的新手。我编写了这段代码,但我无法弄清楚是什么导致我的应用程序崩溃。它告诉我活动无法暂停。我确定这是我要遗漏的东西。我写了过去工作的非常相似的代码,所以我不知道从哪里开始。

SplashActivity.java

public class SplashActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        ImageView logo = (ImageView) findViewById(R.id.ImageViewLogo);
        Animation twistIn = AnimationUtils.loadAnimation(this, R.anim.twist_in);
        logo.startAnimation(twistIn);
        twistIn.setAnimationListener(new AnimationListener()
        {
            public void onAnimationStart(Animation animation)
            {
                return; // do nothing
            }
            public void onAnimationRepeat(Animation animation)
            {
                return; // do nothing
            }
            public void onAnimationEnd(Animation animation)
            {
                startActivity(new Intent(SplashActivity.this, MenuActivity.class));
                SplashActivity.this.finish();
            }
        });
    }

    @Override
    protected void onPause()
    {
        super.onPause();

        TextView logo = (TextView) findViewById(R.id.ImageViewLogo);
        logo.clearAnimation();
    }
}

这是 LogCat 的抱怨:

04-04 02:09:10.460: W/dalvikvm(12531): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
04-04 02:09:10.480: E/AndroidRuntime(12531): FATAL EXCEPTION: main
04-04 02:09:10.480: E/AndroidRuntime(12531): java.lang.RuntimeException: Unable to pause activity {com.swapwords/com.swapwords.SplashActivity}: java.lang.ClassCastException: android.widget.ImageView
04-04 02:09:10.480: E/AndroidRuntime(12531):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2358)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2315)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2295)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at android.app.ActivityThread.access$1700(ActivityThread.java:117)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at android.os.Looper.loop(Looper.java:130)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at android.app.ActivityThread.main(ActivityThread.java:3691)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at java.lang.reflect.Method.invokeNative(Native Method)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at java.lang.reflect.Method.invoke(Method.java:507)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at dalvik.system.NativeStart.main(Native Method)
04-04 02:09:10.480: E/AndroidRuntime(12531): Caused by: java.lang.ClassCastException: android.widget.ImageView
04-04 02:09:10.480: E/AndroidRuntime(12531):    at com.swapwords.SplashActivity.onPause(SplashActivity.java:57)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at android.app.Activity.performPause(Activity.java:3893)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1194)
04-04 02:09:10.480: E/AndroidRuntime(12531):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2345)
04-04 02:09:10.480: E/AndroidRuntime(12531):    ... 12 more
04-04 02:09:12.452: I/dalvikvm(12531): threadid=4: reacting to signal 3
04-04 02:09:12.452: I/dalvikvm(12531): Wrote stack traces to '/data/anr/traces.txt'
04-04 02:09:18.878: I/dalvikvm(12531): threadid=4: reacting to signal 3
04-04 02:09:18.878: I/dalvikvm(12531): Wrote stack traces to '/data/anr/traces.txt'

我只是迷路了。

4

3 回答 3

3

采用

ImageView logo = (ImageView) findViewById(R.id.ImageViewLogo);

代替

TextView logo = (TextView) findViewById(R.id.ImageViewLogo);

onPause()方法中,因为当前您正在尝试将ImageView(您已在 xml 布局中添加)转换为TextView

于 2013-04-04T06:21:06.830 回答
2

您正在尝试使用 Textview 引用 ImageView 所以它会抛出ClassCastException

改变这个

 TextView logo = (TextView) findViewById(R.id.ImageViewLogo);

对此

 ImageView logo = (ImageView) findViewById(R.id.ImageViewLogo);
于 2013-04-04T06:21:23.643 回答
1

在 Oncreate() 方法中看到这一行

 ImageView logo = (ImageView) findViewById(R.id.ImageViewLogo);

并且onPause() 方法使用已经写了 TextView 而不是 ImageView像上面一样

    TextView logo = (TextView) findViewById(R.id.ImageViewLogo);

所以这给了你 classcast 异常..将 TextView 更改为 ImageView

于 2013-04-04T06:21:46.003 回答