I have following fragment
public class Tab2 extends Fragment {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> contactsList;
// url to get all contacts list
//private static String url_all_contacts = "http://192.168.100.28/andriod_product_demo/get_all_contacts.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_TYPE = "type";
ListView lv;
//..get the user id from the tinydb
TinyDB objTDB;
// contacts JSONArray
JSONArray contacts = null;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab2,container,false);
objTDB=new TinyDB(getActivity());
// Hashmap for ListView
contactsList = new ArrayList<HashMap<String, String>>();
// Get listview
//lv = getListView();
lv = (ListView) v.findViewById(R.id.list);
// Loading contacts in Background Thread
new LoadAllContacts().execute();
/*// on selecting single product
// launching Edit contact Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getActivity().getApplicationContext(),
EditProductActivity.class);
// sending pid to next activity
in.putExtra(TAG_PID, pid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});*/
return v;
}
/**
* Background Async Task to Load all contact by making HTTP Request
* */
class LoadAllContacts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading contacts. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All contacts from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", "get_contact_list"));
params.add(new BasicNameValuePair("user_id", String.valueOf(objTDB.getLong(AppConfig.USR_LOG_IN_ID))));
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(AppConfig.URL_CONTACTS, params);
// Check your log cat for JSON reponse
Log.d("All contacts: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// contacts found
// Getting Array of contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String type = c.getString(TAG_TYPE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_TYPE, type);
// adding HashList to ArrayList
contactsList.add(map);
}
} /*else {
// no contacts found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}*/
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all contacts
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), contactsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME,TAG_TYPE},
new int[] { R.id.pid, R.id.name ,R.id.type});
// updating listview
lv.setAdapter(adapter);
//setListAdapter(adapter);
}
});
}
}
}
And i am getting following error (from logcat) on line lv.setAdapter(adapter); ,
06-20 09:23:26.996: E/AndroidRuntime(1550): FATAL EXCEPTION: main 06-20 09:23:26.996: E/AndroidRuntime(1550): Process: com.example.simplifimed, PID: 1550 06-20 09:23:26.996: E/AndroidRuntime(1550): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 06-20 09:23:26.996: E/AndroidRuntime(1550): at com.example.simplifimed.Tab2$LoadAllContacts$1.run(Tab2.java:256) 06-20 09:23:26.996: E/AndroidRuntime(1550): at android.app.Activity.runOnUiThread(Activity.java:5293) 06-20 09:23:26.996: E/AndroidRuntime(1550): at com.example.simplifimed.Tab2$LoadAllContacts.onPostExecute(Tab2.java:245) 06-20 09:23:26.996: E/AndroidRuntime(1550): at com.example.simplifimed.Tab2$LoadAllContacts.onPostExecute(Tab2.java:1) 06-20 09:23:26.996: E/AndroidRuntime(1550): at android.os.AsyncTask.finish(AsyncTask.java:636) 06-20 09:23:26.996: E/AndroidRuntime(1550): at android.os.AsyncTask.access$500(AsyncTask.java:177) 06-20 09:23:26.996: E/AndroidRuntime(1550): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653) 06-20 09:23:26.996: E/AndroidRuntime(1550): at android.os.Handler.dispatchMessage(Handler.java:102) 06-20 09:23:26.996: E/AndroidRuntime(1550): at android.os.Looper.loop(Looper.java:135) 06-20 09:23:26.996: E/AndroidRuntime(1550): at android.app.ActivityThread.main(ActivityThread.java:5257) 06-20 09:23:26.996: E/AndroidRuntime(1550): at java.lang.reflect.Method.invoke(Native Method) 06-20 09:23:26.996: E/AndroidRuntime(1550): at java.lang.reflect.Method.invoke(Method.java:372) 06-20 09:23:26.996: E/AndroidRuntime(1550): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 06-20 09:23:26.996: E/AndroidRuntime(1550): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
The tab2.xml is,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
and list_item.xml is as follows,
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for individual news entries in a list -->
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="@+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17dip"
android:textStyle="bold" />
<!-- type Label -->
<TextView
android:id="@+id/type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17dip"
android:textStyle="bold" />
</TableRow>
</TableLayout>