0
public abstract class AbstractIME extends InputMethodService implements 
KeyboardView.OnKeyboardActionListener, CandidateView.CandidateViewListener {
    ...
    private void clearCandidates() {
        ...
        final dbhelper helper = new dbhelper(this);
        Cursor c = helper.query();
    }
    ...
}

public class CangjieDictionary extends WordDictionary {
    private AbstractIME ime;
    ....
    private String sortWords(char[] data) {
        ....
        dbhelper dbh = new dbhelper(ime); ;
        Cursor c = dbh.query();
    }
}

这里有两个类。第一个没问题,但第二个代码相同

以下是 logcat 消息为什么第二类有空指针?

FATAL EXCEPTION: main
  java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
at com.googlecode.tcime.unofficial.dbhelper.query(dbhelper.java:50)
at com.googlecode.tcime.unofficial.CangjieDictionary.sortWords(CangjieDictionary.java:111)
4

1 回答 1

0

WordDictionary声明一个 Context 成员并创建一个具有 Context 参数的构造函数

Context mContext;

public WordDictionary(Context context)
{
     mContext = context:
}  

然后在CangjieDictionary

public class CangjieDictionary extends WordDictionary {
private AbstractIME ime;
....
private String sortWords(char[] data) {
    ....
    dbhelper dbh = new dbhelper(mContext);
    Cursor c = dbh.query();
}

}

于 2013-04-15T18:08:58.550 回答