SQLiteAssetHelper中的SharedPreferences空对象引用

时间:2018-09-22 12:25:34

标签: android listview android-sqlite sharedpreferences

我尝试使用sharedpreferences fetch对我的SQliteDatabase进行排序,当我在数据库类上添加sharedpreference时,它显示了空对象引用,我不知道该如何解决,有人可以帮忙吗? 我在下面也张贴了我的logcat,请检查。

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.preference.PreferenceManager;
import com.sample.quotes.QuoteActivity;
import com.sample.quotes.R;
import com.sample.quotes.datacon.QuoteData;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
import java.util.ArrayList;
import java.util.List;
import static android.content.Context.MODE_PRIVATE;

public class Database extends SQLiteAssetHelper{

public static final String DB_NAME="quote.db";
public static final int DB_VER=1;

PreferenceManager preferenceManager;

public Database(Context context) {
    super(context, DB_NAME, null, DB_VER);

}

public List<QuoteData> getQuotes()
{
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[] sqlSelect={"Id","Details","Headings","Categories"};

    String tableName="TableOne";

    qb.setTables(tableName);

    SharedPreferences textColorPref = preferenceManager.getSharedPreferences();
    String checked = textColorPref.getString("quotesSort", "");

    if(checked.equals("")){
        Cursor cursor = qb.query(db,sqlSelect,null,null,null,null,checked);
        List<QuoteData> result = new ArrayList<>();
        if(cursor.moveToFirst())
        {
            do{
                QuoteData quotedata = new QuoteData();
                quotedata.setId(cursor.getInt(cursor.getColumnIndex("Id")));
                quotedata.setDetails(cursor.getString(cursor.getColumnIndex("Details")));
                quotedata.setHeadings(cursor.getString(cursor.getColumnIndex("Headings")));
                quotedata.setCategories(cursor.getString(cursor.getColumnIndex("Categories")));


                result.add(quotedata);
            }while (cursor.moveToNext());
        }
        return result;
    } else {
        Cursor cursor = qb.query(db,sqlSelect,null,null,null,null,null);
        List<QuoteData> result = new ArrayList<>();
        if(cursor.moveToFirst())
        {
            do{
                QuoteData quotedata = new QuoteData();
                quotedata.setId(cursor.getInt(cursor.getColumnIndex("Id")));
                quotedata.setDetails(cursor.getString(cursor.getColumnIndex("Details")));
                quotedata.setHeadings(cursor.getString(cursor.getColumnIndex("Headings")));
                quotedata.setCategories(cursor.getString(cursor.getColumnIndex("Categories")));
                result.add(quotedata);
            }while (cursor.moveToNext());
        }
        return result;
    }
}
public List<QuoteData> getQuotesByDetail(String details)
{
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

String[] sqlSelect={"Id","Details","Headings","Categories"};
String tableName="TableOne";

qb.setTables(tableName);
//  Cursor cursor = qb.query(db,sqlSelect,"Name = ?",new String[] 
{name},null,null,null);

Cursor cursor = qb.query(db,sqlSelect,"Details LIKE ?",new String[] 
{"%"+details+"%"},null,null,null);
List<QuoteData> result = new ArrayList<>();
if(cursor.moveToFirst())
{
    do{
        QuoteData quotedata = new QuoteData();
        quotedata.setId(cursor.getInt(cursor.getColumnIndex("Id")));
        quotedata.setDetails(cursor.getString(cursor.getColumnIndex("Details")));
        quotedata.setHeadings(cursor.getString(cursor.getColumnIndex("Headings")));
        quotedata.setCategories(cursor.getString(cursor.getColumnIndex("Categories")));

        result.add(quotedata);
    }while (cursor.moveToNext());
}
return result;
}
}

堆栈跟踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{QuoteActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.preference.PreferenceManager.getSharedPreferences()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2439)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2499)
        at android.app.ActivityThread.access$900(ActivityThread.java:166)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5468)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.preference.PreferenceManager.getSharedPreferences()' on a null object reference
        at com.sample.quotes.database.Database.getQuotes(Database.java:45)
        at com.sample.quotes.QuoteActivity.onCreate(QuoteActivity.java:81)
        at android.app.Activity.performCreate(Activity.java:6556)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2392)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2499) 
        at android.app.ActivityThread.access$900(ActivityThread.java:166) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5468) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

2 个答案:

答案 0 :(得分:0)

您没有在代码中的任何地方初始化preferenceManager,因此在getQuotes()中给出了空指针异常

答案 1 :(得分:0)

preferenceManager从未被初始化,除非您位于PreferenceFragment中,否则通常不是您想要获取SharedPreferences实例的方式。

相反,您应该使sharedPreferences成为全局变量,并在构造函数中对其进行初始化:

private SharedPreferences sharedPreferences;

public Database(Context context) {
    super(context, DB_NAME, null, DB_VER);
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(); //or however you initialize the SharedPreferences you want elsewhere
}