2012-03-25 109 views
0

我希望用户能够点击按钮并将其带到不同的活动。我之前在其他应用中使用过类似的代码,但每次按下按钮时,应用都会崩溃。在主菜单中,我有:在Android中的活动之间切换

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);  

} 

} 
+0

你能还发布DDMS日志?这将有助于了解应用程序崩溃的位置。 – 2012-03-25 12:47:18

+0

在Eclipse中使用'adb logcat',DDMS或LogCat视图来检查LogCat并查看与崩溃相关的堆栈跟踪。 – CommonsWare 2012-03-25 12:58:52

+0

如何发布DDMS日志?我试图看看LogCat中的东西,但我不知道它的含义。这是控制台中的最后一行: [2012-03-25 13:59:46 - com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper] C:\ Users \ Tabitha \ workspace的解析器异常\ HelloTabWidget \ AndroidManifest.xml:根元素后面的文档中的标记必须是格式良好的。 – user1260239 2012-03-25 13:04:22

回答

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(); 
     } 
    }); 
+0

我得到这些错误:'changeAct'不能解析为一个变量,stop()未定义为新的View.OnClickListener(){}。 – user1260239 2012-03-25 15:02:10

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>