这是我的图像识别和语音识别应用程序代码。
public class MainActivity extends AppCompatActivity {
ImageView micicon;
ImageView camicon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
micicon = findViewById(R.id.micicon);
camicon = findViewById(R.id.cameraicon);
if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{
Manifest.permission.CAMERA
}, 100);
}
camicon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 100);
}
});
micicon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//python voice
Intent voice = new Intent();
if(!Python.isStarted()){
Python.start(new AndroidPlatform(getApplicationContext()));
}
Python py = Python.getInstance();
PyObject pyf = py.getModule("voice").callAttr("voicerun");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
Bitmap captureImage = (Bitmap) data.getExtras().get("data");
}
}
@Override
public void onBackPressed() {
}
}
当我构建应用程序时,它没有问题。但是当我尝试运行应用程序并使用麦克风图标来使用 python 脚本时,它会显示此错误,并且会崩溃。python 文件在 VSCode 上完美运行。
2020-05-05 03:10:41.978 20585-20585/com.example.medicator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.medicator, PID: 20585
com.chaquo.python.PyException: ModuleNotFoundError: No module named 'voice'
at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:973)
at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:991)
at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:1014)
at <python>.importlib.import_module(__init__.py:127)
at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModule(chaquopy_java.pyx:153)
at com.chaquo.python.Python.getModule(Native Method)
at com.example.medicator.MainActivity$2.onClick(MainActivity.java:57)
at android.view.View.performClick(View.java:6897)
at android.view.View$PerformClick.run(View.java:26101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
如何解决这个问题?我正在使用 Chaquopy 7.0.2。我已经在 Android Studio 中安装了 Python 社区插件。