简体   繁体   中英

How do I make the back button I created in the activity change the text in the textView to the previous text created by the random generator?

When I start the activity on my phone, it says that the app is not responding. The problem is the back button which I can't seem to program to make it gather the previous text from the textView.

Xml code for activity

<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:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Author" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/button_shape"
    android:text="@string/Getstarted"
    android:textColor="#FFFFFF"
    android:textSize="23sp" />

<ImageButton
    android:id="@+id/next"
    android:layout_width="90dp"
    android:layout_height="50dp"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="14dp"
    android:background="@drawable/button_shape"
    android:contentDescription="@string/Next"
    android:onClick="NextQuote"
    android:src="@drawable/navigationnextitem" />

<ImageButton
    android:id="@+id/share"
    android:layout_width="90dp"
    android:layout_height="50dp"
    android:layout_alignTop="@+id/next"
    android:layout_centerHorizontal="true"
    android:background="@drawable/button_shape"
    android:contentDescription="@string/share"
    android:onClick="Sharing"
    android:src="@drawable/socialshare" />

    <ImageButton
        android:id="@+id/back"
        android:layout_width="90dp"
        android:layout_height="50dp"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignTop="@+id/next"
        android:background="@drawable/button_shape"
        android:contentDescription="@string/Back"
        android:onClick="PreviousQuote"
        android:src="@drawable/navigationpreviousitem" />

</RelativeLayout>

Java code for activity

package com.android.motivateme3;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

public class Author extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_author);
        // Show the Up button in the action bar.
        setupActionBar();
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);

             Button NextQuote = (Button)findViewById(R.id.next);
                final TextView display = (TextView) findViewById(R.id.textView1);
                NextQuote.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                         Random numGen = new Random();
                        int rNumber = numGen.nextInt(10);
                    if (rNumber  == 0)
                    {   
                        display.setText(R.string.Author1);
                    }
                    else if (rNumber  == 1)
                    {   
                        display.setText(R.string.Author2);
                    }
                    else if (rNumber  == 2)
                    { 
                        display.setText(R.string.Author3);
                    }
                    else if (rNumber  == 3)
                    {
                        display.setText(R.string.Author4);
                    }
                    else if (rNumber  == 4)
                    {
                        display.setText(R.string.Author5);
                    }
                    else if (rNumber  == 5)
                    {
                        display.setText(R.string.Author6);
                    }
                    else if (rNumber  == 6)
                    {
                        display.setText(R.string.Author7);
                    }
                    else if (rNumber  == 7)
                    {
                        display.setText(R.string.Author8);
                    }
                    else if (rNumber  == 8)
                    {
                        display.setText(R.string.Author9);
                    }
                    else if (rNumber  == 9)
                    {
                        display.setText(R.string.Author10);
                    }       }
                });
                        }

                    ImageButton Sharing = (ImageButton)findViewById(R.id.share);
                    Sharing.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v){

                        TextView text = (TextView)findViewById(R.id.textView1);
                        String quote = text.getText().toString();{
                        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
                        shareIntent.setType("plain/text");
                        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "This is a great quote (from the Motivate Me! app)");
                        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, quote);
                        startActivity(Intent.createChooser(shareIntent, "Share via:"));}}});



                        Button BackQuote = (Button)findViewById(R.id.back);
                        final TextView display = (TextView) findViewById(R.id.textView1);
                        BackQuote.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                String prev = (String) display.getText();
                                display.setText(prev);
                            }
                    });}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.author, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

This is the stack trace

04-03 14:29:34.174: E/AndroidRuntime(17383):    at android.app.ActivityThread.main(ActivityThread.java:4441)
04-03 14:29:34.174: E/AndroidRuntime(17383):    at java.lang.reflect.Method.invokeNative(Native Method)
04-03 14:29:34.174: E/AndroidRuntime(17383):    at java.lang.reflect.Method.invoke(Method.java:511)
04-03 14:29:34.174: E/AndroidRuntime(17383):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
04-03 14:29:34.174: E/AndroidRuntime(17383):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
04-03 14:29:34.174: E/AndroidRuntime(17383):    at dalvik.system.NativeStart.main(Native Method)
04-03 14:29:34.174: E/AndroidRuntime(17383): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
04-03 14:29:34.174: E/AndroidRuntime(17383):    at com.android.motivateme3.Author.setupActionBar(Author.java:36)
04-03 14:29:34.174: E/AndroidRuntime(17383):    at com.android.motivateme3.Author.onCreate(Author.java:25)
04-03 14:29:34.174: E/AndroidRuntime(17383):    at android.app.Activity.performCreate(Activity.java:4465)
04-03 14:29:34.174: E/AndroidRuntime(17383):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-03 14:29:34.174: E/AndroidRuntime(17383):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
04-03 14:29:34.174: E/AndroidRuntime(17383):    ... 11 more
04-03 14:29:41.664: I/Process(17383): Sending signal. PID: 17383 SIG: 9

It seems to me that R.id.next refers to an ImageButton , which cannot be cast into a Button , since it is not a subclass of a Button

Do you happen to see a ClassCastException?

To fix that particular issue, replace:

Button NextQuote = (Button)findViewById(R.id.next);

with

ImageButton NextQuote = (ImageButton)findViewById(R.id.next);

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