简体   繁体   中英

how to open another activity in android using a button

so the code below is not doing the proper function it is called to do it is supposed to open up a new activity upon a click of a button, but instead nothing happens buttons display and no errors and this stupid website is requiring me to explain a little more so im just going type random nonsense until it lets me post my question

package com.Tripp.thebasics;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Menu extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //setting up the button references
        Button jokeD = (Button) findViewById(R.id.jokeoftheday);
        Button jokeC = (Button) findViewById(R.id.jokecatagories);

        jokeD.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                startActivity(new Intent(Menu.this, JokeOfTheDay.class));

            }
        });

        jokeC.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                Intent s = new Intent("com.Tripp.thebasics.JOKECATAGORIES");
                startActivity(s);
            }
        });
    }

    @Override
    protected void onPause() {

        // TODO Auto-generated method stub
        super.onPause();
    }
}

Help with creating an intent such that you can call an activity when a button is clicked. Also need help declaring an activity in a manifest file:

 myBtn.setOnClickListener() {
        public void onClick() {
            Intent intent = new Intent(this, SecondActivity.class);
            startActivity(intent);
        }
    }
  1. create seperate xml and java files for new activity.

  2. Create on click listener like below for the button. Button.setOnClickListener() { public void onClick() { Intent myintent = new Intent(this, newactivity.class); startActivity(myintent); } }

3.add new activity on android manifest.xml file.

<application>

`<activity android:name=".classname" ></activity>`

</application

If you want more clarifications ,inform me

jokeD.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            startActivity(new Intent(this,    JokeOfTheDay.class));

            }
         });

And make sure you have your new class in the AndroidManifest.xml:

<activity
            android:name=".youractivitypackagename.JokeOfTheDay"
            android:label="JokeOfTheDay" >
</activity>

EDIT: Take a look here for a better explanation of how to declare an activity in the Manifest, as it depends on how you declare the package: http://developer.android.com/guide/topics/manifest/manifest-intro.html .

If your package for your class is, for example, com.example.project.Test, then you should have the following, within the tags:

<activity
                android:name="com.example.project.Test.JokeOfTheDay"
                android:label="JokeOfTheDay" >
 </activity>

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