CCBP 2016-08-12 04:17 采纳率: 0%
浏览 2360

Android 关于蓝牙的app打开闪退 显示NullPointerException

我把中间的部分省略了,应该是onResume()那里出了问题,请问应该如何解决

图片说明

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
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.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class Control extends Activity implements Runnable {

    String tag = "MyControl";
    public boolean BluetoothFlag = true;
    public int requestCode = 1, num1 = 0, num2 = 0,num3 = 0,runState = 0;

    private float HCenterX,HCenterY;
    private float RangeCenterX,RangeCenterY,RangeR = 100;

    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private ImageButton ib1;

    FrameLayout.LayoutParams LP_iv2;

    private BluetoothAdapter mBluetoothAdapter = null;
    private BluetoothSocket btSocket = null;
    private static String address = null;//要连接的蓝牙设备MAC地址

    private OutputStream outStream = null;
    private InputStream InStream = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.control);

        iv1 = (ImageView) findViewById(R.id.ct_iv1);
        iv2 = (ImageView) findViewById(R.id.ct_iv2);
        iv3 = (ImageView) findViewById(R.id.ct_iv3);
        ib1 = (ImageButton) findViewById(R.id.ct_ib1);
        ib1.setOnClickListener(new ClickEvent());

        Animation Aiv1_zoom = AnimationUtils.loadAnimation(this, R.drawable.iv1_zoom);
        iv1.startAnimation(Aiv1_zoom);

        LP_iv2 = (FrameLayout.LayoutParams) iv2.getLayoutParams();//获取控件布局参数
        Display display = getWindowManager().getDefaultDisplay();
        RangeCenterX = display.getWidth() / 2;
        RangeCenterY = display.getHeight() / 2;

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "您的设备不支持蓝牙", Toast.LENGTH_LONG).show();
            Log.i(tag, "设备不支持蓝牙");
        }
    }
    …………

    public void sendCmd(String message) {
        try {
            outStream = btSocket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] msgBuffer;
        msgBuffer = message.getBytes();
        try {
            outStream.write(msgBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();

        DisplayToast("正在尝试连接蓝牙设备,请稍后····");
        if(address == null){
            address = "00:12:06:01:51:26";
        }else {}
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        try {
            btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);//我觉得是这的问题
        } catch (IOException e) {
            DisplayToast("套接字创建失败!");
        }
        DisplayToast("成功连接蓝牙设备!");
        mBluetoothAdapter.cancelDiscovery();
        try {
            btSocket.connect();// Debug到这应用退出
            DisplayToast("连接成功建立,可以开始操控了~~~");
            myText.setText("蓝牙设备已准备好了!");
            sendCmd("Connect OK!");
            BluetoothFlag = true;
            MyThread bluetoothThread = new MyThread();
            bluetoothThread.start();
        } catch (IOException e) {
            try {
                btSocket.close();
            } catch (IOException e2) {
                DisplayToast("连接没有建立,无法关闭套接字!");
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (outStream != null) {
            try {
                outStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            btSocket.close();
            BluetoothFlag = false;// 关闭蓝牙读线程
        } catch (IOException e2) {
        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        mBluetoothAdapter.disable();
        super.onDestroy();
    }

    @Override
    public void run() {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 & resultCode == 1) {
            address = data.getStringExtra("MAC");
        }
    }

    private class ClickEvent implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.ct_ib1 : {
                num2 = 1;
                Intent intent = new Intent(Control.this, BlueTooth.class);
                startActivityForResult(intent, requestCode);
            }
            }
        }
    }

    class MyHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            Bundle data = msg.getData();
            Log.d(tag, "in the thread:age=" + data.getInt("age")
                    + " name=" + data.getString("name"));
            try {
                InStream = btSocket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }

            byte[] tmp = new byte[5];

            try {
                InStream.read(tmp, 0, 5);
                Log.i(tag, new String(tmp, 0, 5) + " ");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        MyHandler(Looper looper) {
            super(looper);
        }
        MyHandler() {}
    }

    public class MyThread extends Thread {

        MyThread() {
            BluetoothFlag = true;
            try {
                InStream = btSocket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            byte[] tmp = null;
            while (BluetoothFlag) {
                try {
                    demo();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        public String demo() throws IOException, InterruptedException {
            byte[] buffer = new byte[1];
            int length = 0;
            byte bb = 0;
            String line = null;
            Log.i(tag, "demo");
            ArrayList<Byte> list = new ArrayList<Byte>();
            while (InStream.read(buffer) == -1) {
                Thread.sleep(100);
                Log.i(tag, "-->" + InStream.read(buffer));
            }
            return null;
        }
    }
}
  • 写回答

1条回答 默认 最新

  • zero172 2016-08-12 05:34
    关注

    你这个代码不完全
    首先DisplayToast方法的具体内容没有贴出来,不过我猜测应该是弹出提示框,不知道你的提示框是不是在那个方法里面进行实例化的
    第二myText完全没有定义,就直接用了.我不知道你的编译器是怎么通过的.
    还有你这个是空指针异常,在onResume设置断点就能调试出来的小问题.

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题