我在Android Studio上做一个应用程序。 这是我的应用程序的工作方式。 (假设工作): 我有2个活动 在第一个我有8个CheckBox和1个按钮, 在第二个活动上,我得到了8个TextView。
所以......我试图从Checkbox中获取文本(如果已经选中)并将其发送到我的第二个活动(使用我的按钮),再发送到Textviews。
所以我想从我的FirstBox上的CheckBox中获取文本,并在第二个活动中将其发送到我的TextView。
一切都运行良好和完美,直到用户按下他的手机上的后退按钮,如果他要求他回到第一个活动,当他再次尝试再做时我得到一个错误,我的索引已经出来例外。
以下是代码:第一个活动:
public class MainActivity extends AppCompatActivity {
CheckBox[] MinhaCheckBox;
SharedPreferences Dados;
String MinhaPasta = "Pasta";
String valor = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn_Gerar(View v)
{
int IDTexBoz[] = {R.id.checkBox,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4,R.id.checkBox5,
R.id.checkBox6,R.id.checkBox7,R.id.checkBox8};
MinhaCheckBox = new CheckBox[IDTexBoz.length];
for(int i = 0 ; i < IDTexBoz.length; i++)
{
MinhaCheckBox[i] = (CheckBox)findViewById(IDTexBoz[i]);
}
for (int a= 0; a < MinhaCheckBox.length;a++)
{
if(MinhaCheckBox[a].isChecked()) {
valor += MinhaCheckBox[a].getText().toString() + ";";
}
}
Intent segundaTela = new Intent(this,Main2Activity.class);
segundaTela.putExtra("valor",valor);
startActivity(segundaTela);
}
}
第二个活动代码:
public class Main2Activity extends AppCompatActivity {
String MinhaPasta = "Pasta";
TextView[] MeusTextViews ;
String[] valor ;
String x = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
int MeusTextVieID[] = {R.id.textView,R.id.textView2,R.id.textView3,R.id.textView4,R.id.textView5,
R.id.textView6,R.id.textView7,R.id.textView8,};
MeusTextViews = new TextView[MeusTextVieID.length];
Bundle extras = getIntent().getExtras();
x = extras.getString("valor");
valor = x.split(";");
x = "";
for (int i = 0;i < MeusTextViews.length;i++)
{
MeusTextViews[i] = (TextView)findViewById(MeusTextVieID[i]);
}
for (int a = 0; a < valor.length;a++)
{
// here is where i got the error, when trying to do it for the second time(if you had pressed the back button on your phone)
MeusTextViews[a].setText(valor[a]);
}
}
}
这是错误:
Process: com.example.talisson.appsharedpreferences, PID: 5934
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.talisson.appsharedpreferences/com.example.talisson.appsharedpreferences.Main2Activity}: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8
at com.example.talisson.appsharedpreferences.Main2Activity.onCreate(Main2Activity.java:38)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:0)
在你的主要活动中,当你创建勇气时,你需要这样做:
valor = "";
for (int a= 0; a < MinhaCheckBox.length;a++)
{
if(MinhaCheckBox[a].isChecked()) {
valor += MinhaCheckBox[a].getText().toString() + ";";
}
}
//remove the last semicolon because when you split by ';' in second activity the array size would be one more that what you want. The last value being empty
if(valor.length()>0){
valor = valor.substring(0,valor.length()-1);
}
它与第一次或第二次无关。发生的事情是你第二次检查所有复选框。
答案 1 :(得分:0)
当您在活动1中再次按下该按钮时,数据将添加到valor变量中,因为它是全局的。因此,第二个活动中的valor数组比MeusTextViews长。
只需将活动1中的全局valor变量设为本地,因此每次按下按钮时都会创建它。
像这样:
public class MainActivity extends AppCompatActivity {
CheckBox[] MinhaCheckBox;
SharedPreferences Dados;
String MinhaPasta = "Pasta";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn_Gerar(View v)
{
String valor = "";
int IDTexBoz[] = {R.id.checkBox,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4,R.id.checkBox5,
R.id.checkBox6,R.id.checkBox7,R.id.checkBox8};
MinhaCheckBox = new CheckBox[IDTexBoz.length];
for(int i = 0 ; i < IDTexBoz.length; i++)
{
MinhaCheckBox[i] = (CheckBox)findViewById(IDTexBoz[i]);
}
for (int a= 0; a < MinhaCheckBox.length;a++)
{
if(MinhaCheckBox[a].isChecked()) {
valor += MinhaCheckBox[a].getText().toString() + ";";
}
}
Intent segundaTela = new Intent(this,Main2Activity.class);
segundaTela.putExtra("valor",valor);
startActivity(segundaTela);
}
}
答案 2 :(得分:0)
在第一个活动中,您可以执行此操作:
...
valor = "";
for (int a= 0; a < MinhaCheckBox.length;a++)
...
您要将字符串值添加到valor两次,并且index大于数组的长度。
编辑: 你只能在onCreate或调用函数
上执行此操作 int IDTexBoz[] = {R.id.checkBox,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4,R.id.checkBox5,
R.id.checkBox6,R.id.checkBox7,R.id.checkBox8};
MinhaCheckBox = new CheckBox[IDTexBoz.length];
for(int i = 0 ; i < IDTexBoz.length; i++)
{
MinhaCheckBox[i] = (CheckBox)findViewById(IDTexBoz[i]);
}