现在我正在开发一个应用程序,它可以在按钮点击时更改StringArray中的项目。我已经实现了左右按钮,当我单击右键时,项目向右更改,当我按左键时,项目更改为左侧。到目前为止一切都很好,但是当我在史蒂夫乔布斯的项目上点击左键时,应用程序强制关闭。知道为什么会这样吗?
的strings.xml:
<string name="sj">Steve Jobs</string>
<string name="mz">Mark Zuckerberg</string>
<string name="bg">Bill Gates</string>
<string name="lp">Larry Page</string>
<string-array name="gl">
<item>@string/sj</item>
<item>@string/mz</item>
<item>@string/bg</item>
<item>@string/lp</item>
</string-array>
MainActivity.java:
package com.example.theatremode;
import android.support.v7.app.ActionBarActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
公共类MainActivity扩展ActionBarActivity实现OnClickListener {
int counter = 0;
TextView tv1;
Button button1;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
Resources res = getResources();
final String[] list = res.getStringArray(R.array.gl); // get the array
((TextView) findViewById(R.id.tv1)).setText(list[counter]); // set the
// initial
// message.
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.tv1);
counter++;
if (counter >= list.length)
counter = 0;
tx.setText(list[counter]); // set the new message.
}
});
Resources res2 = getResources();
final String[] list2 = res2.getStringArray(R.array.gl); // get the array
((TextView) findViewById(R.id.tv1)).setText(list2[counter]);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView tx2 = (TextView) findViewById(R.id.tv1);
counter--;
if (counter >= list2.length)
counter = 0;
tx2.setText(list2[counter]); // set the new message.
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
activity_main.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: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="com.example.theatremode.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:id="@+id/tv1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/tv1"
android:text="Left" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="48dp"
android:layout_marginRight="14dp"
android:text="Right" />
答案 0 :(得分:2)
看起来你有IndexOutOfBoundsException
。
这里发生了什么并不明显,但这可能会解决它:
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView tx2 = (TextView) findViewById(R.id.tv1);
counter--;
//if (counter >= list2.length)
//counter = 0;
if (counter < 0)
counter = list2.length - 1;
tx2.setText(list2[counter]); // set the new message.
}
});