BluetoothSocket作为全局变量崩溃

时间:2014-07-20 22:43:09

标签: android sockets bluetooth global-variables

我创建了一个类,应该让我使用蓝牙套接字作为全局变量

package com.lorenzo.robo_so;

  import android.app.Application;
  import android.bluetooth.BluetoothSocket;

  public class Roboso extends Application {

  private BluetoothSocket Socket;

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

  public BluetoothSocket getState(){
    return Socket;
  }
  public void setState(BluetoothSocket s){
      Socket = s;
  }
}

不幸的是,当我尝试在主要活动中建立连接后更改全局变量时,应用程序崩溃

这是我建立连接的类:

public Roboso application = (Roboso) this.getApplication();

public class Connection extends AsyncTask<String, Void, String> {

    protected void onPreExecute() {
        Toast.makeText(MainActivity.this, "Connessione...", Toast.LENGTH_SHORT).show();
        circle.setVisibility(View.INVISIBLE);
    }

    protected String doInBackground(String... add) {

        moduloBT.cancelDiscovery();
        BluetoothDevice device = moduloBT.getRemoteDevice(add[0]);
        String name = device.getName();
        try {
          SocketBT = device.createRfcommSocketToServiceRecord(uuid);
        } catch (IOException e) {
            Toast.makeText(MainActivity.this, "Errore di connessione", Toast.LENGTH_SHORT).show();
        }
        try {
            SocketBT.connect();
        } catch (IOException e) {
          try {
              SocketBT.close();
              Toast.makeText(MainActivity.this, "Errore di connessione", Toast.LENGTH_SHORT).show();
          } catch (IOException e2) {
              Toast.makeText(MainActivity.this, "Errore di connessione", Toast.LENGTH_SHORT).show();
          }
        }
        application.setState(SocketBT);
        Intent intent = new Intent(MainActivity.this, IPWebcam.class);
        intent.putExtra(EXTRA_NAME, name);
        startActivity(intent);
        return name;
    }

    protected void onPostExecute(String name){
        finish();
    }
}

当我删除命令application.setState(SocketBT);时,此类正常工作,但我不明白为什么使用bluetoothsocket作为全局变量时出现问题

这是崩溃后的日志:

07-21 13:02:44.505:E / AndroidRuntime(24837):致命异常:AsyncTask#2

07-21 13:02:44.505:E / AndroidRuntime(24837):流程:com.lorenzo.robo_so,PID:24837

07-21 13:02:44.505:E / AndroidRuntime(24837):java.lang.RuntimeException:执行doInBackground()时发生错误

07-21 13:02:44.505:E / AndroidRuntime(24837):在android.os.AsyncTask $ 3.done(AsyncTask.java:300)

07-21 13:02:44.505:E / AndroidRuntime(24837):at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)

07-21 13:02:44.505:E / AndroidRuntime(24837):at java.util.concurrent.FutureTask.setException(FutureTask.java:222)

07-21 13:02:44.505:E / AndroidRuntime(24837):at java.util.concurrent.FutureTask.run(FutureTask.java:242)

07-21 13:02:44.505:E / AndroidRuntime(24837):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)

07-21 13:02:44.505:E / AndroidRuntime(24837):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

07-21 13:02:44.505:E / AndroidRuntime(24837):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)

07-21 13:02:44.505:E / AndroidRuntime(24837):at java.lang.Thread.run(Thread.java:841)

07-21 13:02:44.505:E / AndroidRuntime(24837):引起:java.lang.NullPointerException

07-21 13:02:44.505:E / AndroidRuntime(24837):at com.lorenzo.robo_so.MainActivity $ Connection.doInBackground(MainActivity.java:247)

07-21 13:02:44.505:E / AndroidRuntime(24837):at com.lorenzo.robo_so.MainActivity $ Connection.doInBackground(MainActivity.java:1)

07-21 13:02:44.505:E / AndroidRuntime(24837):在android.os.AsyncTask $ 2.call(AsyncTask.java:288)

07-21 13:02:44.505:E / AndroidRuntime(24837):at java.util.concurrent.FutureTask.run(FutureTask.java:237)

07-21 13:02:44.505:E / AndroidRuntime(24837):... 4更多

07-21 13:02:48.755:I / Process(24837):发送信号。 PID:24837 SIG:9

0 个答案:

没有答案