Java 无法暂停活动?

Java 无法暂停活动?,java,android,android-logcat,Java,Android,Android Logcat,我是Android开发新手。我写了这段代码,但我不知道是什么导致我的应用程序崩溃。它告诉我活动无法暂停。我肯定这是我漏掉的东西。我写了非常类似的代码,在过去工作过,所以我不知道从哪里开始 SplashActivity.java public class SplashActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.

我是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'
我迷路了。

使用

ImageView logo = (ImageView) findViewById(R.id.ImageViewLogo);
而不是

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

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

,所以您正在尝试使用TextView引用ImageView 因此它抛出
ClassCastException

改变这个

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

 ImageView logo = (ImageView) findViewById(R.id.ImageViewLogo);
请参见Oncreate()方法中的这一行

onPause()方法使用编写了TextView而不是上面的ImageView

    TextView logo = (TextView) findViewById(R.id.ImageViewLogo);
这是给你的类强制转换异常。。将文本视图更改为图像视图

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