2013-03-09 88 views
0

的Java file---Bttest11.java「應用程序已意外停止」 錯誤的Android

**package niebttest11.example.bluetoothtesta; 
/*import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
public class Bttest11 extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bttest11); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.bttest11, menu); 
     return true; 
    } 
    */ 
import android.bluetooth.BluetoothAdapter; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.Toast; 
public class Bttest11 extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     int state = intent.getExtras().getInt(BluetoothAdapter.EXTRA_STATE); 

    switch (state) { 

    case BluetoothAdapter.STATE_OFF: 

     Toast.makeText(context, "Off", Toast.LENGTH_SHORT).show(); 

      break; 

    case BluetoothAdapter.STATE_TURNING_OFF: 

    Toast.makeText(context, "Turning Off", Toast.LENGTH_SHORT).show(); 

     break; 

    case BluetoothAdapter.STATE_ON: 
      Toast.makeText(context, "On", Toast.LENGTH_SHORT).show(); 

      break; 

case BluetoothAdapter.STATE_TURNING_ON: 

     Toast.makeText(context, "Turning On", Toast.LENGTH_SHORT).show(); 

     break; 
     } 
    } 
}** 



//manifest file---Bluetoothtest11 Manifest 


<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="niebttest11.example.bluetoothtesta" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="8" /> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
    <uses-permission android:name="android.permission.BLUETOOTH"/> 


    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="niebttest11.example.bluetoothtesta.Bttest11" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver android:name="Bttest11" > 
      <intent-filter> 
      <action android:name="android.bluetooth.adapter.action.ACTION_FOUND"/> 
      <action android:name="android.bluetooth.adapter.action.ACTION_DISCOVERY_FINISHED"/> 

      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

錯誤:

的代碼running.But而在Android手機上運行.apk文件,它將顯示一個錯誤,

"The application Bluetoothtest11(process nietest11.example.buetoothtesta) has stopped unexpectedly".

+3

你使用調試器?您可以將eclipse調試器附加到電話進程。此外,logcat很可能會爲您提供堆棧跟蹤。請嘗試自己做腿部工作,而不是在這裏發佈你的代碼。 – Femaref 2013-03-09 09:41:24

+0

發佈一些日誌來分析? – QAMAR 2013-03-09 09:43:17

+0

請發佈logcat – 2013-03-09 11:09:30

回答

0

如果您只使用BroadcastReceiver然後不寫Bttest11在活動bcoz那不是活動。

和 如果您想要活動,則創建一個活動類並從那裏調用接收器。 定義了Android清單中的活動。在活動標籤 和接收器標籤的接收器寫入

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    startService(new Intent(MainActivity.this, MyService.class)); 
} 

}

相關問題