gpt4 book ai didi

java - ListView.setAdapter(ArrayAdapter) 使应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 12:18:37 25 4
gpt4 key购买 nike

我正在尝试使用配对的蓝牙设备填充 ListView 。我尝试在 MainActivity 中使用 ListView 执行此操作,效果非常好。但是,当我在不同的 Activity 中使用 ListView 尝试它时,它使应用程序崩溃了。我基本上想在弹出对话框中填充 ListView。

这是代码:

activity_device_list.xml(主 Activity )

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">

<ListView
android:id="@+id/listDevicesMain"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

device_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.12"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Paired Devices"
android:textSize="25sp" />

<ListView
android:id="@+id/listDevicesDialog"
android:layout_width="match_parent"
android:layout_height="395dp"
android:layout_weight="0.38" />
</LinearLayout>

DeviceList.java

package example.btmodule;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;

import static example.btmodule.R.layout.activity_device_list;

public class DeviceList extends AppCompatActivity {

ListView devicelist;

private BluetoothAdapter myBluetooth = null;
private Set<BluetoothDevice> pairedDevices;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_device_list);

myBluetooth = BluetoothAdapter.getDefaultAdapter();

if(myBluetooth == null)
{
//Show a mensag. that thedevice has no bluetooth adapter
Toast.makeText(getApplicationContext(), R.string.bluetooth_unavailable, Toast.LENGTH_LONG).show();
//finish apk
finish();
}
else {
if (myBluetooth.isEnabled()) {
} else {
//Ask to the user turn the bluetooth on
Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnBTon, 1);
}
}
}

private void pairedDevicesList()
{
pairedDevices = myBluetooth.getBondedDevices();
ArrayList list = new ArrayList();

devicelist = (ListView)findViewById(R.id.listDevicesDialog);

if (pairedDevices.size()>0)
{
for(BluetoothDevice bt : pairedDevices)
{
list.add(bt.getName() + "\n" + bt.getAddress()); //Get the device's name and the address
}
}
else
{
Toast.makeText(getApplicationContext(), R.string.no_devices_found, Toast.LENGTH_LONG).show();
}

final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
devicelist.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

// menu item selection
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_connect:
showDialog();
pairedDevicesList();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// show the dialog for connected devices
private void showDialog(){
AlertDialog.Builder mBuilder = new AlertDialog.Builder(DeviceList.this);
View mView = getLayoutInflater().inflate(R.layout.device_dialog, null);
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
}

}

如果我将 devicelist 更改为 R.id.listDevicesMain 代码工作得很好。

最佳答案

您调用了错误的 ListView ,您给出的 ListView 的 id 是

 <ListView
android:id="@+id/listDevicesMain"
android:layout_width="match_parent"
android:layout_height="match_parent" />

并且您正在尝试使用 id 调用 ListView devicelist = (ListView)findViewById(R.id.listDevicesDialog);希望这能解决问题。

关于java - ListView.setAdapter(ArrayAdapter) 使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45969087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com