2014-04-18 103 views
0

我工作的藍牙應用,但我的應用程序崩潰。這裏是我的MainActivity.java爲什麼我的藍牙應用程序崩潰

package com.race_gurram.bluetoothadapter; 

import java.util.ArrayList; 
import java.util.Set; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
private Button on, off, get, bring; 
private BluetoothAdapter BA; 
private Set<BluetoothDevice> pairedDevices; 
private ListView lv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    on = (Button) findViewById(R.id.on); 
    off = (Button) findViewById(R.id.off); 
    get = (Button) findViewById(R.id.get); 
    bring = (Button) findViewById(R.id.list); 

    lv = (ListView) findViewById(R.id.listView1); 
    BA = BluetoothAdapter.getDefaultAdapter(); 
} 

public void on(View view) 
{ 
    if(!BA.isEnabled()) 
    { 
     Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(turnOn,0); 
     Toast.makeText(getApplicationContext(), "Turned On", Toast.LENGTH_SHORT).show(); 
    } 
    else 
    { 
     Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_SHORT).show(); 
    } 
} 

public void list(View view) 
{ 
    pairedDevices = BA.getBondedDevices(); 
    ArrayList list = new ArrayList(); 
    for(BluetoothDevice bt : pairedDevices) 
    list.add(BA.getName()); 

    Toast.makeText(getApplicationContext(), "Searching for devices", Toast.LENGTH_SHORT).show(); 
    final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list); 
    lv.setAdapter(adapter); 

} 

public void get() 
{ 
    Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
    startActivityForResult(getVisible,0); 

} 
public void off() 
{ 
    BA.disable(); 
    Toast.makeText(getApplicationContext(), "Turned off", Toast.LENGTH_SHORT).show(); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

,這是我activity.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:orientation="vertical" 
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=".MainActivity" > 

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/on" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="on" 
      android:text="@string/on" /> 

     <Button 
      android:id="@+id/get" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="get" 
      android:text="@string/get" /> 

     <Button 
      android:id="@+id/list" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="list" 
      android:text="@string/bring" /> 

     <Button 
      android:id="@+id/off" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="off" 
      android:text="@string/off" /> 

     <ListView 
      android:id="@+id/listView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:visibility="visible" > 
     </ListView> 
    </LinearLayout> 
</ScrollView> 

</RelativeLayout> 

每當我點擊一個按鈕應用程序崩潰...請幫助我與此..在此先感謝。

+3

你可以發佈logcat異常堆棧嗎? – Pphoenix

+0

我沒有在elmulator上運行它。我直接將SDK文件加載到sdk卡上,然後運行應用程序 – TULSIRAJ

+0

嘗試使用usb連接並運行eclipse來運行它,然後您應該能夠看到logcat。 – Pphoenix

回答

0

也許是當你點擊列表按鈕。假設手機有0個保稅設備, paired.getBondedDevices()可能會返回null,並且由於NullPointerException而導致程序崩潰。

pairedDevices = BA.getBondedDevices(); 
ArrayList list = new ArrayList(); 
if(pairedDevices != null) { 
    for(BluetoothDevice bt : pairedDevices) 
     list.add(BA.getName()); 
} 
2

關於get和off方法,應該有一個View參數,對吧?

public void get(View v) { 
    Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
    startActivityForResult(getVisible,0); 

} public void off(View v) { 
    BA.disable(); 
    Toast.makeText(getApplicationContext(), "Turned off", Toast.LENGTH_SHORT).show(); } 
相關問題