我想显示android蓝牙api发现的蓝牙设备列表,但我不知道为什么会出现这个错误:
FATAL EXCEPTION:main java.lang.RuntimeException:无法启动 活动 ComponentInfo {com.example.fatma.listviewcst / com.example.fatma.listviewcst.MainActivity}: 显示java.lang.NullPointerException
MainActivity.java
package com.example.fatma.listviewcst;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
BluetoothDev bluetoothDev;
ArrayList<BluetoothDevice> mArrayAdapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
displayDiscovry();
}
public void displayDiscovry(){
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast t = new Toast(this);
t.setText("Sorry your phone do not support Bluetooth");
t.show();
} else {
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,1);}
// bluetoothDev=new BluetoothDev();
//added
// Create a BroadcastReceiver for ACTION_FOUND
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device);
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
//end added
listView=(ListView)findViewById(R.id.listView);
devList listeDev=new devList(this,R.layout.item,mArrayAdapter);
listView.setAdapter(listeDev);}
}
}
devList.java
package com.example.fatma.listviewcst;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by FATMA on 02/04/2016.
*/
public class devList extends ArrayAdapter<BluetoothDevice>{
Context context;
int resource;
public devList(Context context, int resource, List<BluetoothDevice> bluetoothDev) {
super(context, resource, bluetoothDev);
this.context=context;
this.resource=resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//inflater charge le contenu d un fichier ds une vue
LayoutInflater inflater =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
//false pour dire non pas attaché au root
View view =inflater.inflate(resource,parent,false);
TextView devName= (TextView) view.findViewById(R.id.devName);
TextView devMac= (TextView) view.findViewById(R.id.devMac);
devName.setText( getItem(position).getName());
devMac.setText(getItem(position).getAddress());
return view;
}
}
list_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/devName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/devMac" />
</LinearLayout>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.fatma.listviewcst.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
答案 0 :(得分:0)
编辑:这已被接受,但答案不正确。
问题是由mArrayAdapter
在调用add
之前未初始化造成的。
如果没有看到更多的堆栈跟踪,很难说出问题归结为什么。
但我建议您可能会因为注释掉代码setContentView(R.layout.activity_main);
而导致这种情况。我建议这样做是因为只要您尝试连接到蓝牙以列出您调用listView=(ListView)findViewById(R.id.listView);
的设备,但是没有根视图可以查找任何视图。