How to get values ​​from the Internet and the String [] array and pass it as a grid in Android?

I got the values ​​from the Internet and put the data from the GRID_DATA array into the grid, working separately, and both worked fine. I combined them together when the data from JSONObject adds the values ​​to the JSON array, so from GRID_DATA we get the rows needed to represent GRID. But I get some errors.

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;


public class QGFragment extends Fragment {

    GridView grdView;
     // This Data show in grid ( Used by adapter )
    String strurl;
    //URL to get JSON Array
    private static String url = "http://localhost/app/data.php";
    //JSON Node Names
    private static final String TAG_QP = "qp";

    private static final String TAG_NAME = "name";
    //private static final String TAG_VAL = "val";
    //private static final String TAG_NPICS = "nPics";
    JSONArray qp = null;

    static String[] GRID_DATA;

    public QGFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_qg, container, false);

        grdView = (GridView)rootView.findViewById(R.id.gridview);

        new JSONParse().execute();

        // Instance of ImageAdapter Class

        grdView.setAdapter(  new GridAdapter( getActivity(), GRID_DATA ) );




        return rootView;
    }
    class GridAdapter extends BaseAdapter {

        private Context context;
        private final String[] gridValues;

        //Constructor to initialise values
        public GridAdapter(Context context, String[ ] gridValues) {

            this.context        = context;
            this.gridValues     = gridValues;
        }

        @Override
        public int getCount() {

            // Number of times getView method call depends upon gridValues.length
            return gridValues.length;
        }

        @Override
        public Object getItem(int position) {

            return null;
        }

        @Override
        public long getItemId(int position) {

            return 0;
        }


        // Number of times getView method call depends upon gridValues.length

        public View getView(int position, View convertView, ViewGroup parent) {

            // LayoutInflator to call external grid_item.xml file

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View gridView;

            if (convertView == null) {

                gridView = new View(context);

                // get layout from grid_item.xml ( Defined Below )

                gridView = inflater.inflate( R.layout.grid_list , null);

                // set value into textview

                TextView textView = (TextView) gridView
                        .findViewById(R.id.icon_text);

                textView.setText(gridValues[position]);

                // set image based on selected text

                ImageView imageView = (ImageView) gridView
                        .findViewById(R.id.icon_image);


                    imageView.setImageResource(R.drawable.ic_launcher);

            } else {

               gridView = (View) convertView;
            }

            return gridView;
        }
    }
    private class JSONParse extends AsyncTask<String, String, JSONObject> {
         private ProgressDialog pDialog;
        @Override
           protected void onPreExecute() {
               super.onPreExecute();


               pDialog = new ProgressDialog(getActivity());
               pDialog.setMessage("Getting Data ...");
               pDialog.setIndeterminate(false);
               pDialog.setCancelable(true);
               pDialog.show();
        }
        @Override
           protected JSONObject doInBackground(String... args) {
            JSONParser jParser = new JSONParser();
            // Getting JSON from URL
            JSONObject json = jParser.getJSONFromUrl(url);
            return json;
        }
         @Override
            protected void onPostExecute(JSONObject json) {
             pDialog.dismiss();
             try {


                    // Getting JSON Array
                    qp = json.getJSONArray(TAG_QP);

                    for(int i = 0 ; i < qp.length(); i++){
                        JSONObject c = qp.getJSONObject(i);

                        // Storing  JSON item in a Variable


                         String name = c.getString(TAG_NAME);
                         //String val = c.getString(TAG_VAL);
                         //String npics = c.getString(TAG_NPICS);

                         GRID_DATA[i] = name;


                        //Set JSON Data in TextView

                    };


            } catch (JSONException e) {
                e.printStackTrace();
            }

         }

    } 


}

Log Code:

12-24 20:53:06.702: W/dalvikvm(4304): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-24 20:53:06.743: E/AndroidRuntime(4304): FATAL EXCEPTION: main
12-24 20:53:06.743: E/AndroidRuntime(4304): java.lang.NullPointerException
12-24 20:53:06.743: E/AndroidRuntime(4304):     at com.qrodsintegrated.QGFragment$GridAdapter.getCount(QGFragment.java:74)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at android.widget.GridView.setAdapter(GridView.java:180)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at com.qrodsintegrated.QGFragment.onCreateView(QGFragment.java:51)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at android.app.BackStackRecord.run(BackStackRecord.java:622)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:417)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at android.os.Handler.handleCallback(Handler.java:605)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at android.os.Looper.loop(Looper.java:137)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at android.app.ActivityThread.main(ActivityThread.java:4340)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at java.lang.reflect.Method.invokeNative(Native Method)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at java.lang.reflect.Method.invoke(Method.java:511)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-24 20:53:06.743: E/AndroidRuntime(4304):     at dalvik.system.NativeStart.main(Native Method)
12-24 20:53:08.722: I/Process(4304): Sending signal. PID: 4304 SIG: 9
+4
source share
1 answer

It seems like it GRID_DATAnever gets the value, so NullPointerExceptionwhen you try to get element ( gridValues.length) to it QGFragment.java:74.

GRID_DATA[i] = name;, - GRID_DATA = new String[];

- ( String s). , [location] . , [location] , , .

+3

All Articles