Hello我的代码给出了以下两个异常,我似乎无法识别或解决问题,我已经给出了我确定错误存在的类,如果你需要我项目中的其他类[请参阅进口]让我知道。谢谢
注意应用程序崩溃通常当Google Playstore正在更新应用程序或有人使用Android 4.3版本时4.4
java.lang.RuntimeException:无法启动接收器
com.taimoor.receiver.ChangePackageReciever: java.lang.RuntimeException: This package not going to stored
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2426)
at android.app.ActivityThread.access$1700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
了java.lang.RuntimeException 在com.taimoor.db.ApplicationTb.createApplication
中java.lang.RuntimeException: This package not going to stored
at com.taimoor.db.ApplicationTb.createApplication(ApplicationTb.java:57)
at com.taimoor.receiver.ChangePackageReciever.onReceive(ChangePackageReciever.java:33)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2419)
ApplicationTb类
package com.taimoor.db;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.util.Log;
public class ApplicationTb {
public final static String TABLE_NAME = "application";
public final static String COL_PACKAGE ="package";
public final static String COL_ID = "_id";
public final static String COL_ORIENTATION = "orientation";
public final static String COL_CATEGORY_ID = "category_id";
public final static String COL_ICON = "icon";
public final static String COL_NAME = "name";
public final static String DEFAULT_ORIENTATION = Application.ORIENTATION_AUTO;
public final static int DEFAULT_CATEGORY_ID = 1; //none
public final static int DEFAULT_ICON = android.R.drawable.ic_dialog_alert;
private MyDbHelper dbHelper;
private Context context;
public static String getCreateQuery() {
return "CREATE TABLE application (\n" +
" \"_id\" INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
" \"package\" TEXT,\n" +
" \"orientation\" TEXT,\n" +
" \"category_id\" INTEGER NOT NULL DEFAULT (1),\n" +
" \"icon\" INTEGER,\n" +
" \"name\" TEXT\n" +
")\n" +
"";
}
public ApplicationTb(Context context) {
this.dbHelper = new MyDbHelper(context);
this.context = context;
}
public Application createApplication(PackageInfo pi) throws NameNotFoundException {
if(this.isSystemPackage(pi) || pi.packageName.equals(this.context.getPackageName())) throw new RuntimeException("This package not going to stored");
Application ret = new Application();
ContentValues values = new ContentValues();
Resources res = null;
res = this.context.getPackageManager().getResourcesForApplication(pi.applicationInfo);
//String name = res.getString(pi.applicationInfo.labelRes);
String name = this.context.getPackageManager().getApplicationLabel(pi.applicationInfo).toString();
values.put(COL_NAME, name);
values.put(COL_PACKAGE,pi.packageName);
values.put(COL_ICON,pi.applicationInfo.icon);
values.put(COL_CATEGORY_ID, DEFAULT_CATEGORY_ID);
values.put(COL_ORIENTATION,DEFAULT_ORIENTATION);
ret.setCategory_id(DEFAULT_CATEGORY_ID);
ret.setIcon(pi.applicationInfo.icon);
ret.setImage(res.getDrawable(pi.applicationInfo.icon));
ret.setName(name);
ret.setOrientation(DEFAULT_ORIENTATION);
ret.setPackageName(pi.packageName);
ret.setImage(res.getDrawable(ret.getIcon()));
long id = this.dbHelper.getWritableDatabase().insert(TABLE_NAME,null, values);
ret.set_id(id);
return ret;
}
public List<Application> getAllApplications() {
return this.getAllApps(null,true);
}
public void updateIcon(PackageInfo pi) {
ContentValues values = new ContentValues();
values.put(COL_ICON,pi.applicationInfo.icon);
this.dbHelper.getWritableDatabase().update(TABLE_NAME, values, COL_NAME + "=?" ,new String[]{pi.packageName});
}
public List<Application> getAllApplications(boolean withDrawables) {
return this.getAllApps(null,withDrawables);
}
public List<Application> getAllApplications(PostProgess progress) {
return this.getAllApps(progress,true);
}
private List<Application> getAllApps(PostProgess progress,boolean withDrawables) {
List<Application> ret = new ArrayList<Application>();
//ALTERPOINT5
Cursor cursor = this.dbHelper.getReadableDatabase().query(TABLE_NAME,null,null,null,null,null,null);
if(!cursor.moveToFirst()) return ret;
int total = cursor.getCount();
int current = 0;
while(!cursor.isAfterLast()) {
if(progress != null) {
progress.postProgress(current,total);
}
Application app;
try {
app = this.cursorToApplication(cursor,withDrawables);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
//Log.e("UERR",e.getMessage());
continue;
}finally {
cursor.moveToNext(); //very imp do not forgot again
}
ret.add(app);
current++;
}
return ret;
}
public ArrayList<Application> insertAll(List<PackageInfo> pks,PostProgess progress) {
ArrayList<Application> appList = new ArrayList<Application>();
int current = 0;
int max = appList.size();
for(PackageInfo pi : pks) {
try {
appList.add(this.createApplication(pi));
progress.postProgress(current, max);
}catch(Exception e){
Log.e("Package Skiped",e.getMessage());
continue;
}finally {
current++;
}
}
return appList;
}
public Application cursorToApplication(Cursor cursor,boolean withDrawables) throws NameNotFoundException {
Application app = new Application();
ApplicationInfo appInfo;
if(withDrawables) {
appInfo = this.context.getPackageManager().getPackageInfo(cursor.getString(cursor.getColumnIndex(COL_PACKAGE)), 0).applicationInfo;
Drawable image = null;
try {
image = this.context.getPackageManager().getResourcesForApplication(appInfo).getDrawable(cursor.getInt(cursor.getColumnIndex(COL_ICON)));
}catch(NotFoundException e) {
image = this.context.getResources().getDrawable(android.R.drawable.ic_dialog_info);
}
app.setImage(image);
Log.e("uhaish", "Drawbles Included");
}else {
Log.e("uhaish", "Drawbles skiped");
}
app.set_id(cursor.getLong(cursor.getColumnIndex(COL_ID)));
app.setCategory_id(cursor.getInt(cursor.getColumnIndex(COL_CATEGORY_ID)));
app.setIcon(cursor.getInt(cursor.getColumnIndex(COL_ICON)));
app.setPackageName(cursor.getString(cursor.getColumnIndex(COL_PACKAGE)));
app.setName(cursor.getString(cursor.getColumnIndex(COL_NAME)));
app.setOrientation(cursor.getString(cursor.getColumnIndex(COL_ORIENTATION)));
return app;
}
public Application updateOrientation(Application app,String orientation) {
ContentValues cv = new ContentValues();
cv.put(COL_ORIENTATION,orientation);
this.dbHelper.getWritableDatabase().update(TABLE_NAME,cv, "_id=" + app.get_id(),null);
app.setOrientation(orientation);
return app;
}
public Application updateCategory(Application app,long catId) {
ContentValues cv = new ContentValues();
cv.put(COL_CATEGORY_ID,catId);
this.dbHelper.getWritableDatabase().update(TABLE_NAME,cv, "_id=" + app.get_id(),null);
return app;
}
public void removeApplication(PackageInfo pi){
this.dbHelper.getWritableDatabase().delete(TABLE_NAME, COL_PACKAGE + "=?",new String[]{pi.packageName});
}
public void removeApplication(String packageName){
this.dbHelper.getWritableDatabase().delete(TABLE_NAME, COL_PACKAGE + "=?" ,new String[]{packageName});
}
public interface PostProgess {
public void postProgress(int current,int maximum);
}
private boolean isSystemPackage(PackageInfo pk){
//return false;
return (pk.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)!=0;
}
public int update(String where,ContentValues values) {
return this.dbHelper.getWritableDatabase().update(TABLE_NAME, values,where,null);
}
public void close() {
this.dbHelper.close();
}
}
ChangePackageReciever Class
package com.taimoor.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.preference.PreferenceManager;
import android.util.Log;
import com.taimoor.activity.PrefActivity;
import com.taimoor.db.ApplicationTb;
import com.taimoor.service.MainService;
public class ChangePackageReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String packageName = intent.getDataString().substring(8);
Log.e("uhaish", "Package change occured action " + packageName);
try {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
ApplicationTb appTb = new ApplicationTb(context);
if(intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
PackageInfo pi = context.getPackageManager().getPackageInfo(packageName, 0);
appTb.createApplication(pi);
pref.edit().putBoolean("package_change", true).commit();
Log.d("uhaish", "package added");
}else if(intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
appTb.removeApplication(packageName);
pref.edit().putBoolean("package_change", true).commit();
Log.d("uhaish","package removed");
}else if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
PackageInfo pi = context.getPackageManager().getPackageInfo(packageName, 0);
appTb.updateIcon(pi);
//package updated check that icon if it's changed
//appTb.
Log.d("uhaish", "package replaced");
}
this.refreshService(context, pref);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("boot recieer", e.getMessage());
}
}
public void refreshService(Context context,SharedPreferences pref) {
if(pref.getBoolean(PrefActivity.PREF_ENABLE_SERVICE, true)) {
Intent mainService = new Intent(context,MainService.class);
mainService.setAction(MainService.ACTION_SYNC_WITH_DB);
context.startService(mainService);
}
}
}
我是android新手,很抱歉低级错误
答案 0 :(得分:0)
尝试更改此内容:
public static String getCreateQuery() {
return "CREATE TABLE application (\n" +
" \"_id\" INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
" \"package\" TEXT,\n" +
" \"orientation\" TEXT,\n" +
" \"category_id\" INTEGER NOT NULL DEFAULT (1),\n" +
" \"icon\" INTEGER,\n" +
" \"name\" TEXT\n" +
")\n" +
"";
}
对此:
public static String getCreateQuery()
{
return "CREATE TABLE application " +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"package TEXT, " +
"orientation TEXT, " +
"category_id INTEGER NOT NULL DEFAULT (1), " +
"icon INTEGER, " +
"name TEXT)"
}
我删除了所有\“和\ n字符。现在它应该正常工作。
因为,例如:此\"_id\"
会错误地尝试插入"_id"
而非_id
。
此TEXT\n
不是有效的SQLite类型,而此TEXT
是