当前位置: 动力学知识库 > 问答 > 编程问答 >

android - open another activity with a button not working

问题描述:

So when this code runs I get no errors it displays the buttons correctly and everything, but when I click on the button nothing happens it just clicks and clicks and clicks nothing happens below I will post xml manifest code so you can see that I have done everything perfectly, but somehow it it is not doing the correct thing that I am asking it to do

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

//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

Intent i = new Intent(getApplicationContext(),JokeOfTheDay.class);

startActivity(i);

}

});

jokeC.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent s = new Intent(".JokeCatagories");

startActivity(s);

}

});

}

@Override

protected void onPause() {

// TODO Auto-generated method stub

super.onPause();

}

}

below here I will post the manifest code

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.Tripp.thebasics"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="15"

android:targetSdkVersion="21" />

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme"

>

<activity

android:name=".Main"

android:label="@string/app_name"

android:screenOrientation="portrait"

>

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity

android:name=".Menu"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MENU" />

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

</activity>

<activity

android:name=".com.Tripp.thebasics.JokeCatagories"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.JOKECATAGORIES" />

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

</activity>

<activity

android:name=".Sweet"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.SWEET" />

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

</activity>

<activity

android:name=".com.Tripp.thebasics.JokeOfTheDay"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.JOKEOFTHEDAY" />

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

</activity>

</application>

</manifest>

网友答案:

Change your OnClickListeners as follows...

jokeD.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent i = new Intent(Menu.this, JokeOfTheDay.class);
        startActivity(i);
    }
});

jokeC.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent s = new Intent(Menu.this, JokeCatagories.class);
        startActivity(s);
    }
});

Then get rid of this <intent-filter> from the JokeCategories <activity> section in the manifest...

<intent-filter>
    <action android:name="android.intent.action.JOKECATAGORIES"     />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

...and get rid of this one from the JokeOfTheDay <activity> section...

<intent-filter>
    <action android:name="android.intent.action.JOKEOFTHEDAY" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Basically, don't use the application Context to start an Activity (it's not necessary from an Activity as it has its own Context) and, on the whole, use explicit Intents to start Activities rather than defining <intent-filter> section in the manifest.

分享给朋友:
您可能感兴趣的文章:
随机阅读: