2013-12-22 39 views
0

我查看了其他同樣問題的問題,但因爲這個錯誤是特定於每個人的個人代碼的,所以他們沒有多大幫助。我收到以下錯誤:需要幫助調試:發送結果失敗 - java.lang.NullPointerException

(我不能上傳圖片的是,Eclipse將不會讓我複製+粘貼錯誤,是因爲我懶,這裏有一個Gyazo:CLICK ME

這裏是我的代碼:

Mikey.java(忽略愚蠢的名字)

package com.jamco.apps.mikey; 

import java.util.ArrayList; 
import java.util.Locale; 
import java.util.Random; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.speech.RecognitionListener; 
import android.speech.RecognizerIntent; 
import android.speech.tts.TextToSpeech; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageButton; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Mikey extends Activity implements RecognitionListener, OnClickListener,  TextToSpeech.OnInitListener { 

protected static final int REQUEST_OK = 1; 
private ImageButton btn; 
private TextToSpeech tts; 
private TextView txt; 
private String thoughts; 
private ArrayList<String> thingsYouSaid; 
private Integer numberOfThingsSaid = 0; 

@Override 
public void onDestroy() { 
    //Don't forget to shutdown tts! 
    if (tts != null) { 
     tts.stop(); 
     tts.shutdown(); 
    } 
    super.onDestroy(); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_mikey); 

    btn = (ImageButton) findViewById(R.id.imageButton1); 
    txt = (TextView) findViewById(R.id.textView1); 

    tts = new TextToSpeech(this, this); 

    btn.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US"); 
      try { 
       startActivityForResult(i, REQUEST_OK); 
     } catch (Exception e) { 
       Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show(); 
     } 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode==REQUEST_OK && resultCode==RESULT_OK) { 
      numberOfThingsSaid += 1; 
      ArrayList<String> youJustSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      ((TextView)findViewById(R.id.textView1)).setText("You said: " + youJustSaid); 
      thingsYouSaid.add(youJustSaid.get(0).toString()); 
      think(); 
     } 
    } 

@Override 
public void onInit(int status) { 
    if (status == TextToSpeech.SUCCESS) { 

     int result = tts.setLanguage(Locale.UK); 

     if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
      Log.e("TTS", "This Language is not supported"); 
     } else { 
      btn.setEnabled(true); 
     } 

    } else { 
     Log.e("TTS", "Initilization Failed!"); 
    } 
} 

@Override 
public void onBeginningOfSpeech() { 
    //TODO Auto-generated method stub 

} 

@Override 
public void onBufferReceived(byte[] buffer) { 
    //TODO Auto-generated method stub 

} 

@Override 
public void onEndOfSpeech() { 
    //TODO Auto-generated method stub 

} 

@Override 
public void onError(int error) { 
    //TODO Auto-generated method stub 

} 

@Override 
public void onEvent(int eventType, Bundle params) { 
    //TODO Auto-generated method stub 

} 

@Override 
public void onPartialResults(Bundle partialResults) { 
    //TODO Auto-generated method stub 

} 

@Override 
public void onReadyForSpeech(Bundle params) { 
    //TODO Auto-generated method stub 

} 

@Override 
public void onResults(Bundle results) { 
    //TODO Auto-generated method stub 

} 

@Override 
public void onRmsChanged(float rmsdB) { 
    //TODO Auto-generated method stub 

} 

private void speak(String speech) { 
    tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null); 
    Toast.makeText(this, speech, Toast.LENGTH_LONG).show(); 
} 

private void think() { 
    if (thingsYouSaid.get(numberOfThingsSaid) == "Hello" || thingsYouSaid.get(numberOfThingsSaid) == "Hi") { 
     switch (randInt(0, 2)) { 
      case 0: 
       speak("Hello"); 
      case 1: 
       speak("Hello"); 
      case 2: 
       speak(thingsYouSaid.get(numberOfThingsSaid)); 
     } 
    } 
} 

public static int randInt(int min, int max) { 

    //Usually this can be a field rather than a method variable 
    Random rand = new Random(); 

    //nextInt is normally exclusive of the top value, 
    //so add 1 to make it inclusive 
    int randomNum = rand.nextInt((max - min) + 1) + min; 

    return randomNum; 
} 

} 

(很抱歉,如果格式不好,我是新來這個網站)。

這裏是我的活動XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".Mikey" > 

<ImageButton 
    android:id="@+id/imageButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="42dp" 
    android:src="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="119dp" 
    android:text="Click the Microphone and ask a question!" /> 

</RelativeLayout> 

堆棧軌跡告訴我去這條線:

thingsYouSaid.add(youJustSaid.get(0).toString()); 

然而,大量的試驗和錯誤我不能工作了之後它出什麼問題了。我嘗試添加.toString()來查看它是否奇蹟般地修復了某些內容,但沒有。

希望有人可以幫我解決這個:)

聖誕快樂!

回答

0

我沒有看到你初始化thingsYouSaid;任何地方。你聲明它是這樣的

private ArrayList<String> thingsYouSaid; 

但你沒有初始化它。嘗試編輯改變,要

private ArrayList<String> thingsYouSaid = new ArrayList<String>(); 

我還沒有和TextToSpeech的工作,但我可以告訴你,你是比較String小號正確這裏

thingsYouSaid.get(numberOfThingsSaid) == "Hello" || thingsYouSaid.get(numberOfThingsSaid) == "Hi") 

think()。您應該使用equals()來比較String

if ((("Hello".equals(thingsYouSaid.get(numberOfThingsSaid)) 
    || ("Hi".equals(thingsYouSaid.get(numberOfThingsSaid)))) 

也,你應該在每個case的末尾添加break;陳述或邏輯都將落空,即使它匹配的第一個case語句之一。

+0

謝謝!我不能相信我忘了這麼做,而且這麼簡單的修復! +1 – IncognitoJam

+0

其實,現在我有另一個問題。現在我可以對我的應用程序和語音識別進行文本編輯,程序將語音文本輸出到TextView,但是,該程序正在輸出看起來像我可以說的多個事物,而不是輸出最可能的結果。我該如何解決?再次感謝! – IncognitoJam

+0

看我的編輯。希望有所幫助。 – codeMagic

0

thingsYouSaid未在您的代碼中的任何位置實例化。你應該初始化它(你可以做到這一點,同時聲明):

ArrayList<String> thingsYouSaid = new ArrayList<String>();