尝试使用蓝牙时,空指针异常(Nullpointer exception when trying to

2019-09-29 12:08发布

我试图创建一个使用设备的蓝牙Android应用程序。 然而,当我试图仿效的应用,它给了我这个错误:不幸的是应用程序已经停止。

我搜索了一个解决方案,但对于这个问题,我已经做了以下内容:

  1. 使用实际的设备,而不是仿真器,因为蓝牙功能无法仿真
  2. 根据一些建议,我在网上找到修改代码
  3. 具有蓝牙功能的计算机上使用虚拟机,而不是正常的模拟器。

以上所有的尝试证明是徒劳的和无济于事的。

我的Java文件:

package com.example.application;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity{
/** Called when the activity is first created. */
private BluetoothAdapter btAdapter;

public TextView statusUpdate;
public Button connect;
public Button disconnect;
public ImageView logo;

BroadcastReceiver bluetoothState = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent){
        String prevStateExtra=BluetoothAdapter.EXTRA_PREVIOUS_STATE;
        String stateExtra=BluetoothAdapter.EXTRA_STATE;
        int state = intent.getIntExtra(prevStateExtra,  -1);
        int previousState = intent.getIntExtra(prevStateExtra, -1);
        String toastText="";
        switch(state){
        case(BluetoothAdapter.STATE_TURNING_ON) :
        {
            toastText="Bluetooth turning on";
            Toast.makeText(MainActivity.this, toastText,          Toast.LENGTH_SHORT).show();
            break;
        }   
        case(BluetoothAdapter.STATE_ON) :
        {
            toastText="Bluetooth on";
            Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
            setupUI();
            break;
        }   
        case(BluetoothAdapter.STATE_TURNING_OFF) :
        {
            toastText="Bluetooth turning off";
            Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
            break;
        }       
        case(BluetoothAdapter.STATE_OFF) :
        {
            toastText="Bluetooth off";
            Toast.makeText(MainActivity.this, toastText, Toast.LENGTH_SHORT).show();
            setupUI();
            break;
        }

        }

    }


};
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
setupUI();  
}//end onCreate



private void setupUI(){
//get references
//final TextView statusUpdate = (TextView) findViewById(R.id.result);
final Button connect = (Button)findViewById(R.id.connect);
final Button disconnect = (Button)findViewById(R.id.disconnect);
//final ImageView logo = (ImageView)findviewById(R.id.logo);
//set display view

disconnect.setVisibility(View.GONE);
logo.setVisibility(View.GONE);
btAdapter = BluetoothAdapter.getDefaultAdapter();
if(btAdapter.isEnabled()){
    String address = btAdapter.getAddress();
    String name = btAdapter.getName();
    String statusText = name + " : " + address;
    statusUpdate.setText(statusText);
}
else{
    connect.setVisibility(View.VISIBLE);
    statusUpdate.setText("Bluetooth is not on");
}

connect.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v){
        String actionStateChanged =     BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED;
        String actionRequestEnable = BluetoothAdapter.ACTION_REQUEST_ENABLE;
        IntentFilter filter = new IntentFilter(actionStateChanged);
        registerReceiver(bluetoothState, filter);
        startActivityForResult(new Intent(actionRequestEnable), 0);


    }

}); //end connect onClickListener

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

    }

}); //end disconnect onClickListener

}
} //end setupUI

main.xml中:

<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:padding="1dp" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:text="Sherline Android App"
    android:textColor="#0000ff"
    android:textSize="20dp"
    android:textStyle="bold" />

    <requestFocus />

    <Button
        android:id="@+id/connect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="36dp"
        android:text="Connect Bluetooth"
        android:textSize="12sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:layout_alignTop="@+id/connect"
        android:layout_toRightOf="@+id/disconnect"
        android:background="#808080"
        android:ems="10"
        android:hint="G-code goes here..."
        android:inputType="textMultiLine"
        android:textColorHint="#FFFFFFFF"
        android:width="150dp" >                     

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/disconnect"
        android:layout_alignRight="@+id/disconnect"
        android:layout_below="@+id/disconnect"
        android:radius="3dp"
        android:text="Stop"
        android:textSize="12dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Button01"
        android:layout_alignRight="@+id/Button01"
        android:layout_centerVertical="true"
        android:radius="3dp"
        android:text="Pause"
        android:textSize="12dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Button02"
        android:layout_alignRight="@+id/Button02"
        android:layout_below="@+id/Button02"
        android:radius="3dp"
        android:text="Resume"
        android:textSize="12dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/disconnect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/connect"
        android:layout_below="@+id/connect"
        android:radius="3dp"
        android:text="Disconnect Bluetooth"
        android:textSize="12sp"
        android:textStyle="bold" />

</RelativeLayout>

logcat的显示以下错误(S):

01-31 10:34:41.511: E/AndroidRuntime(978): FATAL EXCEPTION: main
01-31 10:34:41.511: E/AndroidRuntime(978): java.lang.RuntimeException: Unable to start          activity ComponentInfo{com.example.application/com.example.application.MainActivity}:     java.lang.NullPointerException
01-31 10:34:41.511: E/AndroidRuntime(978):  at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-31 10:34:41.511: E/AndroidRuntime(978):  at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-31 10:34:41.511: E/AndroidRuntime(978):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-31 10:34:41.511: E/AndroidRuntime(978):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-31 10:34:41.511: E/AndroidRuntime(978):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 10:34:41.511: E/AndroidRuntime(978):  at android.os.Looper.loop(Looper.java:137)
01-31 10:34:41.511: E/AndroidRuntime(978):  at android.app.ActivityThread.main(ActivityThread.java:5039)
01-31 10:34:41.511: E/AndroidRuntime(978):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 10:34:41.511: E/AndroidRuntime(978):  at java.lang.reflect.Method.invoke(Method.java:511)
01-31 10:34:41.511: E/AndroidRuntime(978):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-31 10:34:41.511: E/AndroidRuntime(978):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-31 10:34:41.511: E/AndroidRuntime(978):  at dalvik.system.NativeStart.main(Native     Method)
01-31 10:34:41.511: E/AndroidRuntime(978): Caused by: java.lang.NullPointerException
01-31 10:34:41.511: E/AndroidRuntime(978):  at     com.example.application.MainActivity.setupUI(MainActivity.java:87)
01-31 10:34:41.511: E/AndroidRuntime(978):  at com.example.application.MainActivity.onCreate(MainActivity.java:73)
01-31 10:34:41.511: E/AndroidRuntime(978):  at android.app.Activity.performCreate(Activity.java:5104)
01-31 10:34:41.511: E/AndroidRuntime(978):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-31 10:34:41.511: E/AndroidRuntime(978):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-31 10:34:41.511: E/AndroidRuntime(978):  ... 11 more
01-31 10:54:41.175: E/AndroidRuntime(1149): FATAL EXCEPTION: main
01-31 10:54:41.175: E/AndroidRuntime(1149): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.application/com.example.application.MainActivity}:     java.lang.NullPointerException
01-31 10:54:41.175: E/AndroidRuntime(1149):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at     android.app.ActivityThread.access$600(ActivityThread.java:141)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at android.os.Looper.loop(Looper.java:137)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at java.lang.reflect.Method.invokeNative(Native Method)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at java.lang.reflect.Method.invoke(Method.java:511)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at dalvik.system.NativeStart.main(Native Method)
01-31 10:54:41.175: E/AndroidRuntime(1149): Caused by: java.lang.NullPointerException
01-31 10:54:41.175: E/AndroidRuntime(1149):     at com.example.application.MainActivity.setupUI(MainActivity.java:87)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at com.example.application.MainActivity.onCreate(MainActivity.java:73)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at android.app.Activity.performCreate(Activity.java:5104)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-31 10:54:41.175: E/AndroidRuntime(1149):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-31 10:54:41.175: E/AndroidRuntime(1149):     ... 11 more

请帮忙! 谢谢!

Answer 1:

那么,你得到NullPointerException ,并有可能成为2箱子兴趣。

  1. statusUpdatelogo变量是NULL ,但其他的答案已经解释。

  2. BluetoothAdapter.getDefaultAdapter()可能返回NULL如果不支持硬件蓝牙。 所以,你需要处理这种情况太..

     btAdapter = BluetoothAdapter.getDefaultAdapter(); if(btAdapter != null && btAdapter.isEnabled()){ // see changes in this line.. 


Answer 2:

问题是,有在这条线的空指针:

       logo.setVisibility(View.GONE);

取消注释该行:

         //final ImageView logo = (ImageView)findviewById(R.id.logo);

编辑:同样的问题statusUpdate。



Answer 3:

一个问题... ...该组件心不是连接到您的布局:

statusUpdate.setText(statusText);

我想你需要的东西是这样的:

statusUpdate = (TextView)findViewById(R.id.textView1);


文章来源: Nullpointer exception when trying to use Bluetooth