我从互联网上获取值并将GRID_DATA数组中的数据放到网格视图中,这些数据都是单独工作的,并且都运行良好。我将两者结合在一起,其中来自JSONObject的数据将值添加到JSON数组,因此GRID_DATA将获得GRID视图所需的字符串。但我在其中遇到了一些错误。
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 Cat是:
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
答案 0 :(得分:3)
看起来GRID_DATA
永远不会给出值,因此当您尝试在NullPointerException
访问其成员(gridValues.length
)时会QGFragment.java:74
。
您正在GRID_DATA[i] = name;
的数组中设置项目,但实际上从未使用GRID_DATA = new String[];
阵列初始化
数组只是一个对象列表(在本例中为String
s)。每个对象都有一个可以在该列表中使用[location]
访问的地址。但是,如果您从未首先创建该列表,那么访问该列表中不存在的[location]
将不起作用。