作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我从 Internet 获取值并将 GRID_DATA 数组中的数据放入 GridView ,这两种方法都可以单独工作,而且都工作正常。我将两者组合在一起,其中来自 JSONObject 的数据会将值添加到 JSON 数组,以便 GRID_DATA 将从中获取 GRID View 所需的字符串。但是我在其中发现了一些错误。
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();
}
}
}
}
日志猫是:
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
最佳答案
看起来 GRID_DATA
从未被赋予值,因此当您尝试访问它的成员 (gridValues.length
) 时出现 NullPointerException
QGFragment.java:74
.
您在 GRID_DATA[i] = name;
的数组中设置了一个项目,但实际上从未使用类似 GRID_DATA = new String[];
的内容创建数组>/p>
数组初始化
数组只是对象的列表(在本例中为 String
)。每个对象都有一个地址,可以使用该列表中的 [location]
访问该地址。但是,如果您一开始就没有创建该列表,那么访问该不存在的列表的 [location]
将无法正常工作。
关于java - 如何从 Internet 获取值到 String[] 数组并将其传递到 Android 中的 GridView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20763250/
我是一名优秀的程序员,十分优秀!