简体   繁体   English

Android substring 强制关闭

[英]Android substring force closes

I give up.我放弃。 I use substring a lot but this time I cannot make it work.我经常使用 substring 但这次我不能让它工作。 I am selecting a contact from a spinner (that is populated with the contacts of the phone) and I want the surname of that selected contact.我正在从微调器中选择一个联系人(其中填充了手机的联系人),并且我想要该选定联系人的姓氏。 Part of the code:部分代码:

public class MyOnItemSelectedListener implements OnItemSelectedListener {公共 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) {

}

} }

The selectedname and sn variables return real values. selectedname 和 sn 变量返回实际值。 eg the例如

    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();

    }
});

returns selname: Anna-4返回 selname:Anna-4

But if i include the String selectedname2= selectedname.substring(1, 3);但是如果我包含String selectedname2= selectedname.substring(1, 3); line, it force closes.线,它强制关闭。 Any ideas?有任何想法吗?

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)

Line 577 is String selectedname2= selectedname.substring(1, 3);第 577 行是String selectedname2= selectedname.substring(1, 3);

This line would be String selectedname2= selectedname.substring(1, sn);这一行将是String selectedname2= selectedname.substring(1, sn); , i was just trying to figure out what is wrong. ,我只是想找出问题所在。

Edit2: Whole code:编辑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) {

    }
}
}

By the way, split() function is working...顺便说一句, split() function 正在工作......

try this code:试试这个代码:

String selectedname2 = new String(selectedname.substring(1, 3));

Do you need this?你需要这个吗?

String selectedname2= selectedname.substring(sn, selectedname.length());

also try to log your exception, look at LogCat or put a try/catch block and Log your exception to give us more details.还尝试记录您的异常,查看 LogCat 或放置一个 try/catch 块并记录您的异常以向我们提供更多详细信息。

[Edited] [已编辑]

Please copy all your activity code, the selectedname var is declared at the activity scope.请复制所有活动代码,选定名称变量在活动 scope 中声明。 Try to declare this var in the method scope, it seems that this var is being modified in another part of your activity.尝试在方法 scope 中声明此 var,看来此 var 正在您活动的另一部分中进行修改。

You're getting an IndexOutOfBoundException:你得到一个 IndexOutOfBoundException:在此处输入图像描述

So the reason is because the selectedname content was changed before the line#577.所以原因是因为在#577行之前更改了selectedname的内容。 First try to declare this var at method scope, then copy&paste your fully activity code so I can see where this var is used.首先尝试在方法 scope 中声明此 var,然后复制并粘贴您的完整活动代码,以便我可以查看此 var 的使用位置。 Remember that a substring method shares thestring's backing array , so any modification in the substring will affect your original string, an then your string array.请记住,substring 方法共享字符串的支持数组,因此 substring 中的任何修改都会影响您的原始字符串,然后是您的字符串数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM