I'm new to android, and woking on webservices that is parsing my web page data using JSON, followed androidhive json parser tutorial.. right now able to parse the title but not the Image, checked with logcat but even there no error. Feeling difficult to find the issue without error, Help will be Appreciated, Much Thanks
public class MainActivity extends Activity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String TITLE = "title";
static String ALTERNATE = "alternate";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Android JSON Parse Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("https://www.facebook.com/feeds/page.php?format=json&id=APP_ID");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("entries");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("title", jsonobject.getString("title"));
map.put("alternate", jsonobject.getString("alternate"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
SingleListview.activity
public class SingleItemView extends Activity {
// Declare Variables
String title;
String position;
ImageLoader imageLoader = new ImageLoader(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from singleitemview.xml
setContentView(R.layout.singleitemview);
Intent i = getIntent();
// Get the result of rank
title = i.getStringExtra("title");
alternate = i.getStringExtra("alternate");
// Locate the TextViews in singleitemview.xml
TextView txttitle = (TextView) findViewById(R.id.rank);
// Locate the ImageView in singleitemview.xml
ImageView imgalternate = (ImageView) findViewById(R.id.flag);
// Set results to the TextViews
txttitle.setText(title);
imageLoader.DisplayImage(alternate, imgalternate);
}
But when clicked on the ImageView getting NullPointer Exception. Logcat Output
02-04 11:47:28.078: E/AndroidRuntime(706): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.androidbegin.jsonparsetutorial/com.androidbegin.jsonparsetutorial.SingleItemView}: java.lang.NullPointerException
02-04 11:47:28.078: E/AndroidRuntime(706): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-04 11:47:28.078: E/AndroidRuntime(706): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-04 11:47:28.078: E/AndroidRuntime(706): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-04 11:47:28.078: E/AndroidRuntime(706): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-04 11:47:28.078: E/AndroidRuntime(706): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 11:47:28.078: E/AndroidRuntime(706): at android.os.Looper.loop(Looper.java:123)
02-04 11:47:28.078: E/AndroidRuntime(706): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-04 11:47:28.078: E/AndroidRuntime(706): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:47:28.078: E/AndroidRuntime(706): at java.lang.reflect.Method.invoke(Method.java:507)
02-04 11:47:28.078: E/AndroidRuntime(706): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-04 11:47:28.078: E/AndroidRuntime(706): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-04 11:47:28.078: E/AndroidRuntime(706): at dalvik.system.NativeStart.main(Native Method)
02-04 11:47:28.078: E/AndroidRuntime(706): Caused by: java.lang.NullPointerException
02-04 11:47:28.078: E/AndroidRuntime(706): at android.content.ContextWrapper.getCacheDir(ContextWrapper.java:188)
02-04 11:47:28.078: E/AndroidRuntime(706): at com.androidbegin.jsonparsetutorial.FileCache.<init>(FileCache.java:18)
02-04 11:47:28.078: E/AndroidRuntime(706): at com.androidbegin.jsonparsetutorial.ImageLoader.<init>(ImageLoader.java:34)
02-04 11:47:28.078: E/AndroidRuntime(706): at com.androidbegin.jsonparsetutorial.SingleItemView.<init>(SingleItemView.java:17)
02-04 11:47:28.078: E/AndroidRuntime(706): at java.lang.Class.newInstanceImpl(Native Method)
02-04 11:47:28.078: E/AndroidRuntime(706): at java.lang.Class.newInstance(Class.java:1409)
02-04 11:47:28.078: E/AndroidRuntime(706): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-04 11:47:28.078: E/AndroidRuntime(706): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-04 11:47:28.078: E/AndroidRuntime(706): ... 11 more