开发者

Android substring force closes

开发者 https://www.devze.com 2023-03-17 09:30 出处:网络
I give up. I use substring a lot but this time I 开发者_如何学JAVAcannot make it work. I am selecting a contact from a spinner (that is populated with the contacts of the phone) and I want the surname

I give up. I use substring a lot but this time I 开发者_如何学JAVAcannot make it work. 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 {

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. e.g. 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

But if i include the 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);

This line would be String selectedname2= selectedname.substring(1, sn);, i was just trying to figure out what is wrong.

Edit2: Whole code:

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...


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.

[Edited]

Please copy all your activity code, the selectedname var is declared at the activity scope. Try to declare this var in the method scope, it seems that this var is being modified in another part of your activity.

You're getting an IndexOutOfBoundException:

Android substring force closes

So the reason is because the selectedname content was changed before the line#577. 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. Remember that a substring method shares the string's backing array, so any modification in the substring will affect your original string, an then your string array.

0

精彩评论

暂无评论...
验证码 换一张
取 消