当我尝试获取我保存的数据时,我有一个NullPointerException。我认为它在我的SqlHelper类中导致错误的“打开”方法。它与错误的上下文相关联。因为当我使用Activity时所有工作,现在我使用Fragment并且有这个错误。 错误:
10-10 09:23:03.009 7657-7657/com.example.vnvbnv.myapplication E/AndroidRuntime: Caused by: java.lang.NullPointerException
10-10 09:23:03.009 7657-7657/com.example.vnvbnv.myapplication E/AndroidRuntime: at com.example.vnvbnv.myapplication.MainList.displaysavedlv(MainList.java:168)
10-10 09:23:03.009 7657-7657/com.example.vnvbnv.myapplication E/AndroidRuntime: at com.example.vnvbnv.myapplication.MainList.onActivityCreated(MainList.java:56)
MainList(导致错误的行我将用“****”标记):
public class MainList extends ListFragment{
SqlHelper dbHelper;
ListView mainList;
ProgressBar progBar;
private static String url = "https://fierce-citadel-4259.herokuapp.com/hamsters";
private static final String TITLE = "title";
private static final String DESCRIPTION = "description";
private static final String IMAGE = "image";
ArrayList<HashMap<String,String>> jsonlist1 = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String,String>> bdList = new ArrayList<HashMap<String, String>>();
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.list_fragme, null);
mainList = (ListView)v.findViewById(android.R.id.list);
progBar = (ProgressBar) v.findViewById(R.id.progressBar);
return v;
}
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
if (isNetworkConnected()==true) {
new ProgressTask().execute();
}
else{
displaysavedlv();***** and this!
}
}
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
super.onListItemClick(l, view, position, id);
String title = jsonlist1.get(position).get("title");
String description= jsonlist1.get(position).get("description");
String image = jsonlist1.get(position).get("image");
MyDetailFragment detailFragment = new MyDetailFragment();
Bundle bundle = new Bundle();
if(isNetworkConnected()==true) {
bundle.putString("title", title);
bundle.putString("description", description);
bundle.putString("image", image);
}
else
{
String dbtitle= bdList.get(position).get("title");
String dbdescription = bdList.get(position).get("description");
String dvimage = bdList.get(position).get("image");
bundle.putString("title",dbtitle);
bundle.putString("description",dbdescription);
bundle.putString("image",dvimage);
}
detailFragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = getActivity().getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.FragmentCont,detailFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
private class ProgressTask extends AsyncTask<String,Void,Boolean> {
private ProgressDialog dialog;
private Activity activity;
private MainActivity context;
private String[] params;
public ProgressTask(MainActivity activity) throws SQLException {
this.activity = getActivity();
context = activity;
dialog = new ProgressDialog(getActivity().getApplicationContext());
dbHelper = new SqlHelper(getActivity().getApplicationContext());
}
public ProgressTask() {
dialog = new ProgressDialog(getActivity().getApplicationContext());
try {
dbHelper = new SqlHelper(getActivity().getApplicationContext());
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
protected Boolean doInBackground(String... params) {
this.params = params;
JSONParser jParser = new JSONParser();
JSONArray json = jParser.getJSONFromUrl(url);
for(int i =0;i<json.length();i++) {
try {
JSONObject c = json.getJSONObject(i);
String vtitle = c.getString(TITLE);
String vdescription = c.getString(DESCRIPTION);
String vimage = c.getString(IMAGE);
dbHelper.open();
dbHelper.createEntry(vtitle, vimage, vdescription);
dbHelper.close();
HashMap<String, String> map = new HashMap<>();
map.put(TITLE, vtitle);
map.put(DESCRIPTION, vdescription);
map.put(IMAGE, vimage);
jsonlist1.add(map);
} catch (JSONException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
protected void onPreExecute(){
progBar.setVisibility(View.VISIBLE);
}
protected void onPostExecute(final Boolean success){
if (isNetworkConnected()==true) {
progBar.setVisibility(View.GONE);
CustomListAdapter adapter = new CustomListAdapter(getActivity(), jsonlist1, R.layout.list_item, new String[]{TITLE, DESCRIPTION}, new int[]{R.id.title, R.id.description});
mainList.setAdapter(adapter);
}
else {
displaysavedlv();
}
}
}
private void displaysavedlv() {
bdList = dbHelper.getAllData();****(this one)
CustomListAdapter adapter1 = new CustomListAdapter(getActivity(), bdList, R.id.list_item, new String[]{TITLE, DESCRIPTION}, new int[]{R.id.title, R.id.description});
mainList.setAdapter(adapter1);
}
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
return false;
} else
return true;
}
}
和我在SqlHelper和构造函数中的open方法(我觉得问题在这里):
public SqlHelper(Context c) throws SQLException {
ourContext = c;
try {
open();
} catch (SQLException e) {
e.printStackTrace();
}
}
public SqlHelper open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}