运行时异常Android 4.3 / 4.4

泰穆尔阿里

您好我的代码给出了以下两个异常,但是我似乎无法识别或解决问题,如果您的项目中需要其他类,请给我确定错误存在的类[请参阅导入],让我知道。谢谢

注意通常在Google Play商店更新应用程序或使用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)

com.taimoor.db.ApplicationTb.createApplication中的java.lang.RuntimeException

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类

 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的新手,很抱歉出现低级错误

Phantômaxx

尝试更改此:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular 4中的运行时绑定

使用log4j记录Java中的运行时异常

cmu sphinx4 java-由FileNotFoundException引起的运行时异常

Junit4在测试运行时引发异常

运行时错误:4 维权重 X 的预期 4 维输入,但得到大小为 Y 的 3 维输入

__init __()接受3个位置参数,但运行时给出4个

如何在.NET 4运行时中运行PowerShell?

获取EF4的运行时错误

N + N / 2 + N / 4 ...个迭代运行时

Antlr4 c# 运行时错误

DirectX 11:使用XMLoadFloat4X4()的运行时断言失败

3列4里

SQUASHFS 3 vs 4

无法运行 Log4j2 演示类 - 将类作为 Java 应用程序运行时,屏幕上弹出 Java 异常

无法在Python 3中打印列表(range(4 ** 4 ** 4))

R for回路向量1,2,2,3,3,3,4,4,4,4,..,10,

为什么用Install4j 6构建的macOS安装程序会在JRE 10中抛出运行时异常?

T4在C#中的运行时期间运行或编译tt文件

Symfony 4:捕获异常

异常测试 - Junit 4

Log4net:如何在运行时启用调试日志记录?

如何在运行时更改slf4j级别?

使用Log4j2 2.8.1在运行时动态添加文件日志记录

如何使用launch4j与Java自定义运行时的形象?

覆盖运行时SLF4J绑定以进行测试

为什么`to_unsigned(0,4)> = -1`在运行时评估为“ FALSE”?

向v4.Fragment请求运行时权限,并让回调转到Fragment?

在slf4j中设置运行时消息的日志级别

配置log4j以在运行时登录到自定义文件

在运行时检索Log4J Appender的列表