我正在尝试设置一个按钮,使用数组(概念类似于基于文本的冒险游戏)显示对话框的下一行,但是当我导入.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)
HUX布斯
守候你守候我
GCT1015
相关分类