NoClassDefFoundError导致的非法状态异常

时间:2016-11-01 06:33:36

标签: java android json illegalstateexception

我正在尝试制作一个简单的Android项目,它接受一个json代码,读取它并在我点击一个按钮时显示输出。我导入了2个外部库,com.fasterxml.jackson.core.jar和com.fasterxml.jackson.databind.jar

这是代码,

package com.example.tejas.comon;

import android.support.v7.app.AppCompatActivity;

import java.io.File;
import java.io.IOException;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "myMessage";  long id;
    String firstname = "";
    String lastname = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void onClick(View view){
        try{



            ObjectMapper mapper = new ObjectMapper();

            JsonNode root = mapper.readTree(new File("E:\\a.json"));

            TextView textViewID =(TextView) findViewById(R.id.text_Id);
            TextView textViewFname =(TextView) findViewById(R.id.text_Fname);
            TextView textViewLname =(TextView) findViewById(R.id.text_Lname);
            //Get ID
            id = root.path("id").asLong();
            textViewID.setText((int)id);

            //Get Name
            JsonNode nameNode = root.path("name");
            if( nameNode.isMissingNode()){
                //do nothing now
            } else {
                firstname = nameNode.path("first").asText();
                lastname = nameNode.path("last").asText();

                textViewFname.setText(firstname);
                textViewLname.setText(lastname);
            }

            Log.i(TAG,Long.toString(id));
            Log.i(TAG,firstname);
            Log.i(TAG,lastname);
        }catch (JsonGenerationException e){
            e.printStackTrace();
        }catch (JsonMappingException e ){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

这是错误

 11-01 11:51:54.573 3561-3561/com.example.tejas.comon E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.tejas.comon, PID: 3561
   java.lang.IllegalStateException: Could not execute method for android:onClick
       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
       at android.view.View.performClick(View.java:4780)
       at android.view.View$PerformClick.run(View.java:19866)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5254)
       at java.lang.reflect.Method.invoke(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    Caused by: java.lang.reflect.InvocationTargetException
       at java.lang.reflect.Method.invoke(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
       at android.view.View.performClick(View.java:4780) 
       at android.view.View$PerformClick.run(View.java:19866) 
       at android.os.Handler.handleCallback(Handler.java:739) 
       at android.os.Handler.dispatchMessage(Handler.java:95) 
       at android.os.Looper.loop(Looper.java:135) 
       at android.app.ActivityThread.main(ActivityThread.java:5254) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:372) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/fasterxml/jackson/annotation/JsonAutoDetect;
       at com.fasterxml.jackson.databind.introspect.VisibilityChecker$Std.<clinit>(VisibilityChecker.java:172)
       at com.fasterxml.jackson.databind.introspect.VisibilityChecker$Std.defaultInstance(VisibilityChecker.java:180)
       at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:195)
       at com.example.tejas.comon.MainActivity.onClick(MainActivity.java:38)
       at java.lang.reflect.Method.invoke(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:372) 
       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
       at android.view.View.performClick(View.java:4780) 
       at android.view.View$PerformClick.run(View.java:19866) 
       at android.os.Handler.handleCallback(Handler.java:739) 
       at android.os.Handler.dispatchMessage(Handler.java:95) 
       at android.os.Looper.loop(Looper.java:135) 
       at android.app.ActivityThread.main(ActivityThread.java:5254) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:372) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    Caused by: java.lang.ClassNotFoundException: Didn't find class "com.fasterxml.jackson.annotation.JsonAutoDetect" on path: DexPathList[[dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-support-annotations-24.2.1_b6343c6d7c5e90b35a6cd0b4b81057315b662eb5-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-internal_impl-24.2.1_b359b2374d8440f9d50bbae0e692ec6e2c581da6-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-internal_impl-24.2.1_ab66133e1fbd3f516301dedaf0cec658eee394b0-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-internal_impl-24.2.1_a6744094956b1304b74e46941d88d9ac7aa74c2a-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-internal_impl-24.2.1_5e587ec0010893c0647a59d4eedb4d6a8a6d0204-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-internal_impl-24.2.1_14cece6c6722e9e44464e611791fca05f5804d5f-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-com.fasterxml.jackson.databind_d8adf718561770d2b8c122c749c1791ccbb850d4-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-com.fasterxml.jackson.core_21a86c83e902b6b59aaee614f85bafbfffc4ad8f-classes.dex", dex file "/data/data/com.example.tejas.comon/files/instant-run/dex/slice-com.android.support-support-vector-drawable-24.2.1_84bbb9c551b4ae409ae8

我正确导入了这两个外部库,并确保它们是gradle sync。

onClick的IllegalStateException是一个在我的其他项目中困扰我的问题。请有人能解决这个问题吗?

编辑1:XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.tejas.comon.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me !"
        android:id="@+id/myButton"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="onClick" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="ID:"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/text_Id"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/myButton" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="First Name:"
        android:id="@+id/textView3"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true"
        android:layout_marginTop="45dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/text_Fname"
        android:layout_alignTop="@+id/textView3"
        android:layout_alignStart="@+id/text_Id" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Last Name:"
        android:id="@+id/textView5"
        android:layout_below="@+id/textView3"
        android:layout_alignParentStart="true"
        android:layout_marginTop="57dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/text_Lname"
        android:layout_alignTop="@+id/textView5"
        android:layout_alignStart="@+id/text_Fname" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

将这些依赖项与最新版本一起使用 1.jackson,数据绑定 2.jackson的注解 3.jackson芯