0

我试图从 json URL 获取图像以在动态网格中显示它们,

我的安卓版本是:4.2

输出:不幸的是,应用程序已停止。

public class FreeCategory extends Activity {
public String[] mThumbs=null;

private Context mContext;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_free_category);

    Intent intent=getIntent();
    Intent intent2=getIntent();

    final GridView gridview=(GridView) findViewById(R.id.grid_view);
    gridview.setAdapter(new ImageAdapter(this));
} 
 public class ImageAdapter extends BaseAdapter {

 protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
               InputStream in = entity.getContent();
                 StringBuffer out = new StringBuffer();
                 int n = 1;
                 while (n>0) {
                     byte[] b = new byte[4096];
                     n =  in.read(b);
                     if (n>0) out.append(new String(b, 0, n));
                 }
                 return out.toString();
            }

            protected  void doInBackground(Void... params) {
             HttpClient httpClient = new DefaultHttpClient();
             HttpContext localContext = new BasicHttpContext();
         HttpGet httpGet = new HttpGet( My URL);
         String text = null;
         String result = null;

         String name=null;
         String id=null;

         try {
               HttpResponse response = httpClient.execute(httpGet, localContext);
               HttpEntity entity = response.getEntity();
               text = getASCIIContentFromEntity(entity);
                   response.getEntity().getContent() );


                   text=text.replaceAll("&lt;", "<").replace("&gt;", ">");
                   int start=text.indexOf("<message>");
                   start=start+9;
                   int end=text.indexOf("</message>");
                   text=text.substring(start, end);



                   JSONArray ja = new JSONArray(text) ;

                                int n = ja.length();
                                for (int i = 0; i < n; i++) {

                                 JSONObject jo = ja.getJSONObject(i);

                                 mThumbs[i]=jo.getString("SubCatImage");
                                 name= jo.getString("SubcatName");
                                         //.split(",").toString();
                                  id= jo.getString("Subcatid");



                                }
         } catch (Exception e) {
                      e.getLocalizedMessage();
                   }



            }   

            public ImageAdapter(Context c){
                mContext = c;
            }



            public int getCount() {
                // TODO Auto-generated method stub
                return mThumbs.length;
            }

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

            public long getItemId(int position) {

                return 0;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                 ImageView imageView;
                 if (convertView == null) { 
                     imageView = new ImageView(mContext);
                     imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                     imageView.setPadding(8, 8, 8, 8);
                 } else {
                      imageView = (ImageView) convertView;
                 }

                 imageView.setImageURI(Uri.parse(mThumbs[position]));

                 return imageView;

            }

        }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_free_category, menu);
    return true;
}}

日志文件:

    02-13 18:38:51.359: D/dalvikvm(704): Not late-enabling CheckJNI (already on)
     02-13 18:38:53.399: D/dalvikvm(704): GC_FOR_ALLOC freed 47K, 3% free 9961K/10179K, paused 72ms
     02-13 18:38:53.429: I/dalvikvm-heap(704): Grow heap (frag case) to 12.753MB for 3085244-byte allocation
     02-13 18:38:53.529: D/dalvikvm(704): GC_CONCURRENT freed 1K, 3% free 12973K/13255K, paused 6ms+16ms
     02-13 18:38:53.980: D/dalvikvm(704): GC_FOR_ALLOC freed <1K, 3% free 12973K/13255K, paused 48ms
     02-13 18:38:54.059: I/dalvikvm-heap(704): Grow heap (frag case) to 19.377MB for 6947096-byte allocation
     02-13 18:38:54.189: D/dalvikvm(704): GC_CONCURRENT freed 0K, 2% free 19758K/20103K, paused 10ms+6ms
     02-13 18:38:55.059: D/gralloc_goldfish(704): Emulator without GPU emulation detected.
     02-13 18:38:55.190: W/TextLayoutCache(704): computeValuesWithHarfbuzz -- need to force to single run
     02-13 18:41:11.760: D/dalvikvm(704): GC_FOR_ALLOC freed 3353K, 17% free 17299K/20743K, paused 51ms
     02-13 18:41:11.799: I/dalvikvm-heap(704): Grow heap (frag case) to 19.922MB for 3088320-byte allocation
     02-13 18:41:11.981: D/dalvikvm(704): GC_FOR_ALLOC freed 6K, 15% free 20309K/23815K, paused 79ms
     02-13 18:41:12.099: D/dalvikvm(704): GC_CONCURRENT freed 1K, 15% free 20308K/23815K, paused 10ms+6ms
     02-13 18:41:12.449: D/dalvikvm(704): GC_FOR_ALLOC freed 25K, 15% free 20282K/23815K, paused 55ms
     02-13 18:41:12.519: I/dalvikvm-heap(704): Grow heap (frag case) to 26.520MB for 6951712-byte allocation
     02-13 18:41:12.649: D/dalvikvm(704): GC_CONCURRENT freed 0K, 12% free 27071K/30663K, paused 6ms+9ms
     02-13 18:41:13.839: W/TextLayoutCache(704): computeValuesWithHarfbuzz -- need to force to single run
     02-13 18:41:50.869: D/AndroidRuntime(704): Shutting down VM
     02-13 18:41:50.892: W/dalvikvm(704): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
     02-13 18:41:50.919: E/AndroidRuntime(704): FATAL EXCEPTION: main
     02-13 18:41:50.919: E/AndroidRuntime(704): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.erpsecurity/com.example.erpsecurity.FreeCategory}: java.lang.NullPointerException
     02-13 18:41:50.919: E/AndroidRuntime(704):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at android.os.Handler.dispatchMessage(Handler.java:99)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at android.os.Looper.loop(Looper.java:137)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at android.app.ActivityThread.main(ActivityThread.java:4340)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at java.lang.reflect.Method.invokeNative(Native Method)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at java.lang.reflect.Method.invoke(Method.java:511)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at dalvik.system.NativeStart.main(Native Method)
     02-13 18:41:50.919: E/AndroidRuntime(704): Caused by: java.lang.NullPointerException
     02-13 18:41:50.919: E/AndroidRuntime(704):     at com.example.erpsecurity.FreeCategory$ImageAdapter.getCount(FreeCategory.java:242)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at android.widget.GridView.setAdapter(GridView.java:180)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at com.example.erpsecurity.FreeCategory.onCreate(FreeCategory.java:56)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at android.app.Activity.performCreate(Activity.java:4465)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
     02-13 18:41:50.919: E/AndroidRuntime(704):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
     02-13 18:41:50.919: E/AndroidRuntime(704):     ... 11 more
     02-13 18:41:54.840: I/Process(704): Sending signal. PID: 704 SIG: 9
     02-13 18:50:26.289: W/ActivityThread(750): Application com.example.erpsecurity is waiting for the debugger on port 8100...
     02-13 18:50:26.389: I/System.out(750): Sending WAIT chunk
     02-13 18:50:26.541: I/dalvikvm(750): Debugger is active
     02-13 18:50:26.629: I/System.out(750): Debugger has connected
     02-13 18:50:26.629: I/System.out(750): waiting for debugger to settle...
     02-13 18:50:26.829: I/System.out(750): waiting for debugger to settle...
     02-13 18:50:27.029: I/System.out(750): waiting for debugger to settle...
     02-13 18:50:27.229: I/System.out(750): waiting for debugger to settle...
     02-13 18:50:27.439: I/System.out(750): waiting for debugger to settle...
     02-13 18:50:27.639: I/System.out(750): waiting for debugger to settle...
     02-13 18:50:27.840: I/System.out(750): waiting for debugger to settle...
     02-13 18:50:28.099: I/System.out(750): waiting for debugger to settle...
     02-13 18:50:28.299: I/System.out(750): waiting for debugger to settle...
     02-13 18:50:28.546: I/System.out(750): waiting for debugger to settle...
     02-13 18:50:28.794: I/System.out(750): waiting for debugger to settle...
     02-13 18:50:29.001: I/System.out(750): debugger has settled (1372)
     02-13 18:50:30.271: D/dalvikvm(750): GC_FOR_ALLOC freed 52K, 3% free 9962K/10179K, paused 57ms
     02-13 18:50:30.332: I/dalvikvm-heap(750): Grow heap (frag case) to 12.753MB for 3085244-byte allocation
     02-13 18:50:30.519: D/dalvikvm(750): GC_CONCURRENT freed 1K, 3% free 12973K/13255K, paused 23ms+4ms
     02-13 18:50:30.899: D/dalvikvm(750): GC_FOR_ALLOC freed <1K, 3% free 12974K/13255K, paused 32ms
     02-13 18:50:30.969: I/dalvikvm-heap(750): Grow heap (frag case) to 19.378MB for 6947096-byte allocation
     02-13 18:50:31.089: D/dalvikvm(750): GC_CONCURRENT freed 0K, 2% free 19758K/20103K, paused 4ms+9ms
     02-13 18:50:32.599: D/gralloc_goldfish(750): Emulator without GPU emulation detected.
     02-13 18:50:32.749: W/TextLayoutCache(750): computeValuesWithHarfbuzz -- need to force to single run
     02-13 18:52:00.749: D/dalvikvm(750): GC_FOR_ALLOC freed 3379K, 17% free 17301K/20807K, paused 53ms
     02-13 18:52:00.769: I/dalvikvm-heap(750): Grow heap (frag case) to 19.924MB for 3088320-byte allocation
     02-13 18:52:00.919: D/dalvikvm(750): GC_FOR_ALLOC freed 6K, 15% free 20311K/23879K, paused 53ms
     02-13 18:52:01.279: D/dalvikvm(750): GC_CONCURRENT freed 1K, 15% free 20312K/23879K, paused 5ms+7ms
     02-13 18:52:01.469: D/dalvikvm(750): GC_FOR_ALLOC freed 26K, 16% free 20286K/23879K, paused 64ms
     02-13 18:52:01.529: I/dalvikvm-heap(750): Grow heap (frag case) to 26.524MB for 6951712-byte allocation
     02-13 18:52:01.649: D/dalvikvm(750): GC_FOR_ALLOC freed 0K, 12% free 27075K/30727K, paused 56ms
     02-13 18:52:02.139: D/dalvikvm(750): GC_CONCURRENT freed 1K, 12% free 27078K/30727K, paused 5ms+11ms
     02-13 18:52:04.359: W/TextLayoutCache(750): computeValuesWithHarfbuzz -- need to force to single run




**above are my code and log file** 

Any help would be great full
Thanks in Advance


nw my error log file:
here is my error.log file:02-14 16:48:19.546: E/AndroidRuntime(893): FATAL EXCEPTION: main
02-14 16:48:19.546: E/AndroidRuntime(893): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.erpsecurity/com.example.erpsecurity.FreeCategory}: java.lang.NullPointerException
02-14 16:48:19.546: E/AndroidRuntime(893):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-14 16:48:19.546: E/AndroidRuntime(893):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-14 16:48:19.546: E/AndroidRuntime(893):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-14 16:48:19.546: E/AndroidRuntime(893):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-14 16:48:19.546: E/AndroidRuntime(893):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 16:48:19.546: E/AndroidRuntime(893):  at android.os.Looper.loop(Looper.java:137)
02-14 16:48:19.546: E/AndroidRuntime(893):  at android.app.ActivityThread.main(ActivityThread.java:4340)
02-14 16:48:19.546: E/AndroidRuntime(893):  at java.lang.reflect.Method.invokeNative(Native Method)
02-14 16:48:19.546: E/AndroidRuntime(893):  at java.lang.reflect.Method.invoke(Method.java:511)
02-14 16:48:19.546: E/AndroidRuntime(893):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-14 16:48:19.546: E/AndroidRuntime(893):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-14 16:48:19.546: E/AndroidRuntime(893):  at dalvik.system.NativeStart.main(Native Method)
02-14 16:48:19.546: E/AndroidRuntime(893): Caused by: java.lang.NullPointerException
02-14 16:48:19.546: E/AndroidRuntime(893):  at com.example.erpsecurity.FreeCategory$ImageAdapter.getCount(FreeCategory.java:134)
02-14 16:48:19.546: E/AndroidRuntime(893):  at android.widget.GridView.setAdapter(GridView.java:180)
02-14 16:48:19.546: E/AndroidRuntime(893):  at com.example.erpsecurity.FreeCategory.onCreate(FreeCategory.java:56)
02-14 16:48:19.546: E/AndroidRuntime(893):  at android.app.Activity.performCreate(Activity.java:4465)
02-14 16:48:19.546: E/AndroidRuntime(893):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-14 16:48:19.546: E/AndroidRuntime(893):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
4

2 回答 2

0

mThumbs 在 getCount() 上为空,你应该重新检查 json 解析方法

于 2013-02-14T06:02:54.963 回答
0

检查来自 URL 的响应,可能是您没有获得关键字“ SubCatImage ”的任何值,这就是为什么您的数组没有被初始化,因此您得到空指针异常。在 getCount() 方法中,执行以下操作:-

    public int getCount() 
    {
        // TODO Auto-generated method stub
        if (mThumbs.length>0) 
        {
            return mThumbs.length;
        }
        else {
            return 0;
        }
    }
于 2013-02-14T07:10:00.937 回答