简体   繁体   中英

Trying to start a new Activity using Intent

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?

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)

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>

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM