在片段中打开数据库的android -ullPointerException

时间:2015-06-21 14:09:16

标签: android android-fragments android-listview

我的活动有一个幻灯片,它有4个片段。每个片段都包含一个listView,我用baseAdapter填充这个listview。

我已经发布了我的应用程序,我非常感到这个错误:

STACK_TRACE = java.lang.NullPointerException
at android.content.ComponentName.<init>(ComponentName.java:75)
at android.content.Intent.<init>(Intent.java:3970)
at ir.radaar.goblin.Fragment_E$ListAdapter.<init>(Fragment_E.java:396)
at ir.radaar.goblin.Fragment_E$GetContacts.onPostExecute(Fragment_E.java:359)
at ir.radaar.goblin.Fragment_E$GetContacts.onPostExecute(Fragment_E.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

这是我的baseAdapter代码:

public class ListAdapter extends BaseAdapter {
    Activity activity;
    public ArrayList<HashMap<String, String>> list;
    DatabaseHandler db_e;

    public ListAdapter(Activity activity, ArrayList<HashMap<String, String>> list) {
        super();
        this.activity = (Activity) activity;
        this.list = list;
        try {
                db_e = new DatabaseHandler(getActivity());
                if (!db_e.isOpen())
                    db_e.open();

        } catch (Exception e) {
            startActivity(new Intent (getActivity(), FistActiivty.class));
        }
        ostan = getResources().getStringArray(R.array.ostan);
    }

    public int getCount() {
        return list.size();

    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public HashMap<String, String> geting(int position) {
        return list.get(position);
    }

    public long getItemId(int arg0) {
        return 0;
    }

    private class ViewHolder {
        ImageView img;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater = activity.getLayoutInflater();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.ads_ekhtesasi, null);
            holder = new ViewHolder();
            holder.img = (ImageView) convertView.findViewById(R.id.img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        return convertView;
    }

    public void clear() {
        if (this.list.size() > 0)
            this.list.clear();
    }
}

Fragment_E.java第396行是这样的: db_e.open();

我不知道为什么会出错。其他片段上的同一行也会出错。

在databaseHandler类上,这是打开数据库的类:

public DatabaseHandler open() {
    helper = new OpenHelper(context);
    myDB = helper.getWritableDatabase();
    return this;
}
你能帮助我吗?为什么我会收到此错误?

谢谢

0 个答案:

没有答案