致命异常:与java.lang.NoClassDefFoundError相关的AsyncTask#1

时间:2019-05-27 07:31:43

标签: java android jsonparser

我在教授教给我们的东西上做过同样的事情,但是当我尝试在自己的计算机上(我在学校的计算机上做过)时,我发现与AsyncTask有关的错误java.lang.NoClassDefFoundError,但是我是使用Java进行android开发的新手,原因是我在移动开发中使用react-native。

我没有尝试其他任何事情,因为我在Java和Android Studio中有点菜鸟。 但是我发现我的系统和学校有一些不同的规格或软件版本。

Softare/Tool   |  My_SYSTEM | School_SYSTEM
Android Studio | 3.0+       | 2.1
Java           |  8         | 8
Javac          |  8         | 11

我的软件或工具是否存在问题,导致我出错

这是doInBackground方法

@Override
        protected String doInBackground(String... args0) {

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("lastname", lname));

            JSONObject json = jsonParser.makeHttpRequest(url_sample1, "POST", params);
            Log.d("Create Response", json.toString());

            try {
                int success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    //success
                    String success_res = "Success";
                    Log.d("Success Response", success_res);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "Successfully Saved!",
                                    Toast.LENGTH_LONG).show();
                        }
                    });

                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "Error",
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

这是我的PHP文件。我已经在浏览器中仔细检查了它,仍然没有错误

<?php
include("connection.php");

$response = array();

if(isset($_POST['lastname'])){
    $last = $_POST['lastname'];
    $result = mysqli_query($con, "INSERT INTO tblsample1('lname') VALUES ('$last')");

    if($result){
        $response["success"] = 1;
        $response["message"] = "successful";
        echo json_encode($response);
    }else{
        $response["success"] = 0;
        $response["message"] = "error";
        echo json_encode($response);
    }
}else{
    $response["success"] = 0;
        $response["message"] = "Please input lastname";
        echo json_encode($response);
}


?>

我还在AndroidManifest上包含了权限标签,因此我可以在自己的Android应用上使用互联网。

每当我按下“提交”按钮时,这就是我的logcat

2019-05-27 15:06:25.887 13296-13330/com.example.prjsample1 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.example.prjsample1, PID: 13296
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:354)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/logging/LogFactory;
        at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:187)
        at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146)
        at com.example.prjsample1.JSONParser.makeHttpRequest(JSONParser.java:42)
        at com.example.prjsample1.MainActivity$sample1.doInBackground(MainActivity.java:69)
        at com.example.prjsample1.MainActivity$sample1.doInBackground(MainActivity.java:51)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 
     Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.commons.logging.LogFactory" on path: DexPathList[[zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/base.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_resources_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/lib/x86, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:187) 
        at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146) 
        at com.example.prjsample1.JSONParser.makeHttpRequest(JSONParser.java:42) 
        at com.example.prjsample1.MainActivity$sample1.doInBackground(MainActivity.java:69) 
        at com.example.prjsample1.MainActivity$sample1.doInBackground(MainActivity.java:51) 
        at android.os.AsyncTask$2.call(AsyncTask.java:333) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 
        Suppressed: java.io.IOException: No original dex files found for dex location /data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_resources_apk.apk
        at dalvik.system.DexFile.openDexFileNative(Native Method)
        at dalvik.system.DexFile.openDexFile(DexFile.java:354)
        at dalvik.system.DexFile.<init>(DexFile.java:101)
        at dalvik.system.DexFile.<init>(DexFile.java:75)
        at dalvik.system.DexPathList.loadDexFile(DexPathList.java:394)
        at dalvik.system.DexPathList.makeDexElements(DexPathList.java:354)
        at dalvik.system.DexPathList.<init>(DexPathList.java:164)
        at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:74)
        at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
        at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
        at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)
        at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)
2019-05-27 15:06:25.890 13296-13330/com.example.prjsample1 E/AndroidRuntime:     at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:74)
        at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:40)
        at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:727)
        at android.app.LoadedApk.getClassLoader(LoadedApk.java:810)
        at android.app.LoadedApk.getResources(LoadedApk.java:1032)
        at android.app.ContextImpl.createAppContext(ContextImpl.java:2345)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5749)
        at android.app.ActivityThread.access$1100(ActivityThread.java:199)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

当我按下提交按钮时,我应该进行一次toastmaketest,就像我们在学校计算机上所做的那样

1 个答案:

答案 0 :(得分:0)

崩溃提示在路径上找不到类“ org.apache.commons.logging.LogFactory”。

这意味着缺少 slf4j 的依赖项。将 slf4j 添加到模块级别build.gradle文件中。

// https://mvnrepository.com/artifact/org.slf4j/slf4j-android
implementation group: 'org.slf4j', name: 'slf4j-android', version: '1.7.26'

如何将依赖项添加到build.gradle文件中。

  1. 打开项目工具窗口(快捷方式:Windows:Alt + 1或Mac:Command + 1)。 [您会在Android Studio的左上角看到它。]

  2. 展开app文件夹以找到build.gradle文件。 [将有多个build.gradle文件。确保文件位于appsrc文件夹旁边的build文件夹中,而不位于文件外部。]

  3. 打开build.gradle文件并向下滚动以找到dependencies块。 [看起来像下面的东西。]

    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
        implementation "com.android.support:appcompat-v7:$supportVersion"
        implementation "com.android.support.constraint:constraint-layout:$constraintVersion"
    ...
        implementation group: 'org.slf4j', name: 'slf4j-android', version: '1.7.26'
    }
    
  4. 将依赖项添加到块中(如上)。