当我从 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);
}
}