项目xml文件的下一个按钮 - android

时间:2014-09-20 13:02:01

标签: android

我有一个包含500个文本的xml文件。我有一个按钮随机播放并显示它。 我想为下一个和后一个添加两个按钮。采取每一个空间。 这是我的rando按钮代码。

final TextView tv = (TextView) findViewById(R.id.quote);
        Resources res = getResources();
        myString = res.getStringArray(R.array.quote);
        String q = myString[rgenerator.nextInt(myString.length)];
        tv.setText(q);
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                myString = getResources().getStringArray(R.array.quote);
                String q = myString[rgenerator.nextInt(myString.length)];
                tv.setText(q);
            }
        });

    }

这些按钮的代码是什么?谢谢

所有代码:

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class MainActivity extends ActionBarActivity {
    Context context;
    private String[] myString;
    private static final Random rgenerator = new Random();
    private AdView adView;
    int index = 0;
    private static final String AD_UNIT_ID = "ca-app-pub-7628432187347131/3277094808";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        context = this;
        LinearLayout layout = (LinearLayout) findViewById(R.id.ads_lin);
        layout.addView(adView);

        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("5B895A3CC0CA50D56506E300A4C8342B")
                .addTestDevice("D039292A1F434C999B21503D63D6FD88")

                .addTestDevice("TEST_EMULATOR").build();

        // Start loading the ad in the background.

        adView.loadAd(adRequest);
        // Start loading the ad in the background.
        final TextView tv = (TextView) findViewById(R.id.quote);

        Resources res = getResources();
        myString = res.getStringArray(R.array.quote);
        String q = myString[rgenerator.nextInt(myString.length)];
        tv.setText(q);
        Button btn = (Button) findViewById(R.id.btn);
        Button btnNext = (Button) findViewById(R.id.btnNext);
        Button btnPreview = (Button) findViewById(R.id.btnPreview);
        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                myString = getResources().getStringArray(R.array.quote);
                index = rgenerator.nextInt(myString.length);
                String q = myString[index];
                tv.setText(q);
            }
        });

        btnNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String q = myString[index++];
                tv.setText(q);
            }
        });

        btnPreview.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                String q = myString[index--];
                        tv.setText(q);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }

    @Override
    public void onPause() {
        if (adView != null) {
            adView.pause();
        }
        super.onPause();
    }

    /** Called before the activity is destroyed. */
    @Override
    public void onDestroy() {
        // Destroy the AdView.
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }

}

没有错误,但是当我运行应用程序时,它就停止了。

这是logcat:

09-20 17:24:53.341: E/dalvikvm(7651): adjustAdaptiveCoef max=6291456, min=1572864, ut=568
09-20 17:24:54.152: E/AndroidRuntime(7651): FATAL EXCEPTION: main
09-20 17:24:54.152: E/AndroidRuntime(7651): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.stiaica/com.example.stiaica.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.ActivityThread.access$700(ActivityThread.java:150)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.os.Looper.loop(Looper.java:137)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.ActivityThread.main(ActivityThread.java:5283)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at java.lang.reflect.Method.invoke(Method.java:511)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at dalvik.system.NativeStart.main(Native Method)
09-20 17:24:54.152: E/AndroidRuntime(7651): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
09-20 17:24:54.152: E/AndroidRuntime(7651):     at com.example.stiaica.MainActivity.onCreate(MainActivity.java:59)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.Activity.performCreate(Activity.java:5283)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
09-20 17:24:54.152: E/AndroidRuntime(7651):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
09-20 17:24:54.152: E/AndroidRuntime(7651):     ... 11 more
09-20 17:24:54.252: E/GooglePlayServicesUtil(7651): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.

我尝试使用Activity更改ActionBarActivity,但会出现更多错误。

现在正在工作,按钮正常工作。但是有一个问题,当我到达最后的传票时,我按下一个按钮,应用程序停止了。同样的事情是第一次发送。

logcat的:

09-20 18:08:56.915: E/dalvikvm(20667): adjustAdaptiveCoef max=4194304, min=1048576, ut=568
09-20 18:08:58.937: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:08:59.508: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:08:59.548: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:08:59.568: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:09:31.949: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:09:32.020: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:09:32.040: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:09:32.040: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:03.740: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:03.841: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:03.861: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:03.871: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:34.871: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:34.931: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:34.961: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:34.961: E/GooglePlayServicesUtil(20667): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
09-20 18:10:40.226: E/AndroidRuntime(20667): FATAL EXCEPTION: main
09-20 18:10:40.226: E/AndroidRuntime(20667): java.lang.ArrayIndexOutOfBoundsException: length=593; index=593
09-20 18:10:40.226: E/AndroidRuntime(20667):    at com.example.stiaica.MainActivity$2.onClick(MainActivity.java:73)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at android.view.View.performClick(View.java:4432)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at android.view.View$PerformClick.run(View.java:18338)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at android.os.Handler.handleCallback(Handler.java:725)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at android.os.Looper.loop(Looper.java:137)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at android.app.ActivityThread.main(ActivityThread.java:5283)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at java.lang.reflect.Method.invokeNative(Native Method)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at java.lang.reflect.Method.invoke(Method.java:511)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-20 18:10:40.226: E/AndroidRuntime(20667):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

您需要将生成的随机数存储在索引变量中,如此

  

移动下一个增量变量

     

向后移动

修改

以下是如何操作:

        public class MainActivity extends Activity {
        final int index = 0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final TextView tv = (TextView) findViewById(R.id.quote);
        Resources res = getResources();
        final myString = res.getStringArray(R.array.quote);
        final String q = myString[rgenerator.nextInt(myString.length)];
        tv.setText(q);
        Button btnNext = (Button) findViewById(R.id.btnNext);
        Button btnPrevious = (Button) findViewById(R.id.btnPrevious);
        Button btn = (Button) findViewById(R.id.btn);
        btnNext.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                String q = myString[index++];
                tv.setText(q);
            }
        });

        btnPrevious.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                String q = myString[index--];
                tv.setText(q);
            }
        });

       btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            myString = getResources().getStringArray(R.array.quote);
            index = rgenerator.nextInt(myString.length);
            String q = myString[index];
            tv.setText(q);
        }
    });

    }
    }}

答案 1 :(得分:0)

将所有文本放入arraylist。随机生成文本。对于下一个和后一个,获取随机生成的文本的位置,并为下一个增加位置+1,并将后退的位置减少1。

查看本文中的评论,它将为您提供如何使用arraylist作为文本的想法。希望它会对你有所帮助。干杯! :) Random text on button click