Creating Voice Recognition Test APP - Problem

  • Antworten:3
Sascha Affolter
  • Forum-Beiträge: 2

17.06.2010, 21:53:02 via Website

Hello,
I'm just getting started with building APPs and programming in JAVA.
Here is my problem:

I've copied the code from the sample APP "VoiceRecognition" from "API demos" in Eclipse with the Android SDK.
I've also adjusted some of the code to fit in my demo App. Heres the code:

TestApp.java
1package android.testapp;
2
3import android.testapp.R;
4
5import android.app.Activity;
6import android.content.Intent;
7import android.content.pm.PackageManager;
8import android.content.pm.ResolveInfo;
9import android.os.Bundle;
10import android.speech.RecognizerIntent;
11import android.view.View;
12import android.view.View.OnClickListener;
13import android.widget.ArrayAdapter;
14import android.widget.Button;
15import android.widget.ListView;
16
17import java.util.ArrayList;
18import java.util.List;
19
20/**
21 * Sample code that invokes the speech recognition intent API.
22 */
23public class TestApp extends Activity implements OnClickListener {
24
25 private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
26
27 private ListView mList;
28
29 /**
30 * Called with the activity is first created.
31 */
32 @Override
33 public void onCreate(Bundle savedInstanceState) {
34 super.onCreate(savedInstanceState);
35
36 // Inflate our UI from its XML layout description.
37 setContentView(R.layout.main);
38
39 // Get display items for later interaction
40 Button speakButton = (Button) findViewById(R.id.btn_speak);
41
42 mList = (ListView) findViewById(R.id.list);
43
44 // Check to see if a recognition activity is present
45 PackageManager pm = getPackageManager();
46 List<ResolveInfo> activities = pm.queryIntentActivities(
47 new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
48 if (activities.size() != 0) {
49 speakButton.setOnClickListener(this);
50 } else {
51 speakButton.setEnabled(false);
52 speakButton.setText("Recognizer not present");
53 }
54 }
55
56 /**
57 * Handle the click on the start recognition button.
58 */
59 public void onClick(View v) {
60 if (v.getId() == R.id.btn_speak) {
61 startVoiceRecognitionActivity();
62 }
63 }
64
65 /**
66 * Fire an intent to start the speech recognition activity.
67 */
68 private void startVoiceRecognitionActivity() {
69 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
70 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
71 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
72 intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
73 startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
74 }
75
76 /**
77 * Handle the results from the recognition activity.
78 */
79 @Override
80 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
81 if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
82 // Fill the list view with the strings the recognizer thought it could have heard
83 ArrayList<String> matches = data.getStringArrayListExtra(
84 RecognizerIntent.EXTRA_RESULTS);
85 mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
86 matches));
87 }
88
89 super.onActivityResult(requestCode, resultCode, data);
90 }
91}

main.xml
1<?xml version="1.0" encoding="utf-8"?>
2
3<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 android:orient ation="vertical">
7
8 <TextView
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:paddingBottom="4dip"
12 android:text="@string/voice_recognition_prompt" />
13
14 <Button android:id="@+id/btn_speak"
15 android:layout_width="fill_parent"
16 android:layout_height="wrap_content"
17 android:text="@string/speak_button" />
18
19 <ListView android:id="@+id/list"
20 android:layout_width="fill_parent"
21 android:layout_height="0dip"
22 android:layout_weight="1" />
23
24 </LinearLayout>

The problem is appearing where R.id is used:
1[...]
2
3 Button speakButton = (Button) findViewById(R.id.btn_speak);
4 mList = (ListView) findViewById(R.id.list);
5
6[...]

And the code will not compile! it says: R.id cannot be resolved

Antworten
Andy N.
  • Forum-Beiträge: 3.112

17.06.2010, 22:13:06 via Website

Did you use API Level 7+?

Antworten
Gelöschter Account
  • Forum-Beiträge: 48

17.06.2010, 23:08:33 via Website

line 6 -> android:orient ation="vertical"> <- remove the space in orientation! if you have syntax errors in one of the xml files, the R class will not generated...
(mhmm..but i think the error message in that case would be : "R cannot be resolved" ?? ...)

maybe the resources/strings are missing in strings.xml ?!

— geändert am 17.06.2010, 23:24:09

Antworten
Sascha Affolter
  • Forum-Beiträge: 2

18.06.2010, 01:23:55 via Website

Andy N.
Did you use API Level 7+?
Sorry I don't know that, Im new to that stuff..

line 6 -> android:orient ation="vertical"> <- remove the space in orientation! if you have syntax errors in one of the xml files, the R class will not generated...
(mhmm..but i think the error message in that case would be : "R cannot be resolved" ?? ...)

maybe the resources/strings are missing in strings.xml ?!
Yeah, but the original sample does compile this code..
And after fixing that, there also was no change..

No it says: R.id cannot be resolved and underlines only R.id red!

Here the original sample:
1package com.example.android.apis.app;
2
3import com.example.android.apis.R;
4
5import android.app.Activity;
6import android.content.Intent;
7import android.content.pm.PackageManager;
8import android.content.pm.ResolveInfo;
9import android.os.Bundle;
10import android.speech.RecognizerIntent;
11import android.view.View;
12import android.view.View.OnClickListener;
13import android.widget.ArrayAdapter;
14import android.widget.Button;
15import android.widget.ListView;
16
17import java.util.ArrayList;
18import java.util.List;
19
20/**
21 * Sample code that invokes the speech recognition intent API.
22 */
23public class VoiceRecognition extends Activity implements OnClickListener {
24
25 private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
26
27 private ListView mList;
28
29 /**
30 * Called with the activity is first created.
31 */
32 @Override
33 public void onCreate(Bundle savedInstanceState) {
34 super.onCreate(savedInstanceState);
35
36 // Inflate our UI from its XML layout description.
37 setContentView(R.layout.voice_recognition);
38
39 // Get display items for later interaction
40 Button speakButton = (Button) findViewById(R.id.btn_speak);
41
42 mList = (ListView) findViewById(R.id.list);
43
44 // Check to see if a recognition activity is present
45 PackageManager pm = getPackageManager();
46 List<ResolveInfo> activities = pm.queryIntentActivities(
47 new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
48 if (activities.size() != 0) {
49 speakButton.setOnClickListener(this);
50 } else {
51 speakButton.setEnabled(false);
52 speakButton.setText("Recognizer not present");
53 }
54 }
55
56 /**
57 * Handle the click on the start recognition button.
58 */
59 public void onClick(View v) {
60 if (v.getId() == R.id.btn_speak) {
61 startVoiceRecognitionActivity();
62 }
63 }
64
65 /**
66 * Fire an intent to start the speech recognition activity.
67 */
68 private void startVoiceRecognitionActivity() {
69 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
70 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
71 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
72 intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
73 startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
74 }
75
76 /**
77 * Handle the results from the recognition activity.
78 */
79 @Override
80 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
81 if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
82 // Fill the list view with the strings the recognizer thought it could have heard
83 ArrayList<String> matches = data.getStringArrayListExtra(
84 RecognizerIntent.EXTRA_RESULTS);
85 mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
86 matches));
87 }
88
89 super.onActivityResult(requestCode, resultCode, data);
90 }
91}

In this code Eclips doesn't underline R.id!

Oh and the whole XML code is underlined in yellow exept the one of the original..

PS: Didn't know about strings.xml this now looks like that (since this change the main.xml isn't yellow underlined anymore):
1<?xml version="1.0" encoding="utf-8"?>
2<resources>
3 <string name="voice_recognition_prompt"></string>
4 <string name="speak_button"></string>
5</resources>

PS: Grr.. did remove <string name="voice_recognition_prompt"></string> now it works, anyway thanks!!!

— geändert am 18.06.2010, 01:38:01

Antworten