我正在尝试从mysql数据库填充listfragment中的列表。 但我得到Null Pointer Exception。我已经用活动完成了同样的事情并且工作正常。请建议。
这是我的ListFragment
public class AllPatient extends ListFragment {
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList < HashMap < String, String >> productsList = new ArrayList < HashMap < String, String >> ();
// url to get all products list
private static String url_all_patients = "http://192.168.44.208/get_all_patients.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PATIENT_ID = "patient_id";
private static final String TAG_PATIENT_NAME = "patient_name";
JSONArray products = null;
Context ctx;
String pid;
EditText inputSearch = null;
ListAdapter adapter;
@
Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.patient_list, container, false);
new LoadAllPatients().execute();
//ListView lv =view.getListView();
ListView v;
/**/
Getting Null Point Exception here * *
v = (ListView) getView().findViewById(R.id.list);
v.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();
System.out.println("doctor page" + pid);
// Starting new intent
Bundle bundle = new Bundle();
bundle.putString("TAG_PATIENT_ID", pid);
System.out.println("all patient" + pid);
Intent i = new Intent(getActivity(), DocPresc.class);
i.putExtras(bundle);
startActivity(i);
// starting new activity and expecting some response back
// startActivityForResult(in, 100);
}
});
return view;
}
class LoadAllPatients extends AsyncTask < String, String, String > {
protected String doInBackground(String...args) {
// Building Parameters
List < NameValuePair > params = new ArrayList < NameValuePair > ();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_patients, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Patients: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PATIENT_ID).toUpperCase();
String name = c.getString(TAG_PATIENT_NAME).toUpperCase();
// creating new HashMap
HashMap < String, String > map = new HashMap < String, String > ();
// adding each child node to HashMap key => value
map.put(TAG_PATIENT_ID, id);
map.put(TAG_PATIENT_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
}
} 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 products
// pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
getActivity(), productsList,
R.layout.list_item1, new String[] {
TAG_PATIENT_ID,
TAG_PATIENT_NAME
},
new int[] {
R.id.pid, R.id.name
});
// updating listview
setListAdapter(adapter);
}
});
}
}
}
这是Patient_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:padding="10dip"
>
<!-- Email Label -->
<!-- Login Form Ends -->
<EditText android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/gradient_text"
android:drawableLeft="@drawable/search"
android:hint="Search patients.."
android:inputType="textVisiblePassword"
android:layout_margin="6dp"/>
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="#0000"
android:dividerHeight="6dp"
android:paddingBottom="3dp"
android:padding="6dp"
android:background="#0000"
android:cacheColorHint="#0000"
>
</ListView>
</LinearLayout>
这是List_item1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left side Thumbnail image -->
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="@+id/list_image"
android:contentDescription="@string/app_name"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/sunny" />
</LinearLayout>
<!-- Rightend Arrow -->
<ImageView
android:id="@+id/imageView1"
android:contentDescription="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/arrow" />
<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/list_image"
android:layout_marginLeft="75dip"
android:layout_centerVertical="true"
android:paddingBottom ="10dip"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
<!-- Weather Information-->
<TextView
android:id="@+id/pid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvCity"
android:layout_alignLeft="@+id/tvCity"
android:paddingTop="5dip"
android:layout_centerHorizontal="true"
android:textColor="#343434"
android:textSize="15dip"
android:visibility="gone"/>
</RelativeLayout>
这是stacktrace
06-11 16:55:33.264: E/AndroidRuntime(23430): FATAL EXCEPTION: main
06-11 16:55:33.264: E/AndroidRuntime(23430): java.lang.NullPointerException
06-11 16:55:33.264: E/AndroidRuntime(23430): at com.example.actionbar.AllPatient.onCreateView(AllPatient.java:79)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.View.measure(View.java:15479)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.View.measure(View.java:15479)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4828)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.View.measure(View.java:15479)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.View.measure(View.java:15479)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4828)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-11 16:55:33.264: E/AndroidRuntime(23430): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2359)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.View.measure(View.java:15479)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1968)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1214)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1387)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4464)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.Choreographer.doFrame(Choreographer.java:525)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.os.Handler.handleCallback(Handler.java:615)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.os.Looper.loop(Looper.java:137)
06-11 16:55:33.264: E/AndroidRuntime(23430): at android.app.ActivityThread.main(ActivityThread.java:4895)
06-11 16:55:33.264: E/AndroidRuntime(23430): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 16:55:33.264: E/AndroidRuntime(23430): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 16:55:33.264: E/AndroidRuntime(23430): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
06-11 16:55:33.264: E/AndroidRuntime(23430): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
06-11 16:55:33.264: E/AndroidRuntime(23430): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
替换
v = (ListView)getView().findViewById(R.id.list);
与
v = (ListView) view.findViewById(R.id.list);
假设您的ListView包含在R.layout.patient_list
中答案 1 :(得分:0)
替换
v = (ListView) getView().findViewById(R.id.list);
通过
v = (ListView) view.findViewById(R.id.list);
答案 2 :(得分:0)
这里有2个选项
使用(如果你没有布局文件)
v = getListView();
OR
v = (ListView) view.findViewById(android.R.id.list);
在清单更改android:id="@+id/list"
到android:id="@android:id/list"