问题描述
如何解决此RunTimeException?单击按钮时,会发生此异常.试图关闭即时运行",甚至尝试清理并重新启动应用程序,但没有任何效果.这里的实际问题是什么.
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.
我正在使用Android Studio.
I'm using Android Studio.
致命异常:主要
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
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();
}
}
});
}
}
推荐答案
我遇到了类似的问题,事实证明,我没有< uses-library android:name ="com.google.android.things"/>
.我会尝试删除 android:required ="false"
,因为您的应用程序显然需要这样做.
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.
这篇关于java.lang.ClassNotFoundException:找不到类"com.google.android.things.pio.PeripheralManagerService"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!