我放弃了。我经常使用子串,但这次我不能使它工作。我正在从微调器中选择一个联系人(填充了手机的联系人),我想要所选联系人的姓氏。部分代码:
public class MyOnItemSelectedListener实现OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
selectedname = parent.getItemAtPosition(pos).toString();
sn = selectedname .indexOf(' ');
String selectedname2= selectedname.substring(1, 3); //force close line
}
public void onNothingSelected(AdapterView parent) {
}
}
selectedname和sn变量返回实际值。 例如在
BtnOK = (Button)findViewById(R.id.Button01);
BtnOK.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(Alarm.this, "selname: " + selectedname + "-" + sn, Toast.LENGTH_LONG).show();
}
});
返回selname:Anna-4
但如果我包含String selectedname2= selectedname.substring(1, 3);
行,则会强行关闭。
有什么想法吗?
logcat的:
07-04 17:05:32.140: ERROR/AndroidRuntime(280): FATAL EXCEPTION: main
07-04 17:05:32.140: ERROR/AndroidRuntime(280): java.lang.StringIndexOutOfBoundsException
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at java.lang.String.substring(String.java:1579)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at com.bfarago.app.Alarm$MyOnItemSelectedListener.onItemSelected(Alarm.java:577)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at android.widget.AdapterView.access$200(AdapterView.java:42)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at android.os.Handler.handleCallback(Handler.java:587)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at android.os.Handler.dispatchMessage(Handler.java:92)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at android.os.Looper.loop(Looper.java:123)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at java.lang.reflect.Method.invoke(Method.java:521)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-04 17:05:32.140: ERROR/AndroidRuntime(280): at dalvik.system.NativeStart.main(Native Method)
第577行是String selectedname2= selectedname.substring(1, 3);
这一行是String selectedname2= selectedname.substring(1, sn);
,我只想弄清楚出了什么问题。
编辑2:整个代码:
public class Alarm extends Activity {
public ArrayList<String> myArr = new ArrayList<String>();
String contactName;
int spaceIndex, spaceLastIndex, spaceIndex2;
String selectedname;
String selectedname2;
int sn;
int selnamelength;
String[] words;
String surname;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);
contactName = null;
Context context = getApplicationContext();
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
spaceIndex = contactName.indexOf(' ');
spaceLastIndex = contactName.lastIndexOf(' ');
if (spaceIndex > 0) {myArr.add(contactName);}
else if (spaceLastIndex > 0) {myArr.add(contactName);}
else {myArr.add(contactName);}
}
myArr.add("");
Collections.sort(myArr);
Spinner sp1 = (Spinner)findViewById(R.id.Spinner01);
final ArrayAdapter adapter3 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, myArr);
sp1.setAdapter(adapter3);
sp1.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
selectedname = parent.getItemAtPosition(pos).toString();
sn = selectedname.indexOf(' ');
selnamelength = selectedname.length();
selectedname2 = selectedname.substring(1, 3); //force closes
if (!selectedname.equals(""))
{
Toast.makeText(Alarm.this, "selectedname.length = " + selnamelength, Toast.LENGTH_LONG).show(); //This is working
}
}
public void onNothingSelected(AdapterView parent) {
}
}
}
顺便说一句,split()函数正在运行......
答案 0 :(得分:1)
试试这段代码:
String selectedname2 = new String(selectedname.substring(1, 3));
答案 1 :(得分:0)
你需要这个吗?
String selectedname2= selectedname.substring(sn, selectedname.length());
还尝试记录您的异常,查看LogCat或放置一个try / catch块并记录您的异常以提供更多详细信息。
[被修改]
请复制您的所有活动代码,在活动范围内声明selectedname var。尝试在方法范围中声明此var,似乎在您的活动的另一部分中修改了此var。
你得到一个IndexOutOfBoundException:
原因是因为在#577行之前更改了selectedname内容。首先尝试在方法范围内声明此var,然后复制并粘贴完整活动代码,以便我可以看到此var的使用位置。 请记住,子字符串方法共享string's backing array,因此子字符串中的任何修改都会影响原始字符串,然后是字符串数组。