当我在mainactivity中创建一个新类的类实例时,我的应用程序崩溃了

时间:2015-12-30 11:09:15

标签: android

public class main_activity extends Activity implementsBluetoothLeUart.Callback{

Activity activity;
DisplayMetrics metrics;
private ArrayAdapter<String> adapter;
private ArrayList<String> liste,devicedata;
private ListView list;
private AlertDialog.Builder builder;
public EditText input;
String name,address,Devicename,Deviceaddress,datadevicename;
private BluetoothGatt mBluetoothGatt;
public static String SelectedDeviceName;
ProgressDialog progressDialog;
private BluetoothAdapter mBluetoothAdapter;
public boolean mScanning;
public ArrayList<BluetoothDevice> dev;
private Handler mHandler;
private static final int REQUEST_ENABLE_BT = 1;
private static final long SCAN_PERIOD = 10000;    // Stops scanning after 10 seconds.
SqlHandler sh = new SqlHandler(this);
//BluetoothLeService service = new BluetoothLeService();
BluetoothLeUart uart = new BluetoothLeUart(this);//this is where i am creating the instance
public static BluetoothDevice device;
private BluetoothGatt mGatt;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity);


    activity = this;

    mHandler = new Handler();
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    scanLeDevice(true);
    mActionBar.setTitle(Html.fromHtml("<font color='#727272'>Board List</font>"));
    list = (ListView) findViewById(R.id.list);

    liste = new ArrayList<String>();
    liste.clear();
    sh.openDB();
    Cursor cur = sh.getAllnames();
    while(cur.moveToNext()){
         name = cur.getString(0);
         address = cur.getString(1);
         liste.add(name+"  "+address);
    }
    adapter = new ArrayAdapter<String>(list.getContext(), android.R.layout.simple_list_item_1, liste);
    list.setAdapter(adapter);
    sh.close();



    // On Long Click Listener for Paired BLE Device List
    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            SelectedDeviceName = list.getItemAtPosition(position).toString();
            String[] splitString = SelectedDeviceName.split("  ");
            Devicename = splitString[0];
            Deviceaddress = splitString[1];

            showRenameOrDeleteDialog();
            return true;

        }
    });

    // On  Click Listener for Paired BLE Device List
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           // scanLeDevice(false);
            SelectedDeviceName = list.getItemAtPosition(position).toString();
            String[] splitString = SelectedDeviceName.split("  ");
            Devicename = splitString[0];
            Deviceaddress = splitString[1];
            device = mBluetoothAdapter.getRemoteDevice(Deviceaddress);
            Toast.makeText(getApplicationContext(),device.toString(),Toast.LENGTH_SHORT).show();
            //device.connectGatt(getApplicationContext(), true, gattCallback);
            // uart.registerCallback(main_activity.this);

             uart.connectFirstAvailable();//----->this is where i use the instance to call a method
          }
    });

我正在创建实例的其他类是......

public class BluetoothLeUart extends BluetoothGattCallback implements BluetoothAdapter.LeScanCallback {
public String mDeviceAddress = main_activity.device.toString();

private Context context;
private WeakHashMap<Callback, Object> callbacks;
private BluetoothAdapter adapter;
private BluetoothGatt gatt;
private BluetoothGattCharacteristic tx;
private BluetoothGattCharacteristic rx;
private boolean connectFirst;
private boolean writeInProgress; // Flag to indicate a write is currently in progress

// Device Information state.
private BluetoothGattCharacteristic disManuf;
private BluetoothGattCharacteristic disModel;
private BluetoothGattCharacteristic disHWRev;
private BluetoothGattCharacteristic disSWRev;
private boolean disAvailable;

// Queues for characteristic read (synchronous)
private Queue<BluetoothGattCharacteristic> readQueue;

// Interface for a BluetoothLeUart client to be notified of UART actions.
public interface Callback {
    public void onConnected(BluetoothLeUart uart);
    public void onConnectFailed(BluetoothLeUart uart);
    public void onDisconnected(BluetoothLeUart uart);
    public void onReceive(BluetoothLeUart uart, BluetoothGattCharacteristic rx);
    public void onDeviceFound(BluetoothDevice device);
    public void onDeviceInfoAvailable();
}

public BluetoothLeUart(Context context) {
    super();
    this.context = context;
    this.callbacks = new WeakHashMap<Callback, Object>();
    this.adapter = BluetoothAdapter.getDefaultAdapter();
    this.gatt = null;
    this.tx = null;
    this.rx = null;
    this.disManuf = null;
    this.disModel = null;
    this.disHWRev = null;
    this.disSWRev = null;
    this.disAvailable = false;
    this.connectFirst = false;
    this.writeInProgress = false;
    this.readQueue = new ConcurrentLinkedQueue<BluetoothGattCharacteristic>();
}

// Return instance of BluetoothGatt.
public BluetoothGatt getGatt() {
    return gatt;
}






// Disconnect to a device if currently connected.
public void disconnect() {
    if (gatt != null) {
        gatt.disconnect();
    }
    gatt = null;
    tx = null;
    rx = null;
}

// Stop any in progress UART device scan.
public void stopScan() {
    if (adapter != null) {
        adapter.stopLeScan(this);
    }
}

// Start scanning for BLE UART devices.  Registered callback's onDeviceFound method will be called
// when devices are found during scanning.
public void startScan() {
    if (adapter != null) {
        adapter.startLeScan(this);
    }
}

// Connect to the first available UART device.
public void connectFirstAvailable() {
    Toast.makeText(context,"Entered",Toast.LENGTH_SHORT).show();
    // Disconnect to any connected device.
    disconnect();
    // Stop any in progress device scan.
    stopScan();
    // Start scan and connect to first available device.
    connectFirst = true;
    startScan();
}

我尝试过以不同的方式创建实例,例如

BluetoothLeUart uart = new BluetoothLeUart(this);//app crashes
BluetoothLeUart uart = new BluetoothLeUart(getApplicationContext);//app crashes
BluetoothLeUart uart = new BluetoothLeUart();//app crashes

2 个答案:

答案 0 :(得分:0)

你需要在某种方法中创建实例,然后只有this才有效,你的代码应该是

BluetoothLeUart uart;

并在onCreate()

中对其进行初始化
uart = new BluetoothLeUart(this);

答案 1 :(得分:0)

在BluetoothLeUart中,

public String mDeviceAddress = main_activity.device.toString();

将失败(它将为NULL),因为您在创建main_activity期间创建了uart对象。

BluetoothLeUart uart = new BluetoothLeUart(this);//this is where i am creating the instance

IE,您正在尝试初始化彼此完全初始化的对象。可能会有更多错误;我只看到我发现了一个。