Я пытаюсь реализовать Voice Interactor в своем проекте, и основная идея его использования заключается в том, что пользователь будет вызывать определенную функцию, произнося слово (например, «Окей, Google»), а затем приложение будет перемещаться или действовать соответственно . Я просмотрел несколько руководств и нашел этот фрагмент кода, но когда пытаюсь реализовать, выдает исключение времени выполнения. Пожалуйста, найдите мой фрагмент кода ниже и признательны за вашу помощь.
Файл манифеста
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.ivy.ivyvoicebot.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.ivy.ivyvoicebot.MY_ACTION_INTENT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.VOICE" />
</intent-filter>
<intent-filter>
<action android:name="android.media.action.STILL_IMAGE_CAMERA"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.VOICE"/>
</intent-filter>
<intent-filter>
<action android:name="com.google.android.gms.actions.SEARCH_ACTION"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.VOICE"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="com.google.android.voicesearch.SELF_NOTE" />
</intent-filter>
</activity>
</application>
MainActivity. java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
startVoiceTrigger();
} else {
Toast.makeText(this, "Device not supported", Toast.LENGTH_SHORT).show();
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void startVoiceTrigger() {
VoiceInteractor.PickOptionRequest.Option option1 = new VoiceInteractor.PickOptionRequest.Option("cheese", 1);
option1.addSynonym("ready");
option1.addSynonym("go");
option1.addSynonym("take it");
option1.addSynonym("ok");
VoiceInteractor.Prompt prompt = new VoiceInteractor.Prompt("Say Cheese");
VoiceInteractor voiceInteractor = getVoiceInteractor();
voiceInteractor.submitRequest(new VoiceInteractor.PickOptionRequest(prompt,
new VoiceInteractor.PickOptionRequest.Option[]{option1}, null) {
@Override
public void onPickOptionResult(boolean finished, Option[] selections, Bundle result) {
if (finished && selections.length == 1) {
Message message = Message.obtain();
message.obj = result;
Toast.makeText(MainActivity.this, "Navigate To next screen", Toast.LENGTH_SHORT).show();
} else {
getActivity().finish();
}
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.this, "Didn't work", Toast.LENGTH_SHORT).show();
getActivity().finish();
}
});
}
}
Сообщение об ошибке:
2020-06-19 17:02:51.633 23588-23588/com.ivy.ivyvoicebot E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ivy.ivyvoicebot, PID: 23588
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ivy.ivyvoicebot/com.ivy.ivyvoicebot.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.VoiceInteractor.submitRequest(android.app.VoiceInteractor$Request)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3375)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3514)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2110)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7697)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.app.VoiceInteractor.submitRequest(android.app.VoiceInteractor$Request)' on a null object reference
at com.ivy.ivyvoicebot.MainActivity.startVoiceTrigger(MainActivity.java:259)
at com.ivy.ivyvoicebot.MainActivity.onCreate(MainActivity.java:85)
at android.app.Activity.performCreate(Activity.java:7815)
at android.app.Activity.performCreate(Activity.java:7804)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1325)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3514)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2110)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7697)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
2020-06-19 17:02:51.640 23588-23588/com.ivy.ivyvoicebot I/Process: Sending signal. PID: 23588 SIG: 9
Ссылки:
https://codelabs.developers.google.com/codelabs/voice-interaction/index.html?index=..%2F..index#6
https://hub.packtpub.com/voice-interaction-and-android-marshmallow/
https://www.mobindustry.net/how-to-implement-voice-interactions-into-your-android-app/