How do I get values from the Internet to a String[] array and pass them to a GridView in Android?
I get the value from the internet and put the data in the GRID_DATA array into the GridView, both methods work separately and both work fine. I’m combining the two, where the data from JSONObject adds the values to the JSON array so that GRID_DATA will get the string needed for the GRID View from it. But I found some errors in it.
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();
}
}
}
}
The log cat is:
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
Solution
It looks like GRID_DATA
is never given a value, so NullPointerException
appears when you try to access its member (gridValues.length
) QGFragment.java:74
You are in the GRID_DATA[i] = name;
is set in an array, but never actually uses something like GRID_DATA = new String[];
The contents of the >/p> are created
Array initialization
An array is simply a list of objects (in this case, a String
). Each object has an address that can be accessed using [location]
in the list. However, if you don’t create the list in the first place, accessing the [location]
of the non-existent list won’t work.