Android NullPointerException(getDatabaseLocked & getWritableDatabase)

Android NullPointerException (getDatabaseLocked & getWritableDatabase)

我想使用Listview 来显示SQLite 数据库中的数据。我使用片段来获取列表视图。但是现在,我遇到了一个问题:

This is the Log info:
E/AndroidRuntime(12816): FATAL EXCEPTION: main
E/AndroidRuntime(12816): java.lang.NullPointerException
E/AndroidRuntime(12816):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
E/AndroidRuntime(12816):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
E/AndroidRuntime(12816):    at com.tax_ui.sql.Inform_dbHandler.getAllInform(Inform_dbHandler.java:80)
E/AndroidRuntime(12816):    at com.tax_ui.fragment.InformFragment.init(InformFragment.java:72)
E/AndroidRuntime(12816):    at com.tax_ui.fragment.InformFragment.onCreateView(InformFragment.java:55)

这是列表适配器代码:

public class InformListAdapter extends BaseAdapter {

    private Activity activity;
    private LayoutInflater inflater;
    private List<Inform_item> inform_item_list;

    public InformListAdapter(Activity activity,
            List<Inform_item> inform_item_list) {
        this.activity = activity;
        this.inform_item_list = inform_item_list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return inform_item_list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return inform_item_list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.fragment_inform_item, null);

        TextView title = (TextView) convertView.findViewById(R.id.inform_title);
        TextView timestamp = (TextView) convertView
                .findViewById(R.id.inform_timestamp);
        TextView content = (TextView) convertView
                .findViewById(R.id.inform_content);

        Inform_item inform_item = inform_item_list.get(position);

        title.setText(inform_item.get_title());
        timestamp.setText(inform_item.get_timestamp());
        content.setText(inform_item.get_content());

        return null;
    }

}

然后,这是我的片段代码:

public class InformFragment extends Fragment {

    private View mBaseView;
    private TitleBarView mTitleBarView;
    private ListView listView;
    private InformListAdapter listAdapter;
    private List<Inform_item> inform_item_list;

    private Inform_dbHandler inform_dbHandler  = new Inform_dbHandler(getActivity());

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        getActivity();
        mBaseView=inflater.inflate(R.layout.fragment_inform_list, null);
        findView();
        init();

        return mBaseView;       
    }

    private void findView(){
        mTitleBarView=(TitleBarView) mBaseView.findViewById(R.id.title_bar);
        listView = (ListView)mBaseView.findViewById(R.id.inform_list);
    }

    private void init(){
        mTitleBarView.setCommonTitle(View.GONE, View.VISIBLE, View.GONE);
        mTitleBarView.setTitleText(R.string.inform);

        inform_item_list = new ArrayList<Inform_item>();
        inform_item_list = inform_dbHandler.getAllInform();

        listAdapter = new InformListAdapter(getActivity(), inform_item_list);
        listView.setAdapter(listAdapter);
    }

}

我认为这行可能是错误的:

private Inform_dbHandler inform_dbHandler  = 
    new Inform_dbHandler(getActivity());

但是我不知道如何解决这个问题?你能帮助我吗?谢谢~

使用此代码 InformListAdapter class 你只有一个错误,

public class InformListAdapter extends BaseAdapter {

    private Activity activity;
    private LayoutInflater inflater;
    private List<Inform_item> inform_item_list;

    public InformListAdapter(Activity activity,
            List<Inform_item> inform_item_list) {
        this.activity = activity;
        this.inform_item_list = inform_item_list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return inform_item_list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return inform_item_list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.fragment_inform_item, null);

        TextView title = (TextView) convertView.findViewById(R.id.inform_title);
        TextView timestamp = (TextView) convertView
                .findViewById(R.id.inform_timestamp);
        TextView content = (TextView) convertView
                .findViewById(R.id.inform_content);

        Inform_item inform_item = inform_item_list.get(position);

        title.setText(inform_item.get_title());
        timestamp.setText(inform_item.get_timestamp());
        content.setText(inform_item.get_content());

        return convertView;
    }

}

修改如下:

private Inform_dbHandler inform_dbHandler ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        getActivity();
        mBaseView=inflater.inflate(R.layout.fragment_inform_list, null);
        findView();
        init();
        Inform_dbHandler inform_dbHandler  = new Inform_dbHandler(getActivity());

        return mBaseView;       
    }

问题是您正在为 dbhandler 创建对象,但该对象将在附加到 activity 之前进行初始化。在那种情况下 getActivity() 方法返回 null,因此数据库将无法计算。

那么最佳做法是,如果您希望 dbhelper class 对象在

中全局初始化该对象

OnAttach() 个片段。