I'm having issues populating my listview. I thought I had modifed my XML and adapters accordingly but apparently not. When the activity is loaded the dialog pops up stating it's loading then the app force closes. If i remove the 3 lines of coding stating listview.setText(name) then it just loads all the items with the default text in my XML file. My code looks like this:
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.JsonReader;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class DisplayServiceActivity extends ListActivity {
private ListView listOfServices;
//JSONArrays?
JSONArray directory;
//JSON Node names
private static String TAG_ID = "id";
private static String TAG_NAME= "name";
private static String TAG_DIRECTORY = "Categories";
private final static String url= "API LINK HERE";
JSONObject json;
jsonParser jParser = new jsonParser();
ArrayList> directoryList;
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Request request = new Request();
request.execute();
listOfServices =getListView(); //get builtin listView
directoryList = new ArrayList>();
ListAdapter adapter = new SimpleAdapter(this,
directoryList,
R.layout.list_item,
new String[] { TAG_ID,TAG_NAME },
new int[] { android.R.id.text1,android.R.id.text2 });
setListAdapter(adapter);
setContentView(R.layout.service);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}// end of onCreate Method
@SuppressWarnings("unused")
public class Request extends AsyncTask {
private static final int REGISTRATION_TIMEOUT = 3 * 1000;
private static final int WAIT_TIMEOUT = 30 * 1000;
private ProgressDialog dialog =
new ProgressDialog(DisplayServiceActivity.this);
protected void onPreExecute() {
dialog = new ProgressDialog(DisplayServiceActivity.this);
dialog.setMessage("Getting your info real quick... Please wait...");
dialog.show();
}
protected JSONObject doInBackground(String... params) {
json = jParser.getJSONfromURL(url);
return json;
}
protected void onPostExecute(JSONObject s) {
super.onPostExecute(s);
String name = null;
String id = null;
dialog.dismiss();
try {
directory = s.getJSONArray("Categories");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i = 0; i< directory.length(); i++){;
try {
id = directory.getJSONObject(i).getString(TAG_ID);
name = directory.getJSONObject(i).getString(TAG_NAME);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
displayCatList(id, name);
}
}
}
public void displayCatList(String id, String name){
//create new HashMap
HashMap map = new HashMap();
//add each child node to HashMap key
//map.put(TAG_ID, id);
map.put(TAG_NAME, name);
//adding HashList to ArrarList
directoryList.add(map);
// set Adapter for ListView here
ListAdapter adapter = new SimpleAdapter(this,
directoryList,
R.layout.list_item,
new String[] { id,name },
new int[] { android.R.id.text1,android.R.id.text2 });
TextView listName = (TextView) findViewById(R.id.listName);
Log.e("XML Variable", name);
listName.setText(name);
setListAdapter(adapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
List_item.xml
android:id="@+id/listName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="rubble rubble rubble"
android:textColor="#FFFFFF"
android:padding="10dip"
android:textSize="20dip"
android:textStyle="bold" >
LogCat:
05-12 23:47:28.395: I/Adreno200-EGLSUB(4402): : Format RGBA_8888.
05-12 23:47:29.006: E/XML Variable(4402): Glass Repair
05-12 23:47:29.006: W/dalvikvm(4402): threadid=1: thread exiting with uncaught exception (group=0x416bf438)
05-12 23:47:29.006: E/AndroidRuntime(4402): FATAL EXCEPTION: main
05-12 23:47:29.006: E/AndroidRuntime(4402): java.lang.NullPointerException
05-12 23:47:29.006: E/AndroidRuntime(4402): at com.example.hstnc_activity.DisplayServiceActivity.displayCatList(DisplayServiceActivity.java:158)
05-12 23:47:29.006: E/AndroidRuntime(4402): at com.example.hstnc_activity.DisplayServiceActivity$Request.onPostExecute(DisplayServiceActivity.java:127)
05-12 23:47:29.006: E/AndroidRuntime(4402): at com.example.hstnc_activity.DisplayServiceActivity$Request.onPostExecute(DisplayServiceActivity.java:1)
05-12 23:47:29.006: E/AndroidRuntime(4402): at android.os.AsyncTask.finish(AsyncTask.java:631)
05-12 23:47:29.006: E/AndroidRuntime(4402): at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-12 23:47:29.006: E/AndroidRuntime(4402): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-12 23:47:29.006: E/AndroidRuntime(4402): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 23:47:29.006: E/AndroidRuntime(4402): at android.os.Looper.loop(Looper.java:137)
05-12 23:47:29.006: E/AndroidRuntime(4402): at android.app.ActivityThread.main(ActivityThread.java:4918)
05-12 23:47:29.006: E/AndroidRuntime(4402): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 23:47:29.006: E/AndroidRuntime(4402): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 23:47:29.006: E/AndroidRuntime(4402): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-12 23:47:29.006: E/AndroidRuntime(4402): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-12 23:47:29.006: E/AndroidRuntime(4402): at dalvik.system.NativeStart.main(Native Method)