我试图通过网站上的json加载来填充gridlayout,它必须在点击时显示图片,名称和链接。
当尝试使用asynctask填充时,我收到以下错误:
03-14 15:02:11.053: E/AndroidRuntime(10155): FATAL EXCEPTION: main
03-14 15:02:11.053: E/AndroidRuntime(10155): java.lang.NullPointerException
03-14 15:02:11.053: E/AndroidRuntime(10155): at com.videotrafico.MainActivity$DownloadCAMARAS.onPostExecute(MainActivity.java:515)
03-14 15:02:11.053: E/AndroidRuntime(10155): at com.videotrafico.MainActivity$DownloadCAMARAS.onPostExecute(MainActivity.java:1)
03-14 15:02:11.053: E/AndroidRuntime(10155): at android.os.AsyncTask.finish(AsyncTask.java:631)
03-14 15:02:11.053: E/AndroidRuntime(10155): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-14 15:02:11.053: E/AndroidRuntime(10155): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-14 15:02:11.053: E/AndroidRuntime(10155): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 15:02:11.053: E/AndroidRuntime(10155): at android.os.Looper.loop(Looper.java:137)
03-14 15:02:11.053: E/AndroidRuntime(10155): at android.app.ActivityThread.main(ActivityThread.java:5227)
03-14 15:02:11.053: E/AndroidRuntime(10155): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 15:02:11.053: E/AndroidRuntime(10155): at java.lang.reflect.Method.invoke(Method.java:511)
03-14 15:02:11.053: E/AndroidRuntime(10155): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-14 15:02:11.053: E/AndroidRuntime(10155): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-14 15:02:11.053: E/AndroidRuntime(10155): at dalvik.system.NativeStart.main(Native Method)
我看到了网页的输出,没问题。
这是我的代码:
public class DownloadCAMARAS extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog message
mProgressDialog.setMessage("Cargando cámaras, esto puede tomar hasta 20 segundos dependiendo de su conexión.");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://www.videotrafico.com/api/camaras.php");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("datos");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("imagen", jsonobject.getString("imagen"));
map.put("nombrecam", jsonobject.getString("nombrecam"));
map.put("enlace", jsonobject.getString("enlace"));
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
gridview = (GridView) findViewById(R.id.gridcams);
adapter2 = new ListViewAdapter2(MainActivity.this, arraylist);
// Set the adapter to the ListView
gridview.setAdapter(adapter2);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
现在它给了我这个问题:
我使用的是android支持库v7:
03-14 15:43:03.039: E/AndroidRuntime(11642): FATAL EXCEPTION: main
03-14 15:43:03.039: E/AndroidRuntime(11642): java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridView
03-14 15:43:03.039: E/AndroidRuntime(11642): at com.videotrafico.MainActivity$DownloadCAMARAS.onPostExecute(MainActivity.java:510)
03-14 15:43:03.039: E/AndroidRuntime(11642): at com.videotrafico.MainActivity$DownloadCAMARAS.onPostExecute(MainActivity.java:1)
03-14 15:43:03.039: E/AndroidRuntime(11642): at android.os.AsyncTask.finish(AsyncTask.java:631)
03-14 15:43:03.039: E/AndroidRuntime(11642): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-14 15:43:03.039: E/AndroidRuntime(11642): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-14 15:43:03.039: E/AndroidRuntime(11642): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 15:43:03.039: E/AndroidRuntime(11642): at android.os.Looper.loop(Looper.java:137)
03-14 15:43:03.039: E/AndroidRuntime(11642): at android.app.ActivityThread.main(ActivityThread.java:5227)
03-14 15:43:03.039: E/AndroidRuntime(11642): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 15:43:03.039: E/AndroidRuntime(11642): at java.lang.reflect.Method.invoke(Method.java:511)
03-14 15:43:03.039: E/AndroidRuntime(11642): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-14 15:43:03.039: E/AndroidRuntime(11642): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-14 15:43:03.039: E/AndroidRuntime(11642): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你的第二个问题是:
gridview = (GridView) findViewById(R.id.gridcams);
返回的视图findViewById(R.id.gridcams)
似乎是GridLayout
而不是GridView
。
==&GT; ClassCastException异常
解决方案:将ID为R.id.gridcams
的视图更改为布局中的GridView,以便findViewById返回GridView。