具有意图的Android startActivity不起作用

时间:2012-06-23 16:08:40

标签: android android-layout android-event

我有一个countDownTimer,它可以节省我想要进入错误屏幕的时间 这是countDownTimer的代码

timer = new CountDownTimer(36000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // TODOAuto-generated method stub
                remaingTimer.setText(millisUntilFinished + "");
            }

            @Override
            public void onFinish() {
                // TODO Auto-generated methodstub
                Intent goToMainTabs = new Intent(
                        "com.localizedbasedcompetition.FINISHTIME");
                startActivity(goToMainTabs);
            }
        };

我想去完成时间活动,这是终结时间活动的代码

public class FinishTime extends Activity implements OnClickListener{
    Button backToMain;
    TextView errorMessage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finishtime);
        initialize();
    }
    private void initialize() {
        // TODO Auto-generated method stub
        backToMain= (Button)findViewById(R.id.bFinishTimeBackToMain);
        errorMessage=(Button)findViewById(R.id.tvFinishTime);
        backToMain.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.bFinishTimeBackToMain:
            Intent goToMainTabs = new Intent(
                    "com.localizedbasedcompetition.MAINTABS");
            startActivity(goToMainTabs);
            break;

        default:
            break;
        }
    }

}

这是终结时间活动的主要活动

<activity
            android:name=".FinishTime"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.localizedbasedcompetition.FINISHTIME" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

我的问题是当countDownTimer完成计数时,我得到了一个激动,我做错了什么?

这是例外

06-23 14:16:14.352: E/AndroidRuntime(1001): FATAL EXCEPTION: main
06-23 14:16:14.352: E/AndroidRuntime(1001): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.localizedbasedcompetition/com.localizedbasedcompetition.FinishTime}: java.lang.ClassCastException: android.widget.TextView
06-23 14:16:14.352: E/AndroidRuntime(1001):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at android.os.Looper.loop(Looper.java:123)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at java.lang.reflect.Method.invokeNative(Native Method)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at java.lang.reflect.Method.invoke(Method.java:521)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at dalvik.system.NativeStart.main(Native Method)
06-23 14:16:14.352: E/AndroidRuntime(1001): Caused by: java.lang.ClassCastException: android.widget.TextView
06-23 14:16:14.352: E/AndroidRuntime(1001):     at com.localizedbasedcompetition.FinishTime.initialize(FinishTime.java:24)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at com.localizedbasedcompetition.FinishTime.onCreate(FinishTime.java:19)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-23 14:16:14.352: E/AndroidRuntime(1001):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-23 14:16:14.352: E/AndroidRuntime(1001):     ... 11 more

1 个答案:

答案 0 :(得分:0)

您需要在CountDownTimer上调用start()。否则它没有做任何事情。

http://developer.android.com/reference/android/os/CountDownTimer.html

根据你的例外,你已经在你的XML中定义了一些TextView,你不小心将其转换为其他东西(是你的Button演员吗?)在没有看到xml的情况下无法分辨,但这就是例外。确保在Java中正确引用您的视图,就像在XML中定义它们一样。

哦,正如iNan指出的那样,清理你的项目以防万一你修好了。 Eclipse有一种讨厌的方法,就是不时更新这样的东西。