安卓工作室:期望类或接口

3
问题出现在下面代码的第102行,如标题所示。我对这个主题非常陌生,我正在尝试制作一个可以控制Arduino的应用程序...我展示给您的代码部分应该管理与蓝牙模块的连接,在我的MainActivity中,我计划将字母发送到其他设备。
希望有人能帮忙。
我的代码(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();
        }
    });










}
}

2
你粘贴的代码中第102行是空白行。你能否在有问题的地方添加注释,或者创建一个次要的、更小的代码块,这样我们至少知道我们要查找什么? - TheWanderer
我使用上面评论中提到的方法解决了我的问题,但现在我的应用程序一打开这个活动就会崩溃……您知道是为什么吗? - BlazeCodeDev
创建一个新问题并包含堆栈跟踪。 - TheWanderer
2个回答

1
在这个部分中,你多了一个}
public void cancel() {
        try {
            bluetoothSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        } // Close try
    } // Close cancel()
} // Close ConnectingThread

} // DELETE THIS CURLY BRACE

谢谢你的回答,但那不是问题所在。它标记了剩下的部分为红色,并显示“;”符号期望...所以我把它改回了我的版本。 - BlazeCodeDev
1
你尝试重新编译了吗?有时候IDE并不总是清除错误。你正在使用那个花括号关闭BT_Classic的代码块... - Chris Stillwell
好的谢谢,我已经重新编译了这个应用程序,没有错误 :D 但是现在当我打开这个 Activity 时,该应用程序会立即崩溃。 - BlazeCodeDev

1

首先,正如Chris所提到的那样,出现了额外的花括号问题。

其次,您尚未将ListView定义为listView = findViewById(R.id.your_id);

就是这样,祝你好运。 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接