English 中文(简体)
StringOutOfBoundException in Bluetooth Chat Example
原标题:StringOutOfBoundException in BluetoothChat Example
  • 时间:2012-04-11 19:55:21
  •  标签:
  • java
  • android

在我的申请中,使用从蓝色帽的例子中略微修改的一类装置法。 该法典在测试的所有装置上运行良好,但一经公布,一开始收到类似报告:

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);
}
问题回答

info.length(),小于17。 确保substring 方法不会作为参数获得任何负值。 在try/ submissions栏中标明你的代码的关键部分,以避免出现例外情况。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签