在我的申请中,使用从蓝色帽的例子中略微修改的一类装置法。 该法典在测试的所有装置上运行良好,但一经公布,一开始收到类似报告:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1555)
at sword.games.highnoonf.activities.DeviceListActivity$1.onItemClick(DeviceListActivity.java:18 0)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3388)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1738)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:5097)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
造成例外的职能是:
private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
// Cancel discovery because it s costly and we re about to connect
mBtAdapter.cancelDiscovery();
// Get the device MAC address, which is the last 17 chars in the View
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
String devName = info.substring(0, info.length() - 17);
// Create the result Intent and include the MAC address
Intent intent = new Intent();
intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
intent.putExtra("DEVICE_NAME", devName);
// Set result and finish this Activity
setResult(Activity.RESULT_OK, intent);
finish();
}
};
The rest of the code is almost identical to the DeviceListActivity example... I hope someone can fix this or give me an explanation of why this happens... Thank you...
Edit:该法典已经检查了MAC地址的名称,其长度与这些行文相符(如果电话没有添加错误,用户可以点击):
String name = device.getName();
String address = device.getAddress();
if(name == null) name = "Unknown";
else if (name.length() == 0) name = "Unknown";
if (address.length() == 17){
mNewDevicesArrayAdapter.add(name + "
" + address);
}