将图像从服务器提取到网格视图中,获取java.lang.NullPointerException

时间:2013-06-20 05:27:40

标签: android json android-gridview

我正在尝试将图像从服务器提取到网格视图,但每当我运行我的应用程序时,由于java.lang.NullPointerException而导致应用程序停止运行

错误位于: app.tabsample.GalleryActivity $ ImageAdapter.getCount(GalleryActivity.java:170)

错误行返回MyArr.size();

public int getCount() {
            // Error line
            return MyArr.size();
        }

完成Logcat:

06-20 05:15:23.298: D/dalvikvm(833): GC_CONCURRENT freed 302K, 7% free 5823K/6248K, paused 71ms+24ms, total 162ms
06-20 05:15:23.418: I/Choreographer(833): Skipped 78 frames!  The application may be doing too much work on its main thread.
06-20 05:15:23.678: I/Choreographer(833): Skipped 45 frames!  The application may be doing too much work on its main thread.
06-20 05:15:24.248: W/System.err(833): org.json.JSONException: Unterminated object at character 111 of [{"ImageID":"1","ImageName":"Item1","ImagePath_Thumbnail":"http://www.littleindia.ca/images/menu/fmenu01.jpg,"ImagePath_FullPhoto":"http://www.littleindia.ca/images/menu/fmenu01.jpg"},{"ImageID":"2","ImageName":"Item2","ImagePath_Thumbnail":"http://www.littleindia.ca/images/menu/fmenu02.jpg","ImagePath_FullPhoto":"http://www.littleindia.ca/images/menu/fmenu02.jpg"},{"ImageID":"3","ImageName":"Item3","ImagePath_Thumbnail":"http://www.littleindia.ca/images/menu/fmenu03.jpg","ImagePath_FullPhoto":"http://www.littleindia.ca/images/menu/fmenu03.jpg"},{"ImageID":"4","ImageName":"Item4","ImagePath_Thumbnail":"http://www.littleindia.ca/images/menu/fmenu04.jpg","ImagePath_FullPhoto":"http://www.littleindia.ca/images/menu/fmenu04.jpg"}]
06-20 05:15:24.248: W/System.err(833):  at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
06-20 05:15:24.329: W/System.err(833):  at org.json.JSONTokener.readObject(JSONTokener.java:394)
06-20 05:15:24.348: W/System.err(833):  at org.json.JSONTokener.nextValue(JSONTokener.java:100)
06-20 05:15:24.359: W/System.err(833):  at org.json.JSONTokener.readArray(JSONTokener.java:430)
06-20 05:15:24.410: W/System.err(833):  at org.json.JSONTokener.nextValue(JSONTokener.java:103)
06-20 05:15:24.410: W/System.err(833):  at org.json.JSONArray.<init>(JSONArray.java:87)
06-20 05:15:24.418: W/System.err(833):  at org.json.JSONArray.<init>(JSONArray.java:103)
06-20 05:15:24.418: W/System.err(833):  at app.tabsample.GalleryActivity$DownloadJSONFileAsync.doInBackground(GalleryActivity.java:230)
06-20 05:15:24.418: W/System.err(833):  at app.tabsample.GalleryActivity$DownloadJSONFileAsync.doInBackground(GalleryActivity.java:1)
06-20 05:15:24.418: W/System.err(833):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-20 05:15:24.468: W/System.err(833):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-20 05:15:24.479: W/System.err(833):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-20 05:15:24.479: W/System.err(833):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-20 05:15:24.479: W/System.err(833):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-20 05:15:24.489: W/System.err(833):  at java.lang.Thread.run(Thread.java:856)
06-20 05:15:24.498: D/AndroidRuntime(833): Shutting down VM
06-20 05:15:24.498: W/dalvikvm(833): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-20 05:15:24.530: E/AndroidRuntime(833): FATAL EXCEPTION: main
06-20 05:15:24.530: E/AndroidRuntime(833): java.lang.NullPointerException
06-20 05:15:24.530: E/AndroidRuntime(833):  at app.tabsample.GalleryActivity$ImageAdapter.getCount(GalleryActivity.java:170)
06-20 05:15:24.530: E/AndroidRuntime(833):  at android.widget.GridView.setAdapter(GridView.java:182)
06-20 05:15:24.530: E/AndroidRuntime(833):  at app.tabsample.GalleryActivity.ShowAllContent(GalleryActivity.java:143)
06-20 05:15:24.530: E/AndroidRuntime(833):  at app.tabsample.GalleryActivity$DownloadJSONFileAsync.onPostExecute(GalleryActivity.java:256)
06-20 05:15:24.530: E/AndroidRuntime(833):  at app.tabsample.GalleryActivity$DownloadJSONFileAsync.onPostExecute(GalleryActivity.java:1)
06-20 05:15:24.530: E/AndroidRuntime(833):  at android.os.AsyncTask.finish(AsyncTask.java:631)
06-20 05:15:24.530: E/AndroidRuntime(833):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-20 05:15:24.530: E/AndroidRuntime(833):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
06-20 05:15:24.530: E/AndroidRuntime(833):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 05:15:24.530: E/AndroidRuntime(833):  at android.os.Looper.loop(Looper.java:137)
06-20 05:15:24.530: E/AndroidRuntime(833):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-20 05:15:24.530: E/AndroidRuntime(833):  at java.lang.reflect.Method.invokeNative(Native Method)
06-20 05:15:24.530: E/AndroidRuntime(833):  at java.lang.reflect.Method.invoke(Method.java:511)
06-20 05:15:24.530: E/AndroidRuntime(833):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-20 05:15:24.530: E/AndroidRuntime(833):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-20 05:15:24.530: E/AndroidRuntime(833):  at dalvik.system.NativeStart.main(Native Method)
06-20 05:15:27.409: I/Process(833): Sending signal. PID: 833 SIG: 9

GalleryActivity.java:

        public class ImageAdapter extends BaseAdapter
        {
            private Context context;
            private ArrayList<HashMap<String, Object>> MyArr = new ArrayList<HashMap<String, Object>>();

            public ImageAdapter(Context c, ArrayList<HashMap<String, Object>> myArrList)
            {
                // TODO Auto-generated method stub
                context = c;
                MyArr = myArrList;
            }
            public int getCount() {
                // TODO Auto-generated method stub
                return MyArr.size();
            }

            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }   


            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.activity_gallery_column, null);
                }
                // ColPhoto
                ImageView imageView = (ImageView) convertView.findViewById(R.id.ColPhoto);
                imageView.getLayoutParams().height = 80;
                imageView.getLayoutParams().width = 80;
                imageView.setPadding(10, 10, 10, 10);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                try
                {
                    imageView.setImageBitmap((Bitmap)MyArr.get(position).get("ImageThumBitmap"));
                } catch (Exception e) {
                    // When Error
                    imageView.setImageResource(android.R.drawable.ic_menu_report_image);
                }
                // ColID
                TextView txtID = (TextView) convertView.findViewById(R.id.ColID);
                txtID.setPadding(5, 0, 0, 0);
                txtID.setText("ID : " + MyArr.get(position).get("ImageID").toString());

                // ColName
                TextView txtName = (TextView) convertView.findViewById(R.id.ColName);
                txtName.setPadding(5, 0, 0, 0);
                txtName.setText("Name : " + MyArr.get(position).get("ImageName").toString());  

                return convertView;
            }
        }

2 个答案:

答案 0 :(得分:1)

我看着你想要解析的json,"ImagePath_Thumbnail":"http://www.littleindia.ca/images/menu/fmenu01.jpg.上有一个缺少的“角色 确保你传递了一个合适的json。它应该是:"ImagePath_Thumbnail":"http://www.littleindia.ca/images/menu/fmenu01.jpg"。 它是一个json解析错误,它基本上会导致空指针异常

答案 1 :(得分:0)

从日志中可以清楚地看出,在调用MyArr时,ImageAdapter对象仍然为空,所以空指针异常......

onPostExecute() DownloadJSONFileAsync检查MyArrList != null ..

if(MyArrList != null)
ShowAllContent(); 

检查你的JsonObject以及它返回null的原因..