我用android手机向arduino发送信息出现fatal exception main java.lang.nullpointerexception错误

qq766813 2014-05-14 08:01:50
我用android手机向arduino发送信息出现fatal exception main java.lang.nullpointerexception错误​

android程序在我手机上总是崩溃,程序运行日志显示错误在以下紫色部分处,存在java.lang.nullPointerException异常,导致每次准备打开电灯时程序都会崩溃。

else{
btout.write("1".getBytes());
btout.flush();
lightButton.setImageResource(R.drawable.light_open);
lightStatus = true;
}
问了很多人都不知道该怎么解决,我刚刚接触android和arduino实在是没有办法了。希望各位大神能够帮帮我,谢谢了。我把错误日志放在最后了。我用的开发环境是直接从官方网站下载的adt-bundle-windows-x86-20131030,SDK版本是22.6.3,手机是android 4.0.4版本。(如果需要源程序请发信息到我邮箱:qq768135@gmail.com)

具体代码在下面:



package com.liuyongliang.smartlightingsystem;

import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

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

private static final String PREF_BLUETOOTH_DEVICE_MAC = "BLUETOOTH_DEVICE_MAC";

BluetoothAdapter localAdapter;
BluetoothSocket lightSocket;
OutputStream btout;
boolean lightStatus = false;
ImageButton lightButton;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView author = (TextView) this.findViewById(R.id.textView1);
author.setText("再续朝圣路");
lightButton = (ImageButton) this.findViewById(R.id.imageButton1);
lightButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// if(btout!=null){

try {
if (lightStatus) {
btout.write("0".getBytes());
btout.flush();
lightButton.setImageResource(R.drawable.light_close);
lightStatus = false;
} else {
btout.write("1".getBytes());
btout.flush();
lightButton.setImageResource(R.drawable.light_open);
lightStatus = true;
}

} catch (IOException e) {
e.printStackTrace();
}
// }
}
});
enableBT();

SharedPreferences settingActivity = getPreferences(MODE_PRIVATE);
String device_mac = settingActivity.getString(
PREF_BLUETOOTH_DEVICE_MAC, null);
if (device_mac == null) {
Intent serverIntent = null;
serverIntent = new Intent(MainActivity.this,
DeviceListActivity.class);
startActivityForResult(serverIntent, 1);
} else {
BluetoothDevice myLightDevice = localAdapter
.getRemoteDevice(device_mac);
connectDevice(myLightDevice);
}
}

public void enableBT() {
localAdapter = BluetoothAdapter.getDefaultAdapter();
// If Bluetooth not enable then do it

if (!localAdapter.isEnabled()) {
localAdapter.enable();
while (!(localAdapter.isEnabled())) {

}
}
}

@Override
protected void onDestroy() {

try {
btout.close();
lightSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
super.onDestroy();
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
connectDevice(getBluetoothDevice(data, true));
}
break;
}
}

public BluetoothDevice getBluetoothDevice(Intent data, boolean secure) {
// Get the device MAC address
String address = data.getExtras().getString(
DeviceListActivity.EXTRA_DEVICE_ADDRESS);

SharedPreferences settingActivity = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settingActivity.edit();
prefEditor.putString(PREF_BLUETOOTH_DEVICE_MAC, address);
prefEditor.commit();
// Get the BLuetoothDevice object
return localAdapter.getRemoteDevice(address);
}

private void connectDevice(BluetoothDevice device) {

try {
lightSocket = device.createRfcommSocketToServiceRecord(UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB"));

lightSocket.connect();

if (lightSocket != null) {
try {
btout = lightSocket.getOutputStream();
// btout.write("MobilePhone Connected!".getBytes());
// btout.flush();
} catch (IOException e) {
e.printStackTrace();
}
} else {
// Error
}

} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent serverIntent = null;
switch (item.getItemId()) {
case R.id.secure_connect_scan:
// Launch the DeviceListActivity to see devices and do scan
serverIntent = new Intent(MainActivity.this,
DeviceListActivity.class);
startActivityForResult(serverIntent, 1);
return true;
}
return false;
}
}



...全文
157 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

58,445

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧