java.lang.ClassNotFoundException: Didn't find

2019-08-05 16:59发布

How do I solve this RunTimeException ? This exception occurs when a click is made on the button. Tried turning off Instant Run and even tried cleaning and restarting the application but nothing worked. What is the actual problem here.

I'm using Android Studio.

FATAL EXCEPTION: main

Process: com.shiva, PID: 18632 
                                                       java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/things/pio/PeripheralManagerService;
                                                                         at com.shiva.MainActivity$1.onClick(MainActivity.java:25)
                                                                         at android.view.View.performClick(View.java:5637)
                                                                         at android.view.View$PerformClick.run(View.java:22429)
                                                                         at android.os.Handler.handleCallback(Handler.java:751)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

                                                                      Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.things.pio.PeripheralManagerService" on path: DexPathList[[zip file "/data/app/com.shiva-1/base.apk"],nativeLibraryDirectories=[/data/app/com.shiva-1/lib/x86, /system/lib, /vendor/lib]]
                                                                         at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                         at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                         at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                         at com.shiva.adafona_trial.MainActivity$1.onClick(MainActivity.java:25) 
                                                                         at android.view.View.performClick(View.java:5637) 
                                                                         at android.view.View$PerformClick.run(View.java:22429) 
                                                                         at android.os.Handler.handleCallback(Handler.java:751) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                         at android.os.Looper.loop(Looper.java:154) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
    applicationId "com.shiva"
    minSdkVersion 24
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:support-v4:25.0.+'
compile 'com.android.support:appcompat-v7:+'
provided 'com.google.android.things:androidthings:0.4.1-devpreview'
//provided 'com.google.android.things.pio.PeripheralManagerService'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <uses-library android:name="com.google.android.things" android:required="false"/>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.IOT_LAUNCHER"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
</application>

MainActivity.java

package com.shiva;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.things.pio.PeripheralManagerService;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
Button btnShow;
PeripheralManagerService manager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnShow = (Button)findViewById(R.id.button_click);
    btnShow.setOnClickListener(new View.OnClickListener() {
                @Override
               public void onClick(View v){
                     manager = new PeripheralManagerService();
                     List<String> deviceList = 
manager.getUartDeviceList();
                     if (deviceList.isEmpty()) {
                        Log.i(TAG, "No UART port available on this 
device.");
                     } else {
                        Log.i(TAG, "List of available devices: " + deviceList);
                        Toast.makeText(getApplicationContext(),deviceList.get(0),Toast.LENGTH_LONG).show();
                     }
               }
    });
}
}

2条回答
beautiful°
2楼-- · 2019-08-05 17:11

The PeripheralManagerService class was renamed, you need to use PeripheralManager class instead.

Type:

import com.google.android.things.pio.PeripheralManager;

Instead of:

import com.google.android.things.pio.PeripheralManagerService;

Also Type:`

PeripheralManager manager;

Instead of:

PeripheralManagerService manager;

And finally Type:

manager = PeripheralManager.getInstance();

Instead of:

manager = new PeripheralManagerService();

Also check this question:

PeripheralManagerService throws NoClassDefFoundError

`

查看更多
老娘就宠你
3楼-- · 2019-08-05 17:27

I had a similar issue, and it turns out in my manifest, I did not have <uses-library android:name="com.google.android.things"/>. I would try removing android:required="false", as it is clearly required by your application.

查看更多
登录 后发表回答