-1

我正在使用图像加载器从远程 url 的 JSON 中获取图像并填充到网格视图中

我有课::

  • 文件缓存.java
  • ImageLoader.java
  • MemoryCache.java
  • 实用程序.java

MainActivity.java

public class MainActivity extends Activity {
    // Declare Variables
    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;

    static String FLAG = "flag";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from listview_main.xml
        setContentView(R.layout.listview_main);


        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);

        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MainActivity.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Android JSON Parse Tutorial");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            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("-----------url-------");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("restaurants");

                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(MainActivity.FLAG, "http://54.218.73.244:7006/"+jsonobject.getString("restaurantIMAGE"));

                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }
}

listview_main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="90dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:stretchMode="columnWidth" >  

</GridView>

日志::

09-30 18:39:40.420: D/AndroidRuntime(550): Shutting down VM
09-30 18:39:40.420: W/dalvikvm(550): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-30 18:39:40.439: E/AndroidRuntime(550): FATAL EXCEPTION: main
09-30 18:39:40.439: E/AndroidRuntime(550): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbegin.jsonparsetutorial/com.androidbegin.jsonparsetutorial.MainActivity}: java.lang.ClassCastException: android.widget.GridView
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.os.Looper.loop(Looper.java:123)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-30 18:39:40.439: E/AndroidRuntime(550):  at java.lang.reflect.Method.invokeNative(Native Method)
09-30 18:39:40.439: E/AndroidRuntime(550):  at java.lang.reflect.Method.invoke(Method.java:507)
09-30 18:39:40.439: E/AndroidRuntime(550):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-30 18:39:40.439: E/AndroidRuntime(550):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-30 18:39:40.439: E/AndroidRuntime(550):  at dalvik.system.NativeStart.main(Native Method)
09-30 18:39:40.439: E/AndroidRuntime(550): Caused by: java.lang.ClassCastException: android.widget.GridView
09-30 18:39:40.439: E/AndroidRuntime(550):  at com.androidbegin.jsonparsetutorial.MainActivity.onCreate(MainActivity.java:34)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-30 18:39:40.439: E/AndroidRuntime(550):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-30 18:39:40.439: E/AndroidRuntime(550):  ... 11 more

如何解决错误!

4

2 回答 2

1

在 xml 文件中的 Main Activity.java 类中,您将其定义为活动中的网格,您以列表视图的形式检索视图,这就是问题所在

更改此行

 listview = (ListView) findViewById(R.id.listview);

 GridView listview;

 listview = (GridView) findViewById(R.id.listview);

然后它工作。

于 2013-09-30T13:27:33.580 回答
0

更改ListViewGridView,在提出问题之前检查您的代码并查看日志 cat 问题是什么

于 2014-04-09T12:11:47.773 回答