2015-02-08 219 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