2

I'm new to Android Dev, so please help me out.

I'm trying to start a new activity after i press a button but nothing seems to work. Here's my code:

public class viewInfo extends Activity { private Button btn; public TextView txt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.info); btn=(Button)findViewById(R.id.buy); btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent myIntent = new Intent(viewInfo.this, buyNow.class); startActivity(myIntent); } }); } 

I've also added this new activity in the Manifest but it keeps crushing after I press the button. What am I doing wrong?

3
  • Paste the logcat output after it crAshes. Commented Jul 30, 2011 at 17:59
  • Could you include the excerpt from the AndroidManifest.xml? Also, what errors show up in the logcat output? (You can access Logcat from the DDMS perspective if you are using Eclipse). Commented Jul 30, 2011 at 17:59
  • Please let us know the error that you are getting from logcat. Commented Jul 30, 2011 at 18:00

4 Answers 4

4

Misread the question initially (original answer below for completeness sake).

Make sure you have the activity you are calling defined in your manifest file:

Something like

<activity android:name=".buyNow" android:label="@string/app_name"></activity> 

within the application tags would suffice.


Here's the original answer.

Assuming that you have the correct button ID - try this in your onclick:

Intent myIntent = new Intent(getApplicationContext(), buyNow.class); startActivity(myIntent); 

You could add a log message inside your onClick too, to make sure it is actually being called. You can see the log from logcat (run via adb logcat on the command line)

Sign up to request clarification or add additional context in comments.

2 Comments

I have the activity in the manifest within the application tag: Here it is:<activity android:name=".buyNow" android:theme="@android:style/Theme.NoTitleBar"></activity> and also I've tried this method but it still crushes!
Maybe it's whatever is in your buyNow class which is causing the application to crash. You need to either step through in eclipse debugger or put in log statements to see where the program is going. at the very least you need to provide us with the logcat output so that we can help you.
0

Try to make it:

startActivity(new Intent("[Here is your package name.what you declared in Manifest file]"));

For this you need to write to your manifest:

Hope it helps.

Comments

0

There may be a problem with your buyNow activity that is causing the error.

You really need to use logcat to trace the error. You can enable this by clicking the menu item:

Window -> Show View -> Other...

the selecting "LogCat" from the Android folder

2 Comments

I've managed to solve the problem! Thanks for all of your help!
Glad to hear it ... Please remember to upvote any answers that were helpful. Also accept a correct answer if applicable. This provides a useful metric for our community. You can read more on How does accepting answers work
0

You can simply do this:

startActivity(new Intent(getBaseContext(),Activity.class)); 

Afther you registred your activity in manifest:

 <activity android:name="com.example.ActivityName" android:label="@string/app_name" > </activity> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.