为什么我不能导入一个字符串数组,因为日志中有一个错误

我正在尝试设置一个按钮,使用数组(概念类似于基于文本的冒险游戏)显示对话框的下一行,但是当我导入.xml时,这只是一个存储所有对话框的简单按钮。onClick


package com.example.project;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageButton;

import android.widget.TextView;


import java.util.ArrayList;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button btn_message;

String[] messages;

    int index = 0;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        //Importing values

        messages = getResources().getStringArray(R.array.messages);

        btn_message=findViewById(R.id.btn_message);


        //set Value to varibles


        btn_message.setOnClickListener(this);



    }


    @Override

    public void onClick(View v) {

        index++;

        btn_message.setText(messages [index]);

    }

}

如果需要日志:


java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.CharSequence

        at com.example.project.MainActivity.onClick(MainActivity.java:34)

        at android.view.View.performClick(View.java:6256)

        at android.view.View$PerformClick.run(View.java:24701)

        at android.os.Handler.handleCallback(Handler.java:789)

        at android.os.Handler.dispatchMessage(Handler.java:98)

        at android.os.Looper.loop(Looper.java:164)

        at android.app.ActivityThread.main(ActivityThread.java:6541)

        at java.lang.reflect.Method.invoke(Native Method)

        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

素胚勾勒不出你
浏览 77回答 3
3回答

HUX布斯

该方法接收一个字符串,它是CharSequence的实现,当您这样做时,您将获得一个 ArrayList,其中包含元素中行的每个单元格,因此是一个数组数组。Button#setText()messages[index]您应该将它们收集在一个字符串中。如果列表为 ,则一种方法是:List<String>String stringMessage = String.join(", ", messages[index]);然后,您应该能够毫无例外地制作cal:setText()@Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; index++;&nbsp; &nbsp; &nbsp; &nbsp; String stringMessage = String.join(", ", messages[index]);&nbsp; &nbsp; &nbsp; &nbsp; btn_message.setText(stringMessage);&nbsp; &nbsp; }像这样,您将用逗号和空格将它们连接起来,如果您想以某种方式加入它们,只需在,#join()如果数组列表不是字符串,则可以使用 lambda 执行相同的操作:String messageString = messages[index].stream().map(Object::toString)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.joining(", "));

守候你守候我

您可以将其设置为遵循条件,如果 onClick 以避免超出范围异常public class MainActivity extends AppCompatActivity implements View.OnClickListener {Button btn_message;String[] messages;int index = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; messages = getResources().getStringArray(R.array.messages);&nbsp; &nbsp; btn_message=findViewById(R.id.btn_message);&nbsp; &nbsp; //set Value to varibles&nbsp; &nbsp; btn_message.setText(messages[index++]);&nbsp; &nbsp; btn_message.setOnClickListener(this);}@Overridepublic void onClick(View v) {&nbsp; &nbsp; if(index < messages.length) {&nbsp; &nbsp; &nbsp; &nbsp; btn_message.setText(messages[index]);&nbsp; &nbsp; &nbsp; &nbsp; index++;&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; index = 0;&nbsp; &nbsp; &nbsp; &nbsp; btn_message.setText(messages[index++]);&nbsp; &nbsp; }}}

GCT1015

试试这个可能会有所帮助index++;&nbsp;btn_message.setText(messages&nbsp;[index].toString());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java