我寫這段代碼來學習如何在Android中使用藍牙,但是我一直在標題中提到錯誤。完整的錯誤是:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android/com.example.android.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.bluetooth.adapter.action.STATE_CHANGED }
沒有找到處理意圖的活動{act = android.bluetooth.adapter.action.STATE_CHANGED}
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.BLUETOOTH" />
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher">
<receiver
android:name=".MainActivity" >
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.example.android;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity
{
private final String LOG_TAG = getClass().getSimpleName();
private static int REQUEST_ENABLE_BT = 1;
private static int REQUEST_STATE_CHANGED_BT = 2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BluetoothAdapter oBTAdapter = BluetoothAdapter.getDefaultAdapter();
if (oBTAdapter == null) Log.i(LOG_TAG, "This device does not support Bluetooth");
if (!oBTAdapter.isEnabled()) {
Intent iEnableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(iEnableBT, REQUEST_ENABLE_BT);
}
Intent iBTStatus = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
startActivityForResult(iBTStatus, REQUEST_STATE_CHANGED_BT);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BT)
if (resultCode == RESULT_CANCELED) Log.e(LOG_TAG, "Bluetooth was not enabled on the device");
else if (requestCode == REQUEST_STATE_CHANGED_BT)
if (resultCode == RESULT_OK && !data.getStringExtra(BluetoothAdapter.EXTRA_STATE).equals(BluetoothAdapter.STATE_ON)) Log.e(LOG_TAG, "Bluetooth adapter is not on");
}
}
在設備上我對與我看到,它沒有跳過的IsEnabled日誌藍牙()測試(查看藍牙功能目前已啓用並可供使用)。它能夠創建觀察藍牙狀態的意圖,但它在startActivityForResult()上崩潰。我做錯了什麼?
你到底爲什麼試圖達到爲什麼接收者和活動的名字與'MainActivity'相同? –
接收者的名字是否會接收到意圖的類? –
添加關於接收機和活動的確切內容的詳細信息 –