在Android中切换活动

时间:2012-03-25 12:41:54

标签: java android android-activity

我希望用户能够点按一个按钮并进行其他活动。我之前在另一个应用程序中使用过类似的代码,但每次按下按钮,应用程序都会崩溃。在主菜单中我有:

Button testButton = (Button) findViewById(R.id.testButton); 
    testButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent("de.vogella.android.c2dm.simpleclient.TEST"));
        }
    });

在清单中:

 <activity
        android:name=".TestClass"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="de.vogella.android.c2dm.simpleclient.TEST" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

TestClass.java是:

package de.vogella.android.c2dm.simpleclient;

import android.app.Activity;
import android.os.Bundle;

public class TestClass extends Activity {
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub  
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);      

}

}

2 个答案:

答案 0 :(得分:0)

试试这个:在onClick上修改你的第一堂课:

Button testButton = (Button) findViewById(R.id.testButton); 
    testButton.setOnClickListener(new View.OnClickListener() {

        final ClassName changeAct = this; 

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent();
                    i.setClass(changeAct, TEST.class); //Change TEST.class to the name of the class you want it to go to.
                    startActivity(i);

                    stop();
        }
    });

答案 1 :(得分:0)

Intent intent = new Intent (CurrentActivity.this, TestClass.class);
startActivity(intent);

如果你的TestClass在另一个软件包中,只需将你的软件包放在前面。

Intent intent = new Intent (CurrentActivity.this, de.vogella.android.c2dm.simpleclient.TestClass.class);
startActivity(intent);

在清单中声明活动,如下所示:

<activity
        android:name="de.vogella.android.c2dm.simpleclient.TestClass"
    </activity>