&引用;应用程序已意外停止";Android中的错误

&引用;应用程序已意外停止";Android中的错误,android,Android,java文件--Bttest11.java **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 savedInst

java文件--Bttest11.java

**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>
**包niebtest11.example.bluetoothtesta;
/*导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
公共类Bttest11扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bttest11);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.bttest11,菜单);
返回true;
}
*/
导入android.bluetooth.BluetoothAdapter;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.widget.Toast;
公共类Bttest11扩展了BroadcastReceiver{
@凌驾
public void onReceive(上下文、意图){
int state=intent.getExtras().getInt(BluetoothAdapter.EXTRA_state);
开关(状态){
机箱BluetoothAdapter.STATE\u关闭:
Toast.makeText(上下文“Off”,Toast.LENGTH_SHORT).show();
打破
机箱BluetoothAdapter.STATE\u关闭:
Toast.makeText(上下文“关闭”,Toast.LENGTH_SHORT).show();
打破
机箱BluetoothAdapter.STATE_打开:
Toast.makeText(上下文“On”,Toast.LENGTH_SHORT).show();
打破
机箱BluetoothAdapter.STATE_打开:
Toast.makeText(上下文“打开”,Toast.LENGTH_SHORT.show();
打破
}
}
}**
//清单文件--Bluetoothtest11清单
错误:

代码正在运行。但是在android手机中运行.apk时,它会显示一个错误

“应用程序Bluetoothtest11(进程nietest11.example.buetoothtesta)意外停止”


如果只使用BroadcastReceiver,则不要在活动bcoz中写入Bttest11,因为这不是活动

及 如果您想要活动,那么创建一个活动类并从中调用接收者。 在android清单中定义该活动。在活动标记下 你的接收器写在接收器标签上

public class MainActivity extends Activity {

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

}

您是否使用了调试器?您可以将eclipse调试器附加到电话进程。此外,logcat很可能会为您提供堆栈跟踪。请试着自己做这段工作,而不是仅仅在这里发布代码。发布一些日志以进行分析?请也发布日志