Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
我写了一个样本来学习BroadcastReceiver。但是,当我重启手机时,该应用程序崩溃了。这是我的源代码和manifest.xml:

 public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
    }

    public class BootCompleteReceiver extends BroadcastReceiver
    {

      @Override
      public void onReceive(Context context, Intent intent) {
          Toast.makeText(context,"Boot Complete",Toast.LENGTH_LONG).show();

      }
    }
  }




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

  <uses-sdk
      android:minSdkVersion="14"
      android:targetSdkVersion="17" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <application
      android:allowBackup="true"
     android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      <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>
      <receiver android:name=".BootCompleteReceiver" >
          <intent-filter>
              <action android:name="android.intent.action.BOOT_COMPLETED" />

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

</manifest>

最佳答案

至少基于您在答案中显示的代码,您没有名为com.example.boot.BootCompleteReceiver的类。

当您有一个名为BootCompleteReceiver的类时:


它是MainActivity的内部类,因此BootCompleteReceiver没有命名为com.example.boot.BootCompleteReceiver
它是MainActivity的常规内部类(无static关键字),因此即使清单中的名称正确,Android也无法创建它的实例。


BootCompleteReceiver移到其自己的.java文件中成为常规的独立Java类,或者将其变为public static class内部的MainActivity。在后一种情况下,清单条目的标准类名称为com.example.boot.MainActivity$BootCompleteReceiver

07-24 09:28