【问题标题】:How to get ArrayAdapter adapters into an ArrayList?如何将 ArrayAdapter 适配器放入 ArrayList?
【发布时间】:2011-12-02 16:29:27
【问题描述】:

我正在尝试制作一个应用程序来检测附近可发现的蓝牙设备,并将所有 mac 地址放入一个列表中。由于我不明白的原因,如果我尝试使用列表存储来自 device.getAddress() 的字符串,应用程序会崩溃。

在未能将这些地址放入 List 后,我​​放弃并使用了 ArrayAdapter,发现它已在某些示例代码中使用。我的想法是也许我可以将结果放入 ArrayAdapter,然后从中提取地址,然后放入 myList。还是不行。据我所知,如果我的应用程序崩溃,我就无法使用列表...

当我注释掉与 myList 相关的任何内容时,应用程序不会崩溃。

/* these are declared as class members */
//private ArrayAdapter<String> myArrayAdapter;
//private List<String> myList;
/*-------------------------------*/

private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        int position = 0;

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            myArrayAdapter.add(device.getAddress());
            //myList.add(device.getAddress()); 
            //position++;  //SO USING THIS INTEGER...

        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {             
            btnLocate.setText("Refresh Location");
            btnLocate.setClickable(true);
            //position--;
            //while(position>=0){
              //myList.add(myArrayAdapater.getItem(position).toString()); 
              //position--;
            //}
        }
    }
};

LogCat:

12-02 12:01:54.472: E/AndroidRuntime(16043): FATAL EXCEPTION: main
12-02 12:01:54.472: E/AndroidRuntime(16043): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND (has extras) } in com.ronnyszutu.lpf.LPFMainActivity$1@40519840
12-02 12:01:54.472: E/AndroidRuntime(16043): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
12-02 12:01:54.472: E/AndroidRuntime(16043): at android.os.Handler.handleCallback(Handler.java:587)

【问题讨论】:

  • 我是……这方面的菜鸟……什么是 logcat,我怎样才能得到它?
  • 在此处发布您的例外情况。转到 DDMS(即窗口 > 显示视图 > 其他 > DDMS)并选择红色的日志猫线,复制并粘贴到此处。
  • 12-02 12:01:54.472: E/AndroidRuntime(16043): 致命异常: main 12-02 12:01:54.472: E/AndroidRuntime(16043): java.lang.RuntimeException:在 com.ronnyszutu.lpf.LPFMainActivity$1@40519840 12-02 12:01:54.472 中接收广播 Intent { act=android.bluetooth.device.action.FOUND (has extras) } 时出错:E/AndroidRuntime(16043):在 android .app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722) 12-02 12:01:54.472: E/AndroidRuntime(16043): at android.os.Handler.handleCallback(Handler.java:587)跨度>
  • 12-02 12:01:54.472: E/AndroidRuntime(16043): 在 android.os.Handler.dispatchMessage(Handler.java:92) 12-02 12:01:54.472: E/ AndroidRuntime(16043): 在 android.os.Looper.loop(Looper.java:130) 12-02 12:01:54.472: E/AndroidRuntime(16043): 在 android.app.ActivityThread.main(ActivityThread.java:3683 )

标签: java android android-arrayadapter


【解决方案1】:

在尝试将设备地址添加到列表之前,您确实初始化了列表,对吗?此外,您可能想了解 Receiver 生命周期,因为开发人员文档讨论了对象的有效期。 BroadcastReceiver#ReceiverLifecycle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多