需要帮助调试:发送失败的结果 - java.lang.NullPointerException

时间:2013-12-22 19:59:23

标签: java android 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>

Stack-trace告诉我转到这一行:

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

然而,经过大量的反复试验后,我无法弄清楚它有什么问题。我尝试添加.toString()只是为了看它是否神奇地修复了一些东西,但没有。

希望有人能够帮我解决这个问题:)

圣诞快乐!

2 个答案:

答案 0 :(得分:0)

thingsYouSaid未在代码中的任何位置实例化。你应该实例化它(你可以在声明时这样做):

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

答案 1 :(得分: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 s。

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

另外,您应该在每个break;的末尾添加case语句,否则即使逻辑与第一个case语句之一匹配,逻辑也会失效。