无法暂停活动?

时间:2013-04-04 06:19:23

标签: java android android-logcat

我是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'

我迷失了。

3 个答案:

答案 0 :(得分:3)

使用

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

而不是

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

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

答案 1 :(得分:2)

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

更改此

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

到这个

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

答案 2 :(得分:1)

在Oncreate()方法

中查看此行
 ImageView logo = (ImageView) findViewById(R.id.ImageViewLogo);

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

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

所以这会给你一个classcast异常..将 TextView更改为ImageView