且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

杰克逊使用Android的解析器崩溃的VerifyError

更新时间:2023-02-16 17:53:44

问题的关键就在这里。

  12月5日至30日:12:17.240:W / dalvikvm(838):VFY:无法解决的异常类748(LCOM / fasterxml /杰克逊/核心/ JsonParseException;)
12月5日至30日:12:17.240:W / dalvikvm(838):VFY:无法找到地址0x2a异常处理程序
12月5日至30日:12:17.240:W / dalvikvm(838):VFY:拒绝LCOM /例子/ bitmapdisplay / MainActivity $ URLArray; .doInBackground([Ljava /朗/无效;)Ljava /朗/无效;
12月5日至30日:12:17.240:W / dalvikvm(838):VFY:在拒绝运code符进行0x002a
12月5日至30日:12:17.250:W / dalvikvm(838):VFY:拒绝LCOM /例子/ bitmapdisplay / MainActivity $ URLArray; .doInBackground([Ljava /朗/无效;)Ljava /朗/无效;
12月5日至30日:12:17.250:W / dalvikvm(838):验证拒绝类LCOM /例子/ bitmapdisplay / MainActivity $ URLArray;

,这一切从那里下坡。

看起来 (距杰克逊可能是其他类)不会被纳入您的APK JsonParseException。如果他们没有被Eclipse的出口可能发生这种情况。

而不是将其添加为一个外部库,只需将杰克逊核心xxxjar 文件夹你的Andr​​oid项目的,它应该足以确保这一点。

Here is my log cat. Unfortunately I am not a pro in reading errors from logcat so please help me :)

05-30 12:12:16.510: I/System.out(838):  We made it this far 1
05-30 12:12:17.230: I/System.out(838):  We made it this far 2
05-30 12:12:17.240: W/dalvikvm(838): VFY: unable to resolve exception class 748 (Lcom/fasterxml/jackson/core/JsonParseException;)
05-30 12:12:17.240: W/dalvikvm(838): VFY: unable to find exception handler at addr 0x2a
05-30 12:12:17.240: W/dalvikvm(838): VFY:  rejected Lcom/example/bitmapdisplay/MainActivity$URLArray;.doInBackground ([Ljava/lang/Void;)Ljava/lang/Void;
05-30 12:12:17.240: W/dalvikvm(838): VFY:  rejecting opcode 0x0d at 0x002a
05-30 12:12:17.250: W/dalvikvm(838): VFY:  rejected Lcom/example/bitmapdisplay/MainActivity$URLArray;.doInBackground ([Ljava/lang/Void;)Ljava/lang/Void;
05-30 12:12:17.250: W/dalvikvm(838): Verifier rejected class Lcom/example/bitmapdisplay/MainActivity$URLArray;
05-30 12:12:17.250: D/AndroidRuntime(838): Shutting down VM
05-30 12:12:17.250: W/dalvikvm(838): threadid=1: thread exiting with uncaught exception (group=0xb1adaba8)
05-30 12:12:17.270: E/AndroidRuntime(838): FATAL EXCEPTION: main
05-30 12:12:17.270: E/AndroidRuntime(838): Process: com.example.bitmapdisplay, PID: 838
05-30 12:12:17.270: E/AndroidRuntime(838): java.lang.VerifyError: com/example/bitmapdisplay/MainActivity$URLArray
05-30 12:12:17.270: E/AndroidRuntime(838):  at com.example.bitmapdisplay.MainActivity.onCreate(MainActivity.java:72)
05-30 12:12:17.270: E/AndroidRuntime(838):  at android.app.Activity.performCreate(Activity.java:5231)
05-30 12:12:17.270: E/AndroidRuntime(838):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-30 12:12:17.270: E/AndroidRuntime(838):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-30 12:12:17.270: E/AndroidRuntime(838):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-30 12:12:17.270: E/AndroidRuntime(838):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-30 12:12:17.270: E/AndroidRuntime(838):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-30 12:12:17.270: E/AndroidRuntime(838):  at android.os.Handler.dispatchMessage(Handler.java:102)
05-30 12:12:17.270: E/AndroidRuntime(838):  at android.os.Looper.loop(Looper.java:136)
05-30 12:12:17.270: E/AndroidRuntime(838):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-30 12:12:17.270: E/AndroidRuntime(838):  at java.lang.reflect.Method.invokeNative(Native Method)
05-30 12:12:17.270: E/AndroidRuntime(838):  at java.lang.reflect.Method.invoke(Method.java:515)
05-30 12:12:17.270: E/AndroidRuntime(838):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-30 12:12:17.270: E/AndroidRuntime(838):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-30 12:12:17.270: E/AndroidRuntime(838):  at dalvik.system.NativeStart.main(Native Method)
05-30 12:12:30.390: I/Process(838): Sending signal. PID: 838 SIG: 9

Here is the logcat code and here is my Java code. It seems like the problems are coming from the thread URLArray because, as seen in the logcat, the system doesn't even print the third "we made it this far":

package com.example.bitmapdisplay;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.fasterxml.jackson.core.JsonParseException;

import android.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;


public class MainActivity extends Activity {

    /**
     * Bitmap items na dimage displaying
     */
    Bitmap image;
    BitmapDrawable bd;
    ImageView temp;
    ProgressDialog pd;

    /**
     * JSON URL
     */
    URL url;

    /**
     * Data from JSON file
     */
    ArrayList<String> urls;
    ArrayList<ImageView> images;

    JsonParsing obj;

    String json_link_str = "http://api.tumblr.com/v2/blog/humansofnewyork.com/posts?api_key=7ag2CJXOuxuW3vlVS5wQG6pYA6a2ZQcSCjzZsAp2pDbVwf3xEk&notes_info=true&filter=text";
    int counter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        System.out.println(" We made it this far 1");
        super.onCreate(savedInstanceState);
        setContentView(com.example.bitmapdisplay.R.layout.image_container);

        counter = 0;
        images = new ArrayList<ImageView>();
        urls = new ArrayList<String>();

        System.out.println(" We made it this far 2");

        new URLArray().execute();


    }

    /**
     * Fills the ListView
     */
    private void fillGridView() {

        ArrayAdapter<ImageView> adapter = new ArrayAdapter<ImageView>(this, com.example.bitmapdisplay.R.layout.image_container,images);

        GridView grid = (GridView) findViewById(com.example.bitmapdisplay.R.id.gvImages);

        grid.setAdapter(adapter);

    }

    public class URLArray extends AsyncTask<Void, Void, Void                                                                                                                                         > {

        public URLArray() {

        }

        @Override
        protected Void doInBackground(Void...params ) {
            try {
                System.out.println(" We made it this far 3");

                URL json_link = new URL(json_link_str);

                JsonParsing parse_images = new JsonParsing(json_link);

                    try {
                        parse_images.parseFile(3, urls);
                        System.out.println(" We made it this far 4");

                    } catch (JsonParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    obj = parse_images;
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void arg) {
            super.onPreExecute();
            System.out.println(" We made it this far 5");

            urls = obj.getURLs();
            new TheTask().execute();
        }

    }


class TheTask extends AsyncTask<Void,Void,Void>
{


    @Override
    protected Void doInBackground(Void... params) {

        System.out.println(" We made it this far 6");

        pd = new ProgressDialog(MainActivity .this);
        pd.show();


        System.out.println(" We made it this far 7");

        try
        {
            int counter = 0;
        for (ImageView temp : images) {

            image = downloadBitmap(urls.get(counter));
            temp.setImageBitmap(image);
            images.add(temp);
            counter++;
        }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        System.out.println(" We made it this far 8");

        super.onPostExecute(result);
        pd.dismiss();
        if(image!=null)
        {
            fillGridView();
        }

    }   
}
 private Bitmap downloadBitmap(String url) {
     // initilize the default HTTP client object
     final DefaultHttpClient client = new DefaultHttpClient();

     //forming a HttoGet request 
     final HttpGet getRequest = new HttpGet(url);
     try {

         HttpResponse response = client.execute(getRequest);

         //check 200 OK for success
         final int statusCode = response.getStatusLine().getStatusCode();

         if (statusCode != HttpStatus.SC_OK) {
             Log.w("ImageDownloader", "Error " + statusCode + 
                     " while retrieving bitmap from " + url);
             return null;

         }

         final HttpEntity entity = response.getEntity();
         if (entity != null) {
             InputStream inputStream = null;
             try {
                 // getting contents from the stream 
                 inputStream = entity.getContent();

                 // decoding stream data back into image Bitmap that android understands
                 image = BitmapFactory.decodeStream(inputStream);


             } finally {
                 if (inputStream != null) {
                     inputStream.close();
                 }
                 entity.consumeContent();
             }
         }
     } catch (Exception e) {
         // You Could provide a more explicit error message for IOException
         getRequest.abort();
         Log.e("ImageDownloader", "Something went wrong while" +
                 " retrieving bitmap from " + url + e.toString());
     } 

     return image;
 }


}

The key problem is here

05-30 12:12:17.240: W/dalvikvm(838): VFY: unable to resolve exception class 748 (Lcom/fasterxml/jackson/core/JsonParseException;)
05-30 12:12:17.240: W/dalvikvm(838): VFY: unable to find exception handler at addr 0x2a
05-30 12:12:17.240: W/dalvikvm(838): VFY:  rejected Lcom/example/bitmapdisplay/MainActivity$URLArray;.doInBackground ([Ljava/lang/Void;)Ljava/lang/Void;
05-30 12:12:17.240: W/dalvikvm(838): VFY:  rejecting opcode 0x0d at 0x002a
05-30 12:12:17.250: W/dalvikvm(838): VFY:  rejected Lcom/example/bitmapdisplay/MainActivity$URLArray;.doInBackground ([Ljava/lang/Void;)Ljava/lang/Void;
05-30 12:12:17.250: W/dalvikvm(838): Verifier rejected class Lcom/example/bitmapdisplay/MainActivity$URLArray;

and it all goes downhill from there.

Looks like JsonParseException (and possibly other classes from Jackson) are not being included in your APK. This could happen if they're not being exported by Eclipse.

Instead of adding it as an external library, just place jackson-core-x.x.x.jar in the libs folder of your Android project and it should be enough to ensure this.