这里是我的代码,我设置计数值为-1。我只给下一个按钮计数了<。并为prev按钮计数> = 0。另外声明(Toast消息)不起作用。请帮我解决这个错误。我得到索引超出限制的异常和吐司消息的错误未来
public class story extends AppCompatActivity {
Button next;
Button prev;
TextView t;
int count=-1;
int[] stories = {
R.string.firststory,
R.string.story2,
R.string.story3,
R.string.story4,
R.string.story5,
R.string.story6,
R.string.story7,
R.string.story8,
R.string.story9,
R.string.story10,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.story);
next = (Button) this.findViewById(R.id.next);
prev = (Button) this.findViewById(R.id.prev);
t = (TextView) this.findViewById(R.id.textView2);
t.setText(Html.fromHtml(getString(stories[0])));
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
count=count+1;
try{
if(count<10) {
t.setText(Html.fromHtml(getString(stories[count])));
}
else{
Toast toast=Toast.makeText(getApplicationContext(),"This is Last story",Toast.LENGTH_SHORT);
}
}
catch(Exception e)
{
}
}
});
prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
count=count-1;
try{
if(count>=0)
{
t.setText(Html.fromHtml(getString(stories[count])));
}
else{
Toast toast=Toast.makeText(getApplicationContext(),"This is first story",Toast.LENGTH_SHORT);
}
}
catch(Exception e){
}
}
});
}
}
请提供logcat错误 – Malik
吐司没有显示,因为你从来没有打过电话toast.show() – Malik