我目前正在开发一个通过蓝牙控制 arduino 的应用程序。我为菜单等添加了一些活动。昨天我添加了一个新的活动来将我的设备连接到蓝牙模块。甚至可以在一个活动中连接到设备并在另一个活动中实际控制(发送信件)它吗?话虽如此,每次我尝试打开带有蓝牙代码的活动时,应用程序都会崩溃。所有其他活动都可以正常工作。我希望有人可以提供帮助。崩溃日志:2018-09-12 17:25:36.957 10178-10178/com.car.bluetooth.bluetoothcar E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.car.bluetooth.bluetoothcar, PID: 10178java.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.Button.setOnClickListener(android.view.View$OnClickListener)' 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.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.car.bluetooth.bluetoothcar.BT_Classic.onCreate(BT_Classic.java:78) at android.app.Activity.performCreate(Activity.java:6984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235)
2 回答
拉丁的传说
TA贡献1789条经验 获得超8个赞
btonButton 和 btoffButton 尚未初始化,然后您将其称为setonClickListener原因NPE
内部onCreate初始化如下:
btonButton = (Button) findViewById(R.id.btonButton);
btoffButton = (Button) findViewById(R.id.btoffButton);
添加回答
举报
0/150
提交
取消