简体   繁体   中英

Android not starting other activity

I'm pretty new to Android programming, but i have never encountered this problem before. When i click the button, the emulator is just not doing anything, when it's supposed to start another activity. Here's my intent within the button:

public class StartScreen extends Activity implements OnClickListener {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start_screen);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.bStartQuiz:
            Intent a = new Intent(StartScreen.this, QuizMenu.class);
            startActivity(a);   
        break;

Here follows some more cases which are not defined yet. onCreate method from the class i try to start:

public class QuizMenu extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.categories);

Here is the manifest:

<activity
        android:name="com.ultimatequiz.StartScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.ultimatequiz.QuizMenu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.ultimatequiz.QuizMenu" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

What i really don't understand is that i get no errors in the log. It's just not "responding", but the button do change into state_pressed.

I dont know if your case works well.Try this.

public class StartScreen extends Activity {

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_start_screen);

            Button myBtn = (Button) findViewById(R.id.btn);
            myBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent a = new Intent(StartScreen.this, QuizMenu.class);
                    startActivity(a);

                }
            });
        }

}

I always use this way, I find it easy to handle.

if you got your layout defined onlick delete it

If I understood correctly you want to open a new activity by clicking a Button . Then all you have to do is to have a Button in your activity_start_screen.xml :

<Button
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:id="@+id/your_button"/>

After that you have to add this in your StartScreen :

public class StartScreen extends Activity {
      @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start_screen);

    Button button = (Button) findViewById(R.id.your_button);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent activityChangeIntent = new Intent(StartScreen.this,QuizMenu.class);
            StartScreen.this.startActivity(activityChangeIntent);

        }
    });

and only this in your manifest :

<activity
    android:name="com.ultimatequiz.StartScreen"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name="com.ultimatequiz.QuizMenu"
    android:label="@string/app_name"/>

Try to do a function for it :

public static void LaunchIntent(){
        Intent a = new Intent(StartScreen.this, QuizMenu.class);
        startActivity(a);   

and in the click listener call the function :

public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bStartQuiz:
        LaunchIntent();
    break;
}

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