提问人:edgar_wideman 提问时间:11/26/2020 最后编辑:edgar_wideman 更新时间:11/26/2020 访问量:50
阵列适配器初始化后为 null(错误的布局文件)
Array Adapter is null after initialized (wrong layout file)
问:
我的应用程序因为一个愚蠢的原因而不断崩溃!我环顾四周,有类似问题的用户正在使用片段,而我正在使用 Activities。我在 on create 方法中的代码如下。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_select_contact);
setResult(Activity.RESULT_CANCELED);
ArrayAdapter<String> mPairedDevicesArrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.device_name);
ListView mPairedListView = findViewById(R.id.paired_devices);
mPairedListView.setAdapter(mPairedDevicesArrayAdapter);
mPairedListView.setOnItemClickListener(mDeviceClickListener);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter.getBondedDevices();
if (mPairedDevices.size() > 0) {
/* List of all paired devices */
findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
for (BluetoothDevice mDevice : mPairedDevices) {
mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n" + mDevice.getAddress());
}
} else {
/* No paired device */
String mNoDevices = "None Paired";
叙述如下,
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' null 对象引用 在ca.edgarwideman.taxibuddy.ConnectPrinter.onCreate(ConnectPrinter.java:39)
第 39 行(发生错误的地方)是这样的,
mPairedListView.setAdapter(mPairedDevicesArrayAdapter);
我明白😋了,答案在下面......
答:
1赞
edgar_wideman
11/26/2020
#1
明白了!我几乎不好意思承认......😌 始终确保您的 OnCreate 启动正确的布局文件!
setContentView(R.layout.activity_select_contact);
应该是
setContentView(R.layout.activity_connect_printer);
谢谢伙计们帮助我! 归功于@ianhanniballake
评论
ListView
activity_select_contact.xml