1

I am trying to populate list in listfragment from mysql database. But i am getting Null Pointer Exception. I've done the same thing with activity and it is working fine. Please suggest.

Here is my 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);


                }

            });

        }

    }
}

Here is 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>

And here is 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>  

Here is the 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)
1
  • Post your stack trace. Commented Jun 11, 2013 at 11:24

3 Answers 3

2

replace

 v = (ListView)getView().findViewById(R.id.list);

with

 v = (ListView) view.findViewById(R.id.list);

assuming that your ListView is contained in R.layout.patient_list

Sign up to request clarification or add additional context in comments.

7 Comments

I did , Yes the list view is in patient_list layout. Now i am getting error as: 06-11 17:01:51.811: E/AndroidRuntime(24774): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
Check if your layout patient_list contains a ListView of id 'list'.
you are using ListActivity? or (i think) ListFragment? if so, they expect you to use that ID for the listview. You should be able to google that ("android.R.id.list") and see why. i've used it...it works.
See the ListFragment documentation : ... your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)
Thnx a lot .....But now i have one more issue. When i am clicking on my list items only the 1st item's id is going to the next activity. As i am getting patient name and id in this list fragment and passing the patient id to another activity , only the 1st list item's id is passing through even if i tap on another item...Hope u got my point ...
|
0

Replace

 v = (ListView) getView().findViewById(R.id.list);

by

 v = (ListView) view.findViewById(R.id.list);

Comments

0

You have 2 options here

Use (If you dont have layout file)

v = getListView(); 

OR

v = (ListView) view.findViewById(android.R.id.list);

in manifest change android:id="@+id/list" to android:id="@android:id/list"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.