2015-02-08 167 views
0

當應用程序首次啓動時,它立即在啓動時崩潰。我已經縮小的問題,以一條線,但真的不知道該怎麼辦= /這裏有兩個班在應用至今:Android藍牙應用程序在啓動時崩潰

package com.example.bluetooth_app; 

import java.util.Set; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 

public class Bluetooth { 
    private BluetoothAdapter mBluetoothAdapter; 
    private Activity activity; //Activity to store main window's activity 
    private ArrayAdapter<String> pDevices; //Array adapter for storing already paired devices 
    private ArrayAdapter<String> nDevices; //Array adapter for storing newly discovered devices 
    private IntentFilter filter; //Filter for catching bluetooth device actions 
    private Button sButton; //Scan button 
    private ListView lvBox; //listview box 

    /** 
    * default constructor, basic initializations 
    */ 
    public Bluetooth(Activity activity) { 
     this.activity = activity; //Set class activity to the activity passed to it by the main activity window 
     pDevices = new ArrayAdapter<String>(activity, R.layout.activity_bluetooth__app); 
     nDevices = new ArrayAdapter<String>(activity, R.layout.activity_bluetooth__app); 
     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

     sButton = (Button) activity.findViewById(R.id.scanButton); //sButton = scan button 
     sButton.setOnClickListener(new View.OnClickListener() { //Listener to check if button is pressed 
      public void onClick(View v) { //If button is pressed start discovering and hide button 
       startDiscovering(); 
       sButton.setVisibility(4); //Make button invisible 
      } 
     }); 

     lvBox = (ListView) activity.findViewById(R.id.deviceList); // lvBox = deviceList listview 
     lvBox.setAdapter(pDevices); 


     // Register for broadcasts when a device is discovered 
     filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     activity.registerReceiver(mReceiver, filter); 

     // Register for broadcasts when discovery has finished 
     filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     activity.registerReceiver(mReceiver, filter); 
    } 

    /** 
    * Check if bluetooth is enabled, if not enable it 
    */ 
    public void getAdapter() { 
     if (!mBluetoothAdapter.isEnabled()) { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      activity.startActivityForResult(enableBtIntent, 1); 
     } 

    } 

    /** 
    * Check if device is bluetooth compatible 
    */ 
    public boolean isCompat() { 
     if(mBluetoothAdapter == null) { 
      return false; //TODO: better error handling 
     } else { 
      return true; 
     } 
    } 

    /** 
    * Close some shit so we do not eat up resources 
    */ 
    public void destroy() { 
     if(mBluetoothAdapter != null) { 
      mBluetoothAdapter.cancelDiscovery(); //cancel discovering devices 
     } 

     activity.unregisterReceiver(mReceiver); 
    } 

    /** 
    * Start discovering devices with bluetooth adapter 
    */ 
    public void startDiscovering() { 
     if(mBluetoothAdapter.isDiscovering()) { 
      mBluetoothAdapter.cancelDiscovery(); 
     } 

     mBluetoothAdapter.startDiscovery(); 
    } 

    public void pairedDevices() { 
     Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     // If there are paired devices 
     if (pairedDevices.size() > 0) { 
      sButton.setText("found some"); 
      // Loop through paired devices 
      for (BluetoothDevice device : pairedDevices) { 
       // Add the name and address to an array adapter to show in a ListView 
       pDevices.add(device.getName() + "\n" + device.getAddress()); 
      } 
     } 
    } 

    /** 
    * The BroadcastReceiver that listens for discovered devices and changes the title when 
    * discovery is finished 
    */ 
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 

      // When discovery finds a device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       // If it's already paired, skip it, because it's been listed already 
       if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 
        nDevices.add(device.getName() + "\n" + device.getAddress()); 
       } 
       // When discovery is finished, change the Activity title 
      } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
       sButton.setVisibility(0); //Make button visible again 
       if (nDevices.getCount() == 0) { 
        sButton.setText("none"); 
        //TODO: none found do something 
       } 
      } 
     } 
    }; 

} 

和:

package com.example.bluetooth_app; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 

import com.example.bluetooth_app.Bluetooth; 

public class Bluetooth_App extends ActionBarActivity { 
    private Bluetooth bT; 

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

     bT = new Bluetooth(this); 

     bT.isCompat(); 
     bT.getAdapter(); 
     bT.pairedDevices(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 

     bT.destroy(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.bluetooth__app, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

的問題是在第一類,這一行:lvBox.setAdapter(pDevices);,如果我註釋它,它運行得很好,如果不是它啓動時崩潰。任何幫助將不勝感激 - 謝謝。

EDIT1 - 沒有它有一個叫DEVICELIST列表視圖,XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.bluetooth_app.Bluetooth_App" > 

    <Button 
     android:id="@+id/scanButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginRight="28dp" 
     android:layout_marginTop="21dp" 
     android:text="Scan" /> 

    <ListView 
     android:id="@+id/deviceList" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/button1" 
     android:layout_below="@+id/button1" 
     android:layout_marginRight="26dp" 
     android:layout_marginTop="48dp" > 
    </ListView> 

</RelativeLayout> 
+0

更新了編輯答案,試試看。 – stkent 2015-02-08 04:49:39

回答

1

請注意,您似乎Bluetooth_App初始化你ArrayAdapter■當被重用你的活動的佈局文件R.layout.activity_bluetooth__app。這可能不是你想要做的。傳遞給ArrayAdapter構造函數的資源應該代表單個AdapterView行的佈局,並且必須包含編號爲text1TextView(要使用更多自定義的行佈局,您需要繼承ArrayAdapter的子類並覆蓋getView)。

+0

呵呵,對不起,即時通訊新的Java,所以這可能是愚蠢的,但是,爲什麼它使用按鈕,並使用它的監聽器? – user3831011 2015-02-08 04:52:42

+0

與按鈕相關的代碼正常工作,因爲該按鈕是您活動佈局的一部分。將活動佈局中的列表視圖(id爲deviceList)視爲「容器」。適配器會將您數據列表中的java對象轉換爲視圖列表,然後將其放入此容器中。適配器通常不瞭解活動佈局;它只需要與一個列表視圖關聯來知道在哪裏放置生成的視圖。這是對局勢的廣泛認識,但希望它有所幫助。 – stkent 2015-02-08 04:59:56

+0

有關您正在使用的ArrayAdapter構造函數的說明,另請參見http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context,%20int)。 – stkent 2015-02-08 05:00:31