0

I want the user to be able to tap a button and be taken to a different activity. I've used similar code before in another app, but every time I press the button now the app crashes. In the main menu I have:

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

In the manifest:

 <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 is:

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

}

}
user1260239
  • 1
  • 1
  • 2
  • Can you also post the DDMS log? This will help understand where the app crashes. – Siddharth Lele Mar 25 '12 at 12:47
  • Use `adb logcat`, DDMS, or the LogCat view in Eclipse to examine LogCat and look at the stack trace associated with your crash. – CommonsWare Mar 25 '12 at 12:58
  • How do I post the DDMS log? I tried looking at the stuff in LogCat but I don't know what any of it means. This is the last line in the console: [2012-03-25 13:59:46 - com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper] Parser exception for C:\Users\Tabitha\workspace\HelloTabWidget\AndroidManifest.xml: The markup in the document following the root element must be well-formed. – user1260239 Mar 25 '12 at 13:04
  • Wait ignore the last line I posted there, that's for a different app. Sorry. – user1260239 Mar 25 '12 at 13:06
  • What is the activity name that holds the button? Probly some activity is missing in the manifest file. – Tiago Almeida Mar 25 '12 at 13:07
  • I don't think I've got an activity holding the button... what should that activity look like? – user1260239 Mar 25 '12 at 15:10

2 Answers2

0

Try this: In the onClick modify your first class to this:

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();
        }
    });
Alexandre Hitchcox
  • 2,704
  • 5
  • 22
  • 33
  • I get these errors: 'changeAct' cannot be resolved to a variable and stop() is undefined for new View.OnClickListener(){}. – user1260239 Mar 25 '12 at 15:02
0
Intent intent = new Intent (CurrentActivity.this, TestClass.class);
startActivity(intent);

If your TestClass is in another package just put your package in front.

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

Declare the activity in manifest like this:

<activity
        android:name="de.vogella.android.c2dm.simpleclient.TestClass"
    </activity>
Radu Dan
  • 453
  • 4
  • 22