我正在构建一个语音识别应用程序,当我说出一个特定的单词时会做某些事情,例如" open"它打开了一些东西,但问题是我的应用程序在我的手机(真实设备)上运行时仍然崩溃,我点击说话按钮。我不知道还能做什么?我试过给它上网和语音识别许可,但它仍然没有帮助
这是java(android studio)中的代码
public class MainActivity extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
private TextView resultText;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button speakButton = (Button) findViewById(R.id.SpeakButton);
speakButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
startVoiceRecognitionActivity();
}
});
}
void startVoiceRecognitionActivity(){
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
@Override
protected void onActivityResult (int requestCode,int resultCode, Intent data){
String wordStr = null;
String[] words = null;
String firstWord = null;
String secondWord = null;
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK)
{
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
wordStr = matches.get(0);
words = wordStr.split(" ");
firstWord = words[0];
secondWord = words[1];
}
if (firstWord.equals("open"))
{
resultText = (TextView)findViewById(R.id.ResultText);
resultText.setText("Results: Open Command Works");
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.starlinginteractivesoftworks.musiccompanion">
<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-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我看着日志,它说:
08-07 20:12:57.813 14350-14350 /? E / BoostFramework:BoostFramework(): Exception_1 = java.lang.ClassNotFoundException:没找到类 &#34; com.qualcomm.qti.Performance&#34;在路径上: DexPathList [[],nativeLibraryDirectories = [/系统/ lib64下, / vendor / lib64]] 08-07 20:12:58.509 14350-14350 / com.starlinginteractivesoftworks.musiccompanion E / AndroidRuntime:致命异常:主要 过程:com.starlinginteractivesoftworks.musiccompanion,PID:14350 android.content.ActivityNotFoundException:找不到要处理的Activity 意图{act = android.speech.action.RECOGNIZE_SPEECH launchParam = MultiScreenLaunchParams {mDisplayId = 0 mFlags = 0}(已 额外的)} 在 android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1839) 在 android.app.Instrumentation.execStartActivity(Instrumentation.java:1531) 在android.app.Activity.startActivityForResult(Activity.java:4399) 在android.app.Activity.startActivityForResult(Activity.java:4358) 在 com.starlinginteractivesoftworks.musiccompanion.MainActivity.startVoiceRecognitionActivity(MainActivity.java:55) 在 com.starlinginteractivesoftworks.musiccompanion.MainActivity $ 1.onClick(MainActivity.java:43) 在android.view.View.performClick(View.java:6205) 在android.widget.TextView.performClick(TextView.java:11103) 在android.view.View $ PerformClick.run(View.java:23653) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
答案 0 :(得分:0)
boost框架日志只是一个警告,不用担心。
是真正的问题ActivityNotFoundException:找不到处理Intent的活动
它可以来自:
确保您安装了识别应用程序(有关详细信息,请参阅ActivityNotFoundException: No Activity found to handle Intent (RECOGNIZE_SPEECH))。
尝试按照here所述启用离线模式:
抓住异常并打开webview下载识别应用程序:
try{
...
}
catch(ActivityNotFoundException e) {
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://market.android.com/details?id=APP_PACKAGE_NAME"));
startActivity(i);
}
在startActivity之前检查识别应用是否可用(请参阅https://stackoverflow.com/a/35290037/2667536):
PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
//Then there is application can handle your intent
}else{
//No Application can handle your intent
}