簡體   English   中英

運行時異常 Android 4.3/4.4

[英]Runtime Exception Android 4.3/4.4

你好我的代碼給出了以下兩個例外,我似乎無法識別或解決問題,我已經給出了我確定存在錯誤的類,如果你需要我的項目中的其他類[查看導入]讓我知道. 謝謝

注意 應用程序崩潰通常是在 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)

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 新手,很抱歉出現低級錯誤

嘗試改變這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM