我是目标编程的初学者,我不知道该怎么办。我在openBT方法中使用空指针得到一些错误。下面是我的logcat和代码。
MainActivity.java:
public class MainActivity extends ActionBarActivity implements DrawingViewInterface {
private DrawingView drawView;
private ImageButton currPaint;
BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
int counter;
volatile boolean stopWorker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = (DrawingView)findViewById(R.id.drawing);
drawView.setViewListener(this);
}
public void findBT(View view){
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null){
Toast.makeText(getApplicationContext(),"Nie wykryto BT", Toast.LENGTH_LONG).show();
}
if(!mBluetoothAdapter.isEnabled()){
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth,0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("MattsBlueTooth"))
{
mmDevice = device;
break;
}
}
}
Toast.makeText(getApplicationContext(),"Znaleziono BT", Toast.LENGTH_LONG).show();
}
public void openBT(View view) throws IOException
{
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
Toast.makeText(getApplicationContext(),"Bluetooth jest otwarty", Toast.LENGTH_LONG).show();
sendData();
}
void sendData() throws IOException
{
String msg = "chuj";
msg += "\n";
mmOutputStream.write(msg.getBytes());
Toast.makeText(getApplicationContext(),"Dane zostały przesłane", Toast.LENGTH_LONG).show();
}
}
logcat的:
03-31 01:00:13.795 18767-18767/com.example.aplikacja3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.aplikacja3, PID: 18767
java.lang.IllegalStateException: Could not execute method of the activity
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothSocket android.bluetooth.BluetoothDevice.createRfcommSocketToServiceRecord(java.util.UUID)' on a null object reference
at com.example.aplikacja3.MainActivity.openBT(MainActivity.java:157)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
03-31 01:35:21.665 20598-20598/com.example.aplikacja3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.aplikacja3, PID: 20598
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4012)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothSocket android.bluetooth.BluetoothDevice.createRfcommSocketToServiceRecord(java.util.UUID)' on a null object reference
at com.example.aplikacja3.MainActivity.openBT(MainActivity.java:157)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
任何人都可以提供帮助吗?
编辑:
谢谢!你帮我。但我的问题不同了。我忘了在“FindBT”中更改设备的名称。 EHH ..
但现在我还有其他问题。我尝试连接我的电脑但是当我尝试发送一些东西时,我得到了这个:
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4012)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:517)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:528)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320)
at com.example.aplikacja3.MainActivity.openBT(MainActivity.java:160)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
答案 0 :(得分:1)
根据您的logcat,您从null对象(即mmDevice)调用方法createRfcommSocketToServiceRecord
。该解决方案仅在mmDevice之后调用openBT才能具有非null值。为避免崩溃,您可以将此if条件作为openBT方法的第一行:
if (mmDevice != null) {
// rest of your method here
答案 1 :(得分:1)
只要您的BT设备未将自己标识为findBT
,您就必须在MattsBlueTooth
方法中调整设备名称或删除条件:
if(device.getName().equals("MattsBlueTooth"))
{
mmDevice = device;
break;
}
不过,您应该应用@androidevil建议的检查,以避免在相关蓝牙设备未配对时发生崩溃。