真机实验碰到类似的问题:
java.lang.ClassNotFoundException: Didn't find class "com.example.newland.f20190820.MainActivity"
on path: DexPathList[[zip file "/data/app/com.example.newland.f20190820-WttI4PUDLJVxUiT8Dt5fZg==
/base.apk"],nativeLibraryDirectories=[/data/app/com.example.newland.f20190820-WttI4PUDLJVxUiT8Dt5f
Zg==/lib/arm64, /system/lib64, /vendor/lib64]]
Process: com.example.newland.f20190820, PID: 23865
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.newland.f20190820/com.example.newland.f20190820.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.newland.f20190820.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.newland.f20190820-WttI4PUDLJVxUiT8Dt5fZg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.newland.f20190820-WttI4PUDLJVxUiT8Dt5fZg==/lib/arm64, /system/lib64, /vendor/lib64]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2879)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3073)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1774)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:198)
at android.app.ActivityThread.main(ActivityThread.java:7055)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:523)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.newland.f20190820.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.newland.f20190820-WttI4PUDLJVxUiT8Dt5fZg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.newland.f20190820-WttI4PUDLJVxUiT8Dt5fZg==/lib/arm64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Instrumentation.newActivity(Instrumentation.java:1175)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2869)
打开AS -> Settings -> Instant Run -> 将Enable Instant Run to hot swap code/resource changes on depoly(default enable)取消勾选,并确认即可
语音播报主要代码:
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//这里要用到内部类并调用自己的对象,所以必须要将声明写在外边
textToSpeech = new TextToSpeech(MainActivity.this , new TextToSpeech.OnInitListener() {
public void onInit(int i) {
if(i==TextToSpeech.SUCCESS){
textToSpeech.speak("你好世界",TextToSpeech.QUEUE_FLUSH,null);
}else {
Log.i("11111","不可用");
}
}
});
}
}