我的Android应用有问题。我尝试从另一个类获取一个变量,并将其用作if语句的条件,这是我用来获取变量的代码:
发送变量(是一个字符串):
Intent intent = new Intent(context, Example1.class);
intent.putExtra("categ", result);
接收变量:
final String[] pos_categ ;
pos_categ = intent1.getExtras().getStringArray("categ");
这里我在if语句中声明了我想要更改的3个变量:
String[] title = null;
String[] eng = null;
final String[] dan = null;
这些是我从中获取title,eng和dan变量数据的字符串:
String [] lessons_titles = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","30","40","50","60","70","80","90"};
String [] eng_version = {"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
String[] dan_version = {"En","To","Tre","Fire","Fem","Seks","Syv","Otte","Ni","Ti","Elleve","Tolv","Tretten","Fjorten","Femten","Seksten","Sytten","Atten","Nitten","Tyve","Tredive","Fyrre","Halvtreds","Tres","Halvfjerds","Firs","Halvfems"};
String [] lessons_titles2 = {"Hej, jeg hedder Khalid","Hvad hedder du?","Jeg hedder Ellen"};
String [] eng_version2 = {"Hej, jeg hedder Khalid","Hvad hedder du?","Jeg hedder Ellen"};
String[] dan_version2 = {"Hej, jeg hedder Khalid","Hvad hedder du?","Jeg hedder Ellen"};
String [] lessons_titles3 = {"Hvor kommer du fra?","Jeg kommer fra Australien.","Hvor kommer du fra?","Jeg kommer dra Moroko."};
String [] eng_version3 = {"Hvor kommer du fra?","Jeg kommer fra Australien.","Hvor kommer du fra?","Jeg kommer dra Moroko."};
String[] dan_version3 = {"Hvor kommer du fra?","Jeg kommer fra Australien.","Hvor kommer du fra?","Jeg kommer dra Moroko."};
String [] lessons_titles4 = {"Hvad sprog taler du?","Jeg taler engelsk.","Hvad med dig?","Jeg taler fransk og arabisk."};
String [] eng_version4 = {"Hvad sprog taler du?","Jeg taler engelsk.","Hvad med dig?","Jeg taler fransk og arabisk."};
String[] dan_version4 = {"Hvad sprog taler du?","Jeg taler engelsk.","Hvad med dig?","Jeg taler fransk og arabisk."};
String [] lessons_titles5 = {"Hvor gammel er du?","Jeg er 22."};
String [] eng_version5 = {"Hvor gammel er du?","Jeg er 22."};
String[] dan_version5 = {"Hvor gammel er du?","Jeg er 22."};
String [] lessons_titles6 = {"Hvor bor du?","Jeg bor i Vestergade.","Hvor i Vestergade?","I nummer 38."};
String [] eng_version6 = {"Hvor bor du?","Jeg bor i Vestergade.","Hvor i Vestergade?","I nummer 38."};
String[] dan_version6 = {"Hvor bor du?","Jeg bor i Vestergade.","Hvor i Vestergade?","I nummer 38."};
这里我使用if语句根据" pos_categ"的值来改变变量。我尝试将pos_categ的第一个值与我已经拥有的某些字符串的第一个值进行比较:
if (pos_categ[0].equals(lessons_titles1[position[0])){
title[0] = lessons_titles[position[0]];
eng[0] = eng_version[position[0]];
dan[0] = dan_version[position[0]].toLowerCase();
}
else if (pos_categ[0].equals(lessons_titles2[position[0])){
title[0] = lessons_titles2[position[0]];
eng[0] = eng_version2[position[0]];
dan[0] = dan_version2[position[0]].toLowerCase();
}
else if (pos_categ[0].equals(lessons_titles3[position[0])){
title[0] = lessons_titles3[position[0]];
eng[0] = eng_version3[position[0]];
dan[0] = dan_version3[position[0]].toLowerCase();
}
else if (pos_categ[0].equals(lessons_titles4[position[0])){
title[0] = lessons_titles4[position[0]];
eng[0] = eng_version4[position[0]];
dan[0] = dan_version4[position[0]].toLowerCase();
}
else if (pos_categ[0].equals(lessons_titles5[position[0])){
title[0] = lessons_titles5[position[0]];
eng[0] = eng_version5[position[0]];
dan[0] = dan_version5[position[0]].toLowerCase();
}
else if (pos_categ[0].equals(lessons_titles6[position[0])){
title[0] = lessons_titles6[position[0]];
eng[0] = eng_version6[position[0]];
dan[0] = dan_version6[position[0]].toLowerCase();
}
title_view.setText(title[0]);
eng_view.setText(eng[0]);
dan_view.setText(dan[0]);
问题在于,当我打开应用程序时,我应该根据变量pos_categ获得正确的信息。相反,我得到了这个错误:
Unfortunately, test has stopped working.
Blame the Split Screen.
答案 0 :(得分:0)
最终变量只能初始化一次。您正在声明“final String [] pos_categ”,这意味着您无法更改该值。 String []也表示java中的String数组。
我的理解是你要传递一个字符串并对其进行评估,如果是这样,你[]为pos_categ变量。
或者,如果你想在android中的活动之间传递数组,你应该执行以下操作:
Passing string array between android activities。
我不知道这是否会解决你的问题,因为我没有看到“不幸的是,测试已停止工作。责怪分屏。”来自。
修改强>:
要解决你的if问题,你需要找出结果和位置的值,然后看看它们是否应该是什么。您可以使用toast,或使用eclipse调试器(如果您使用的是eclipse)来查找。
但是这里的if不是真正的问题,无论如何它是你的比较。当你应该使用STRINGS时,你正在使用STRING ARRAY(“String [] title”)(如:“String title”)。 docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
编辑2
int position = getIntent().getExtras().getInt("position");
String[] pos_categ ;
pos_categ = intent1.getExtras().getString("categ");
-
String title = "";
String eng = "";
String dan = "";
-
if (pos_categ.equals(lessons_titles1[position)){
title = lessons_titles[position];
eng = eng_version[position];
dan = dan_version[position].toLowerCase();
}
//else if....