Sascha Affolter
- Forum-Beiträge: 2
17.06.2010, 21:53:02 via Website
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
main.xml
The problem is appearing where R.id is used:
And the code will not compile! it says: R.id cannot be resolved
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}
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>
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[...]
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
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.