当我的 android 应用程序在启动后调用我的接收器时,我收到以下错误。此问题仅发生在 Android GoogleApi 的 API 8 级平台 2.2 中(这在 api 8 级、10 级及更高版本中运行良好)。
09-18 05:26:10.421: E/AndroidRuntime(261): FATAL EXCEPTION: main
09-18 05:26:10.421: E/AndroidRuntime(261): java.lang.RuntimeException: Unable to instantiate receiver com.adduci.BootReceiver: java.lang.ClassNotFoundException: com.adduci.BootReceiver in loader dalvik.system.PathClassLoader[/data/app/com.adduci-2.apk]
09-18 05:26:10.421: E/AndroidRuntime(261): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2789)
09-18 05:26:10.421: E/AndroidRuntime(261): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
09-18 05:26:10.421: E/AndroidRuntime(261): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
09-18 05:26:10.421: E/AndroidRuntime(261): at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 05:26:10.421: E/AndroidRuntime(261): at android.os.Looper.loop(Looper.java:123)
09-18 05:26:10.421: E/AndroidRuntime(261): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-18 05:26:10.421: E/AndroidRuntime(261): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 05:26:10.421: E/AndroidRuntime(261): at java.lang.reflect.Method.invoke(Method.java:521)
09-18 05:26:10.421: E/AndroidRuntime(261): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-18 05:26:10.421: E/AndroidRuntime(261): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-18 05:26:10.421: E/AndroidRuntime(261): at dalvik.system.NativeStart.main(Native Method)
09-18 05:26:10.421: E/AndroidRuntime(261): Caused by: java.lang.ClassNotFoundException: com.adduci.BootReceiver in loader dalvik.system.PathClassLoader[/data/app/com.adduci-2.apk]
09-18 05:26:10.421: E/AndroidRuntime(261): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
09-18 05:26:10.421: E/AndroidRuntime(261): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
09-18 05:26:10.421: E/AndroidRuntime(261): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
09-18 05:26:10.421: E/AndroidRuntime(261): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2780)
09-18 05:26:10.421: E/AndroidRuntime(261): ... 10 more
我澄清说代码工作正常,因为如果我在模拟器上运行它以运行 10 或 Api Api 16 可以完美运行。我重复的错误,只有当我在模拟器上运行它以运行 8 或 Google Api Api 的平台 2.2 API 级别 8 时才会得到它。谢谢!
我的清单文件是这样的:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ListPro3Activity"
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=".MyReceiverAlarmItem"/>
<activity android:name=".AlarmEventActivity">
</activity>
<activity android:name=".ShowAlarmMessageActivity">
</activity>
<receiver android:name="com.adduci.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
<!--category android:name="android.intent.category.LAUNCHER" /-->
</intent-filter>
</receiver>
<service android:enabled="true" android:name="ConfigAlarmService">
<intent-filter>
<action android:name="com.adduci.ConfigAlarmService">
</action>
</intent-filter>
</service>
</application>
</manifest>
我的广播文件是这样的:
package com.adduci;
import com.adduci.Utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.v("lp","INICIO BROADCAST Y VOY A LLAMAR AL SERVICE");
Intent i = new Intent();
i.setAction("com.adduci.ConfigAlarmService");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName service = context.startService(i);
//context.stopService(new Intent(context, service.getClass()));
}
}
}
我的服务文件是这样的:
package com.adduci;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.text.ParseException;
import android.app.Service;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class ConfigAlarmService extends Service{
private MyAlarm alarm;
private ArrayList<HashMap<String,Object>> columns;
private ArrayList<Datos> datos;
@Override
public void onCreate(){
Log.v("lp","SERVICE ONCREATE!!!");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
Log.v("lp","StartAtBootService -- onStartCommand()");
....
....
String cols="[" +
"{'content':'itemId'," +
"'SQLiteType':'Int'}," +
"{'content':'itemDescripcion'," +
"'SQLiteType':'String'}," +
"{'content':'recordatorio'," +
"'SQLiteType':'Int'}," +
"{'content':'recordatorioFecha'," +
"'SQLiteType':'String'}" +
"]";
this.columns = Utils.jsonToArray(cols);
Log.v("lp","OPENHELPER");
SQLiteOpenHelper sqLiteOpenHelper=new ListProSQLiteHelper(this, "DBListPro", null, 1);
Log.v("lp","FIN OPENHELPER!!!");
this.load("SELECT * FROM items WHERE recordatorio=1", sqLiteOpenHelper);
this.stopSelf();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onDestroy(){
super.onDestroy();
Log.v("lp","CONFIGALARMSERVICE DESTROY!!");
}
private void load(String sqlCondition, SQLiteOpenHelper sqLiteOpenHelper){
this.datos = new ArrayList<Datos>();
Object[]sql={sqlCondition, sqLiteOpenHelper};
DataFromSqlite dataSqlite=new DataFromSqlite(this, this.getColumns(), sql, DataFromSqlite.ASYNCHRO, DataFromSqlite.DIALOG_WHEEL_OFF){
@Override
void onPreExecuteEvent() {
// TODO Auto-generated method stub
}
@Override
void onDoInBackGroundEvent() {
// TODO Auto-generated method stub
}
@Override
void onPostExecuteEvent() {
// TODO Auto-generated method stub
for(int i=0;i< this.getData().size();i++){
ContentValues cv= this.getData().get(i);
Datos datoItem=new Datos();
for(int j=0; j<this.columns.size();j++){
String col=(String)columns.get(j).get("content");
datoItem.put(col, cv.get(col));
}
datos.add(datoItem);
}
setAlarms();
}
};
}
private void setAlarms(){
Log.v("lp","DaToS : "+ this.datos);
for(int i=0; i< this.datos.size(); i++){
Datos dat = this.datos.get(i);
if(Integer.parseInt((String) dat.get("recordatorio")) == 1){
Bundle bundleReceiver= new Bundle();
bundleReceiver.putString("NotificationText", (String) dat.get("itemDescripcion"));
this.alarm= new MyAlarm(this, MyReceiverAlarmItem.class, bundleReceiver);
Date recordatorioFecha= new Date();
SimpleDateFormat formato= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
try {
recordatorioFecha= formato.parse((String) dat.get("recordatorioFecha"));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar calendar= Calendar.getInstance();
calendar.setTime(recordatorioFecha);
if(calendar.getTimeInMillis() > System.currentTimeMillis()){
Log.v("lp","DAT:"+dat+"MES:"+String.valueOf(calendar.get(Calendar.MONTH)));
this.alarm.setAlarm(Integer.parseInt((String) dat.get("itemId")) , calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE));
}
}
}
}
private ArrayList<HashMap<String,Object>> getColumns(){
return this.columns;
}
}