I am parsing JSON weather api in Android from a url. I am successfully getting all the values that i am parsing in the application. But after displaying the values on emulator the app is crashing and i am getting runtime exception.
Here is the logcat errors that i am getting :
10-20 09:34:26.040: E/AndroidRuntime(748): FATAL EXCEPTION: main
10-20 09:34:26.040: E/AndroidRuntime(748): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.work/com.example.work.SingleItemView}: java.lang.NullPointerException
10-20 09:34:26.040: E/AndroidRuntime(748): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
10-20 09:34:26.040: E/AndroidRuntime(748): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-20 09:34:26.040: E/AndroidRuntime(748): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-20 09:34:26.040: E/AndroidRuntime(748): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-20 09:34:26.040: E/AndroidRuntime(748): at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 09:34:26.040: E/AndroidRuntime(748): at android.os.Looper.loop(Looper.java:137)
10-20 09:34:26.040: E/AndroidRuntime(748): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-20 09:34:26.040: E/AndroidRuntime(748): at java.lang.reflect.Method.invokeNative(Native Method)
10-20 09:34:26.040: E/AndroidRuntime(748): at java.lang.reflect.Method.invoke(Method.java:525)
10-20 09:34:26.040: E/AndroidRuntime(748): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-20 09:34:26.040: E/AndroidRuntime(748): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-20 09:34:26.040: E/AndroidRuntime(748): at dalvik.system.NativeStart.main(Native Method)
10-20 09:34:26.040: E/AndroidRuntime(748): Caused by: java.lang.NullPointerException
10-20 09:34:26.040: E/AndroidRuntime(748): at com.example.work.SingleItemView.<init>(SingleItemView.java:5)
10-20 09:34:26.040: E/AndroidRuntime(748): at java.lang.Class.newInstanceImpl(Native Method)
10-20 09:34:26.040: E/AndroidRuntime(748): at java.lang.Class.newInstance(Class.java:1130)
10-20 09:34:26.040: E/AndroidRuntime(748): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-20 09:34:26.040: E/AndroidRuntime(748): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
10-20 09:34:26.040: E/AndroidRuntime(748): ... 11 more
Here is my code.
MainActivity.java :
public class MainActivity extends Activity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String NAME = "id";
static String TYPE = "description";
static String child = "country";
static String child1 = "temp";
static String child2 = "temp_min";
static String child3 = "temp_max";
static String child4 = "pressure";
static String child5 = "humidity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.activity_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 message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void...arg0) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONParser
.getJSONfromURL("http://api.openweathermap.org/data/2.5/weather?q=Germany");
try {
JSONObject child=jsonobject.getJSONObject("sys");
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put(MainActivity.child, "Country - " +child.getString("country"));
arraylist.add(map1);
JSONObject child1=jsonobject.getJSONObject("main");
HashMap<String, String> map2 = new HashMap<String, String>();
map1.put(MainActivity.child1, "Temperature - " +child1.getString("temp"));
map1.put(MainActivity.child2, "Minimum Temperature - " +child1.getString("temp_min"));
map1.put(MainActivity.child3, "Maximum Temperature - " +child1.getString("temp_max"));
map1.put(MainActivity.child4, "Pressure - " +child1.getString("pressure"));
map1.put(MainActivity.child5, "Humidity - " +child1.getString("humidity"));
arraylist.add(map2);
// Locate the array name in JSON
JSONArray jsonarray=jsonobject.getJSONArray("weather");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrieve JSON Objects
map.put(MainActivity.NAME, "Id : "+jsonobject.getString("id"));
map.put(MainActivity.TYPE, "Description : "+jsonobject.getString("description"));
// 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();
}
}
}
SingleItemView.java :
package com.example.work;
import android.content.Intent;
public class SingleItemView {
Intent intent = getIntent();
String id = intent.getStringExtra("name");
String name = intent.getStringExtra("type");
String child = intent.getStringExtra("child");
String child1 = intent.getStringExtra("child1");
String child2 = intent.getStringExtra("child2");
String child3 = intent.getStringExtra("child3");
String child4 = intent.getStringExtra("child4");
String child5 = intent.getStringExtra("child5");
private Intent getIntent() {
// TODO Auto-generated method stub
return null;
}
}
ListViewAdapter.java :
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView id;
TextView description;
TextView country;
TextView temp;
TextView temp_min;
TextView temp_max;
TextView pressure;
TextView humidity;
//ImageView flag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
id = (TextView) itemView.findViewById(R.id.id);
description = (TextView) itemView.findViewById(R.id.description);
country = (TextView) itemView.findViewById(R.id.country);
temp = (TextView) itemView.findViewById(R.id.temp);
temp_min = (TextView) itemView.findViewById(R.id.temp_min);
temp_max = (TextView) itemView.findViewById(R.id.temp_max);
pressure = (TextView) itemView.findViewById(R.id.pressure);
humidity = (TextView) itemView.findViewById(R.id.humidity);
// Capture position and set results to the TextViews
id.setText(resultp.get(MainActivity.NAME));
description.setText(resultp.get(MainActivity.TYPE));
country.setText(resultp.get(MainActivity.child));
temp.setText(resultp.get(MainActivity.child1));
temp_min.setText(resultp.get(MainActivity.child2));
temp_max.setText(resultp.get(MainActivity.child3));
pressure.setText(resultp.get(MainActivity.child4));
humidity.setText(resultp.get(MainActivity.child5));
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, SingleItemView.class);
// Pass all id data
intent.putExtra("name", resultp.get(MainActivity.NAME));
// Pass all main data
intent.putExtra("type", resultp.get(MainActivity.TYPE));
intent.putExtra("child", resultp.get(MainActivity.child));
// Start SingleItemView Class
context.startActivity(intent);
}
});
return itemView;
}
}
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.work"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.work.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.work.SingleItemView"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
I am getting all the values that i am parsing in my application but i am not getting why is it crashing.