公共void onActivityResult上的RESULT_OK出错

时间:2016-01-27 13:25:50

标签: java android android-fragments

我在MainActivity上有这个代码,但是当我在我的HomeFragment片段中尝试它时,我在Public void onActivityResult的RESULT_OK上遇到了错误。

我不知道这有什么不对,有人知道吗? 无论如何这里是我的代码:

package com.thesis.artificialintelligence;


import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Random;
import javax.xml.transform.Result;


public class HomeFragment extends Fragment
{

   private TextView resultTEXT;
private TextView resultTEXT2;
private Button button;
TextToSpeech t1;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    t1 = new TextToSpeech(getActivity().getApplicationContext(), new TextToSpeech.OnInitListener()
    {
        @Override
        public void onInit(int status)
        {
            if(status != TextToSpeech.ERROR)
            {
                t1.setLanguage(Locale.UK);
            }
        }
    });

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{

    View view = inflater.inflate(R.layout.fragment_home, container, false);


    resultTEXT = (TextView)view.findViewById(R.id.TVresult);
    resultTEXT2 = (TextView)view.findViewById(R.id.TVresult2);
    button = (Button)view.findViewById(R.id.imageButton);

    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if(v.getId() == R.id.imageButton)
            {

                promptSpeechInput();
            }
        }
    });


    return view;

}



public void promptSpeechInput()
{
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something!");

    try
    {
        startActivityForResult(i, 100);
        resultTEXT.setText("");
        resultTEXT2.setText("");


    }
    catch(ActivityNotFoundException a)
    {
        //Toast.makeText.(MainActivity.this, "Sorry your device does not support speech language! ", Toast.LENGTH_LONG).show();
    }
}


static final String[] texts =
        {
                "I am fine","I am Okay","I am Good","Well I am doing Good"
        };
static final String[] what1 =
        {
                "Yes? what can I help you with?",
                "What?",
                "yes?",
                "Yes? how can I help you?"

        };

public void ReadText()
{
    Random r = new Random();
    String wow = texts[r.nextInt(4)];
    String what = what1[r.nextInt(4)];

     if(toSpeak.equals("how are you") || toSpeak.equals("hi Ashley how are you")|| toSpeak.equals("hey Ashley how are you"))
    {

        t1.speak(wow, TextToSpeech.QUEUE_FLUSH, null);


        resultTEXT2.setText(wow);

    }
    else  if(toSpeak.equals("hey Ashley") || toSpeak.equals("hey")|| toSpeak.equals("Hey")|| toSpeak.equals("Ashley"))
    {

        t1.speak(what, TextToSpeech.QUEUE_FLUSH, null);


        resultTEXT2.setText(what);

    }
}


 public void onActivityResult(int request_code, int result_code, Intent i)
{
    super.onActivityResult(request_code, result_code, i);

    switch (request_code)
    {
        case 100: if(result_code == RESULT_OK && i != null) // I have an error at this line at "RESULT_OK"
        {
            ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            String r1 = result.get(0);

            resultTEXT.setText(result.get(0));
            ReadText();
        }
            break;
    }
}

}

2 个答案:

答案 0 :(得分:2)

RESULT_OK是Activity类的常量,你不能在片段中获得它的直接。使用以下:

if(result_code == Activity.RESULT_OK)

答案 1 :(得分:0)

由于您在[{1}},因此您需要致电Fragment,或者您也可以使用Activity.RESULT_OK,如下所示:

getActivity().RESULT_OK

但是在等待结果时忘记拨打if(result_code == Activity.RESULT_OK){ //stuff } ,例如您的getActivity().startActivityForResult(intent);应如下:

Intent

顺便看看这个:

public void promptSpeechInput(){

Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something!");

try{

    getActivity().startActivityForResult(i, 100); //Here<---- Add getActivity().
    resultTEXT.setText("");
    resultTEXT2.setText("");
}
catch(ActivityNotFoundException a){
    //Toast.makeText.(MainActivity.this, "Sorry your device does not support speech language! ", Toast.LENGTH_LONG).show();
}
}

由于您传递 /** Standard activity result: operation canceled. */ public static final int RESULT_CANCELED = 0; /** Standard activity result: operation succeeded. */ public static final int RESULT_OK = -1; /** Start of user-defined activity results. */ public static final int RESULT_FIRST_USER = 1; 我不知道您是否也希望获得100,所以也许这是另一个问题。