I am parsing JSON from a URL & displaying it in a list view(ofc using a Async Task) , that works fine but now when i try to add a preference in the Async Task , the program gives a error.
Here is my code till now :
public class VideoList extends ListActivity {
ArrayAdapter<String> adapter;
ListView lv;
PrefMethods pm= new PrefMethods();;
int vid_id;
public void getJson(){
new LoadJsonTask().execute();
}
private class LoadJsonTask extends AsyncTask<Void, Void, ArrayList<String>>{
ProgressDialog dialog ;
protected void onPreExecute (){
dialog = ProgressDialog.show(VideoList.this ,"Loading....","Retrieving Data,Please Wait !");
}
protected ArrayList<String> doInBackground(Void[] params) {
return populate();
}
protected void onPostExecute(ArrayList<String> waveData) {
adapter=new ArrayAdapter(
VideoList.this,android.R.layout.simple_list_item_1,
waveData);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
dialog.dismiss();
};
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.parser);
lv=getListView();
//vid_id=pm.loadprefs();
vid_id=0;
getJson();
}
public ArrayList<String> populate() {
ArrayList<String> items = new ArrayList<String>();
try {
URL urlnew= new URL("www.mydummyjsonlinkhere.com");
HttpURLConnection urlConnection =
(HttpURLConnection) urlnew.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// gets the server json data
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String next;
while ((next = bufferedReader.readLine()) != null){
JSONArray ja = new JSONArray(next);
vid_id=pm.loadprefs();// here i load the preferences & error is occured
int k=ja.length();
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
WaveData waveData = new WaveData(jo.getString("upload"), jo.getInt("id"),jo.getString("link"),jo.getString("custommsg"));
if(jo.getInt("id")>vid_id){
if(i==k-1){
pm.saveprefs(jo.getInt("id"));
Toast.makeText(getApplicationContext(), "Saved pref no :"+jo.getInt("id"), 500).show();
}else{}
}else{}
if(jo.has("id")){
items.add(jo.getString("custommsg"));
}
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return items;
}
}
here is the code for my Preferences class:
public class PrefMethods extends Activity {
SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs=getPreferences(MODE_PRIVATE);
}
public void saveprefs(int h){
SharedPreferences.Editor editor=prefs.edit();
editor.putInt("id", h);
editor.commit();
}
public void saveprefs(boolean b){
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("inserted", b);
editor.commit();
}
public int loadprefs(){
int v=prefs.getInt("id",0);
return v;
}
public boolean loadprefsboolean(){
boolean v=prefs.getBoolean("inserted", false);
return v;
}
}
My Logcat error:
The error is a Null Pointer Exception at the following line in PrefMethods Class:
int v=prefs.getInt("id",0);
Can anyone help me & point me out where i am going wrong?
Thank You
Edit: I have added Internet permissions in Manifest & the JSON is getting parsed perfectly fine without the preferences methods.
prefs is not being set to anything as onCreate is never called.
Activity subclasses are for Activities not Preferences.
Create a PrefMethods class which does not extend Activity, but takes a Context in the constructor.
The PrefMethods class should then use that context to access the Preferences.
Something like this