我有以下片段的Android setadapter空指針
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);
}
});
}
}
}
,我讓下面的在線lv.setAdapter(適配器)錯誤(來自logcat的); ,
9月6日至20日:23:26.996:E/AndroidRuntime(1550):致命異常:主 9月6日至20日:23:26.996:E/AndroidRuntime(1550):進程:com。示例。 PID:1550 06-20 09:23:26.996:E/AndroidRuntime(1550):java.lang.NullPointerException:試圖調用虛擬方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'在null對象引用 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 $ LoadA llContacts.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):在android.os.AsyncTask.finish(AsyncTask.java:636) 06-20 09:23:26.996:E/AndroidRuntime(1550):在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):在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) 9月6日至20日:23:26.996:E/AndroidRuntime(1550):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
的tab2.xml是,
<?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>
和list_item.xml如下,
<?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>
嘗試調試和驗證你在LV =(ListView控件)v.findViewById(R.id.list)獲得的對象;這條線? –
嗨rajan,我已經嘗試添加Log.d(「列表視圖:」,lv.toString());在上面的行之後,但它不會在log cat中顯示任何地方。我正在使用滑動標籤,這是第二個標籤片段代碼,所以這個標籤沒有加載第一..可能是因爲它沒有顯示..我可以檢查嗎? –
如果你沒有得到ListView對象的任何對象引用,那麼你會得到這個錯誤。爲此首先你需要正確加載你的視圖片段 –