Android Studio App在蓝牙代码的活动开始时崩溃[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我刚刚在我的应用程序中添加了一个新代码,该代码应该通过蓝牙进入arduino来控制它...我对编程java很新。编译到目前为止还没有显示任何错误,所以我在我的设备上启动它,一旦打开包含蓝牙代码的Activity,它就会崩溃。您将在下面找到我的Activity和我的错误logcat:

我希望有人能帮帮忙 :)

BT_classic.Java:

    package com.car.bluetooth.bluetoothcar;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;



public class BT_Classic extends AppCompatActivity {

private Button pairedButton;
private Button discoveredButton;
private Button btonButton;
private Button btoffButton;
private ProgressDialog progress;
ListView listView;
BluetoothSocket bluetoothSocket;
BluetoothDevice bluetoothDevice;


private final static UUID uuid = UUID.fromString("fc5ffc49-00e3-4c8b-9cf1-6b72aad1001a");


private ArrayList<String> mDeviceList = new ArrayList<String>();

BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();


//BLUETOOTH VERBINDUNG


private static final int REQUEST_ENABLED = 0;
private static final int REQUEST_DISCOVERABLE = 0;

    private class ConnectingThread extends Thread {

        public ConnectingThread(BluetoothDevice device) {


            BluetoothSocket temp = null;
            BluetoothDevice bluetoothDevice = device;

            // Get a BluetoothSocket to connect with the given BluetoothDevice
            try {
                temp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
            } catch (IOException e) {
                e.printStackTrace();
            }
            bluetoothSocket = temp;

        }


            public void run() {
        // Cancel any discovery as it will slow down the connection
        btAdapter.cancelDiscovery();

        try {
            // This will block until it succeeds in connecting to the device
            // through the bluetoothSocket or throws an exception
            bluetoothSocket.connect();
        } catch (IOException connectException) {
            connectException.printStackTrace();
            try {
                bluetoothSocket.close();
            } catch (IOException closeException) {
                closeException.printStackTrace();
            }
        }

        // Code to manage the connection in a separate thread
    /*
        manageBluetoothConnection(bluetoothSocket);
    */
    }

    // Cancel an open connection and terminate the thread
    public void cancel() {
        try {
            bluetoothSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}








@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bt__classic);


    pairedButton = (Button) findViewById(R.id.pairedButton);
    discoveredButton = (Button) findViewById(R.id.discoveredButton);
    btonButton = (Button) findViewById(R.id.btonButton);
    btoffButton = (Button) findViewById(R.id.btoffButton);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String  itemValue = (String) listView.getItemAtPosition(position);
            String MAC = itemValue.substring(itemValue.length() - 17);
            BluetoothDevice bluetoothDevice = btAdapter.getRemoteDevice(MAC);
            // Initiate a connection request in a separate thread
            ConnectingThread t = new ConnectingThread(bluetoothDevice);
            t.start();
        }
    });

    //Pairing Button

    pairedButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();

            ArrayList<String> devices = new ArrayList<String>();

            for (BluetoothDevice bt : pairedDevices){
                devices.add(bt.getName());
                devices.add(bt.getAddress());

            }

            ArrayAdapter arrayAdapter = new ArrayAdapter(BT_Classic.this, android.R.layout.simple_list_item_1, devices);
            listView.setAdapter(arrayAdapter);
        }
    });








    discoveredButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(!btAdapter.isDiscovering()){
                Intent bton = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                startActivityForResult(bton, REQUEST_DISCOVERABLE);
            }


        }
    });

    btonButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent bton = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(bton, REQUEST_ENABLED);
        }
    });

    btoffButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btAdapter.disable();
        }
    });










}
}

错误Logcat:

2018-09-18 17:48:10.947 31085-31085/com.car.bluetooth.bluetoothcar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.car.bluetooth.bluetoothcar, PID: 31085
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.car.bluetooth.bluetoothcar/com.car.bluetooth.bluetoothcar.BT_Classic}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2830)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2909)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6592)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference
    at com.car.bluetooth.bluetoothcar.BT_Classic.onCreate(BT_Classic.java:121)
    at android.app.Activity.performCreate(Activity.java:6984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2783)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2909) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6592) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769) 
java android android-studio bluetooth android-bluetooth
1个回答
4
投票

你从未初始化你的listView变量。

确保它在onCreate()下初始化:

listView = (ListView) findViewById(R.id.whateveryourlistviewis);
© www.soinside.com 2019 - 2024. All rights reserved.