【问题标题】: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?如何使我在活动中创建的后退按钮将 textView 中的文本更改为随机生成器创建的上一个文本?
【发布时间】:2013-03-25 04:39:20
【问题描述】:

当我在手机上启动活动时,它说应用程序没有响应。问题是后退按钮,我似乎无法对其进行编程以使其从 textView 收集以前的文本。

活动的 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: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 代码

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);
    }
}

这是堆栈跟踪

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

【问题讨论】:

  • 你能发布相关的堆栈跟踪吗?
  • logcat 输出中的红色文本块;如果使用 Eclipse,您可以在 DDMS 透视图中看到它。
  • 注意:您还应该删除按钮 (layout.xml) 中的 android:onClick="XXX" 行,因为您没有在 Activity 中实现此类方法

标签: java android button random


【解决方案1】:

在我看来,R.id.next 指的是ImageButton,它不能转换为Button,因为它不是Button 的子类

你碰巧看到 ClassCastException 吗?

要解决该特定问题,请替换:

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

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

【讨论】:

  • Button BackQuote = (Button)findViewById(R.id.back);应该是:ImageButton BackQuote = (ImageButton)findViewById(R.id.back);
  • 谢谢,我可以进入活动,但后退按钮没有响应
  • 是的,但是您在display中设置了您刚刚从中读取的相同文本; IE。你没有改变任何东西......
  • String prev = (String) display.getText(); display.setText(prev);这将始终产生相同的文本。这就是发生的情况:您将显示中的文本分配给一个变量并将该变量分配给显示文本。结果总是一样的。尝试更改文本。
  • 不,当按下后退按钮时,TextView 中的文本应该变回之前的文本 - textView 文本来自随机生成器
猜你喜欢
  • 2013-03-24
  • 1970-01-01
  • 2020-10-28
  • 2013-05-20
  • 2010-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多