简体   繁体   中英

Why Isn't the Button responding to the Intent? Don't understand what's wrong here

I am trying to get the ABOUT button to listen for a click, in order to go from a screen displaying three buttons and a picture, to another screen just displaying some text.

This is my Main java file (MainActivity.java)

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();




    }

    public void addListenerOnButton() {
        final Context cont = this;

        Button aButton = (Button) findViewById(R.id.ab_button);

        aButton.setOnClickListener(
                new Button.OnClickListener(){
                    public void onClick(View v){
                        Intent intent = new Intent(cont, AboutActivity.class);
                        startActivity(intent);


                    }
                });
    }

This is my second java file for the about button (AboutActivity.java):

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class AboutActivity extends Activity {

    public void onCreate(Bundle about){
        super.onCreate(about);
        setContentView(R.layout.activity_about);
        TextView briText = (TextView) findViewById(R.id.about_b2);
        briText.setText("Goodie");
    }
}

This is the main xml file that displays the picture and the buttons (activity_main.xml):

    android:layout_width="match_parent"
    android:layout_height="match_parent"     
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/about_b"
        android:id="@+id/ab_button"
        android:shadowColor="#8e2300"
        android:background="@drawable/oval_button"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/share_button"
        android:id="@+id/share_button"
        android:shadowColor="#8e2300"
        android:onClick="shareActivity"
        android:background="@drawable/oval_button"

And this is the xml file that displays the text (activity_about.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/back_to_work_rel">

    <TextView android:text="@string/about_bac"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/about_b2" />

</RelativeLayout>

Manifest file (AndroidManifest.xml):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.brian.eggalong" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity

            android:name=".MainActivity"
            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=".AboutActivity"
            android:parentActivityName=".MainActivity"
            android:label="@string/about_bac">
            </activity>
    </application>

</manifest>

Every time I click the ABOUT button, nothing happens. Any help would be greatly appreciated.

Try to remove the background attribute of your button.

Otherwise you can try this

MainActivity.java

public void moveToAboutActivity(){
     Intent i = new Intent(getApplicationContext(), AboutActivity.class);
     startActivity(i);
}

activity_main.xml

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/about_b"
    android:id="@+id/ab_button"
    android:onClick="moveToAboutActivity"/>

good Luck

Ok, so, definetly the problem is that you need to put the activity in your manifest file like:

<activity
            android:name=".activity.SplashScreen"
            android:screenOrientation="portrait"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </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