如何解决此RunTimeException?在按钮上单击时会发生此异常。尝试关闭Instant Run甚至尝试清理并重新启动应用程序,但没有任何效果。这里的实际问题是什么。
我正在使用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
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();
}
}
});
}
}
答案 0 :(得分:4)
我有一个类似的问题,结果在我的清单中,我没有<uses-library android:name="com.google.android.things"/>
。我会尝试删除android:required="false"
,因为您的申请明确要求。
答案 1 :(得分:1)
PeripheralManagerService类已重命名,您需要使用PeripheralManager类。
类型:
import com.google.android.things.pio.PeripheralManager;
而不是:
import com.google.android.things.pio.PeripheralManagerService;
同样输入:`
PeripheralManager manager;
而不是:
PeripheralManagerService manager;
最后输入:
manager = PeripheralManager.getInstance();
而不是:
manager = new PeripheralManagerService();
同时查看此问题:
PeripheralManagerService throws NoClassDefFoundError
`