我使用了现有的语音识别示例代码。但这段代码在我的 eclipse 中不起作用。我在我的 logcat 中出现以下错误。但这段代码在其他人中完美运行。我参考了很多教程,但我不清楚我把错误放在哪里?
我的代码:
public class MainActivity extends Activity {
private static final int REQUEST_CODE = 1234;
private ListView wordsList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button speakButton = (Button) findViewById(R.id.speakButton);
wordsList = (ListView) findViewById(R.id.list);
// Disable button if no recognition service is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0)
{
speakButton.setEnabled(false);
speakButton.setText("Recognizer not present");
}
}
/**
* Handle the action of the button being clicked
*/
public void speakButtonClicked(View v)
{
startVoiceRecognitionActivity();
}
/**
* Fire an intent to start the voice recognition activity.
*/
private void startVoiceRecognitionActivity()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
startActivityForResult(intent, REQUEST_CODE);
}
/**
* Handle the results from the voice recognition activity.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
// Populate the wordsList with the String values the recognition engine thought it heard
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
matches));
}
super.onActivityResult(requestCode, resultCode, data);
}
日志猫:
09-01 23:29:39.441: E/AndroidRuntime(225): Uncaught handler: thread main exiting due to uncaught exception
09-01 23:29:39.580: E/AndroidRuntime(225): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.voicerecog/com.example.voicerecog.MainActivity}: java.lang.NullPointerException
09-01 23:29:39.580: E/AndroidRuntime(225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
09-01 23:29:39.580: E/AndroidRuntime(225): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
09-01 23:29:39.580: E/AndroidRuntime(225): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
09-01 23:29:39.580: E/AndroidRuntime(225): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
09-01 23:29:39.580: E/AndroidRuntime(225): at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 23:29:39.580: E/AndroidRuntime(225): at android.os.Looper.loop(Looper.java:123)
09-01 23:29:39.580: E/AndroidRuntime(225): at android.app.ActivityThread.main(ActivityThread.java:4203)
09-01 23:29:39.580: E/AndroidRuntime(225): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 23:29:39.580: E/AndroidRuntime(225): at java.lang.reflect.Method.invoke(Method.java:521)
09-01 23:29:39.580: E/AndroidRuntime(225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-01 23:29:39.580: E/AndroidRuntime(225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
09-01 23:29:39.580: E/AndroidRuntime(225): at dalvik.system.NativeStart.main(Native Method)
09-01 23:29:39.580: E/AndroidRuntime(225): Caused by: java.lang.NullPointerException
09-01 23:29:39.580: E/AndroidRuntime(225): at com.example.voicerecog.MainActivity.onCreate(MainActivity.java:38)
09-01 23:29:39.580: E/AndroidRuntime(225): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
09-01 23:29:39.580: E/AndroidRuntime(225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
09-01 23:29:39.580: E/AndroidRuntime(225): ... 11 more