Android NullPointerException(getDatabaseLocked& getWritableDatabase)

时间:2015-01-13 11:56:23

标签: java android sqlite android-fragments

我想使用Listview在SQLite数据库中显示数据。我使用Fragment来获取listview。但现在,我有一个问题:

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());

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

2 个答案:

答案 0 :(得分:0)

将此代码用于InformListAdapter类只有一个错误,

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;
    }

}

答案 1 :(得分:0)

像这样修改:

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创建对象,但是在附加到活动之前将初始化该对象。在那种情况下,getActivity()方法返回null,因此不会计算出数据库。

然后最佳实践是,如果您希望dbhelper类对象在

中全局初始化该对象

OnAttach()片段。