0

当我从 Fragment 获取 asyntake 并按如下方式访问 sqlite 时出现以下错误。

引起:android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 处的 java.lang.NullPointerException

这是我的编码。即使我已经声明了“DatabaseUtil dbUtil = new DatabaseUtil(ctx);”,我也不知道为什么会出现这个错误

  public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        .
        ..

    }



    public static class ContentFragment extends Fragment {


        public ContentFragment() {

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.list_layout, container, false);
            MainActivity myActivity = new MainActivity();
            MainActivity.GetDataTask asyncTask = myActivity.new GetDataTask(); 

            return rootView;
        }
    }    

    private class GetDataTask extends AsyncTask<Object, Void, Integer> {

        protected void onPreExecute() {

        }       

        @Override
        protected Integer doInBackground(Object... args) {

            DatabaseUtil dbUtil = new DatabaseUtil(ctx);
                dbUtil.open();
                        dbUtil.insertRecord(....);
                dbUtil.close();

            return 1;
        }

        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
        }
    } 
4

2 回答 2

1

你在做什么是错的

    MainActivity myActivity = new MainActivity();

您正在创建活动类的实例。活动由 启动startActivtiy(new Intent(params))。Activity 有 ui 和生命周期。因此不需要创建实例 od 活动类。

建议在您的片段类中使用 asynctask

   DatabaseUtil dbUtil = new DatabaseUtil(getActivtiy());

到已编辑的问题

  public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    .
    ..

  }

然后在 ContentFragment.java

    public class ContentFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.list_layout, container, false);
         new GetDataTask(getActivity()).execute(params);
        return rootView;
    }
}    

 class GetDataTask extends AsyncTask<Object, Void, Integer> {

    Context mContext;  
   public GetDataTask(Activity activity) {
    // TODO Auto-generated constructor stub
    mContext = activity;
}

    @Override 
    protected void onPreExecute() {
        super.onPreExecute();
    }       

    @Override
    protected Integer doInBackground(Object... args) {

        DatabaseUtil dbUtil = new DatabaseUtil(mContext);
            dbUtil.open();
                    dbUtil.insertRecord(....);
            dbUtil.close();

        return 1;
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
    }
 }
于 2013-09-27T09:14:02.090 回答
0

您可以为GetDataTask制作构造函数....

Context ctx;

public GetDataTask(Context c){

 this.ctx = c;

}

现在你可以使用这个ctx变量

DatabaseUtil dbUtil = new DatabaseUtil(ctx);
于 2013-09-27T09:34:22.457 回答