BluetoothAdapter.getDefaultAdapter()返回null

时间:2016-02-04 15:51:17

标签: android android-fragments bluetooth

我是Android编程的新手,我的应用在启动时会立即崩溃。 当它打开时它应该显示我配对的蓝牙设备,当我选择一个它应该将我移动到另一个片段(PresetsFragment)。

package rcontrol.fragments;

import android.app.Fragment;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

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

import rauhalamika.rcontrol_v1.R;

/**
 * Created by rauhalamika on 03/02/2016.
 */
public class MainFragment extends Fragment {

    ListView devicelist;

    private BluetoothAdapter myBluetooth = null;
    private Set<BluetoothDevice> pairedDevices;
    public static String EXTRA_ADDRESS = "device_address";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.content_main, container,false);
        ListView lv = (ListView) rootView.findViewById(R.id.listView);

        myBluetooth = BluetoothAdapter.getDefaultAdapter();
        if (myBluetooth == null){
            Toast.makeText(getActivity(), "Bluetooth Not Turned On", Toast.LENGTH_LONG).show();
            getActivity().finish();
        } else if (!myBluetooth.isEnabled()){
            Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(turnBTon, 1);
        }

        pairedDevices = myBluetooth.getBondedDevices();
        ArrayList list = new ArrayList();

        if (pairedDevices.size() > 0){
            for (BluetoothDevice bt : pairedDevices){
                list.add(bt.getName() + "\n" + bt.getAddress());
            }
        } else{
            Toast.makeText(getActivity(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_SHORT).show();
        }

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

        return rootView;
    }

    private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener(){
        public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3){
            String info = ((TextView) v).getText().toString();
            String address = info.substring(info.length() - 17);

            Intent i = new Intent(getActivity(), PresetsFragment.class);
            i.putExtra(EXTRA_ADDRESS, address);
            startActivity(i);
        }
    };
}

这是我的错误:

02-04 17:19:50.726 4349-4349/rauhalamika.rcontrol_v1 E/BluetoothAdapter: Bluetooth binder is null
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 D/AndroidRuntime: Shutting down VM
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime: FATAL EXCEPTION: main
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime: Process: rauhalamika.rcontrol_v1, PID: 4349
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{rauhalamika.rcontrol_v1/rauhalamika.rcontrol_v1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Set android.bluetooth.BluetoothAdapter.getBondedDevices()' on a null object reference
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Set android.bluetooth.BluetoothAdapter.getBondedDevices()' on a null object reference
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at rcontrol.fragments.MainFragment.onCreateView(MainFragment.java:52)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.Fragment.performCreateView(Fragment.java:2220)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.BackStackRecord.run(BackStackRecord.java:793)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.FragmentController.execPendingActions(FragmentController.java:325)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.Activity.performStart(Activity.java:6252)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
02-04 17:19:51.109 4349-4349/rauhalamika.rcontrol_v1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
02-04 17:24:51.120 4349-4349/? I/Process: Sending signal. PID: 4349 SIG: 9

我可以看到我的getBondedDevices返回null,但是我怎么能超越它?

0 个答案:

没有答案