Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在ActivityResult上的Public void上的结果_OK中出错_Java_Android_Android Fragments - Fatal编程技术网

Java 在ActivityResult上的Public void上的结果_OK中出错

Java 在ActivityResult上的Public void上的结果_OK中出错,java,android,android-fragments,Java,Android,Android Fragments,我的MainActivity上有这段代码,但当我在fragment(我的HomeFragment)上尝试它时,我在结果\u OK(在ActivityResult上的Public void上)上出错 我不知道这有什么问题,有人知道吗? 总之,这是我的代码: package com.thesis.artificialintelligence; import android.app.Activity; import android.content.ActivityNotFoundException

我的MainActivity上有这段代码,但当我在fragment(我的HomeFragment)上尝试它时,我在结果\u OK(在ActivityResult上的Public void上)上出错

我不知道这有什么问题,有人知道吗? 总之,这是我的代码:

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;
    }
}
package com.thesis.artificialintelligence;
导入android.app.Activity;
导入android.content.ActivityNotFoundException;
导入android.content.ComponentName;
导入android.content.Intent;
导入android.media.AudioManager;
导入android.os.Bundle;
导入android.speech.RecognizerIntent;
导入android.speech.tts.TextToSpeech;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.TextView;
导入java.util.ArrayList;
导入java.util.Calendar;
导入java.util.GregorianCalendar;
导入java.util.HashMap;
导入java.util.Locale;
导入java.util.Random;
导入javax.xml.transform.Result;
公共类HomeFragment扩展了片段
{
私有文本查看结果文本;
私有文本视图resultTEXT2;
私人按钮;
texttospeecht1;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
t1=新建TextToSpeech(getActivity().getApplicationContext(),新建TextToSpeech.OnInitListener())
{
@凌驾
公共无效onInit(int状态)
{
if(状态!=TextToSpeech.ERROR)
{
t1.setLanguage(Locale.UK);
}
}
});
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态)
{
视图=充气机。充气(R.layout.fragment\u home,container,false);
resultTEXT=(TextView)view.findViewById(R.id.TVresult);
resultTEXT2=(TextView)view.findViewById(R.id.TVresult2);
button=(button)view.findViewById(R.id.imageButton);
setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
if(v.getId()==R.id.imageButton)
{
promptSpeechInput();
}
}
});
返回视图;
}
公共无效promptSpeechInput()
{
意图i=新意图(识别者意图、行动、识别、讲话);
i、 putExtra(RecognizerIntent.EXTRA语言模型,RecognizerIntent.LANGUAGE模型自由形式);
i、 putExtra(RecognizerIntent.EXTRA_语言,Locale.getDefault());
i、 putExtra(RecognizerIntent.EXTRA_提示符,“说点什么!”);
尝试
{
startActivityForResult(i,100);
resultTEXT.setText(“”);
resultTEXT2.setText(“”);
}
捕获(ActivityNotFoundException a)
{
//Toast.makeText.(MainActivity.this,“对不起,您的设备不支持语音语言!”,Toast.LENGTH\u LONG.show();
}
}
静态最终字符串[]文本=
{
“我很好”,“我很好”,“我很好”,“我做得很好”
};
静态最终字符串[]what1=
{
“是吗?我能帮你什么忙吗?”,
“什么?”,
“是吗?”,
“是吗?我能帮你什么忙吗?”
};
public void ReadText()
{
随机r=新随机();
字符串wow=文本[r.nextInt(4)];
字符串what=what1[r.nextInt(4)];
如果(toSpeak.equals(“你好”)| toSpeak.equals(“你好,艾希礼”)| toSpeak.equals(“你好,艾希礼,你好”))
{
t1.说话(哇,TextToSpeech.QUEUE_FLUSH,null);
结果xt2.setText(wow);
}
否则如果(toSpeak.equals(“hey Ashley”)| toSpeak.equals(“hey”)| toSpeak.equals(“hey”)| toSpeak.equals(“Ashley”))
{
t1.speak(what,TextToSpeech.QUEUE_FLUSH,null);
resultTEXT2.setText(什么);
}
}
ActivityResult上的公共无效(int请求代码、int结果代码、意图i)
{
super.onActivityResult(请求代码、结果代码、i);
开关(请求代码)
{
案例100:if(result\u code==result\u OK&&i!=null)//我在这行的“result\u OK”处有一个错误
{
ArrayList结果=i.getStringArrayListExtra(RecognizerIntent.EXTRA_结果);
字符串r1=result.get(0);
resultTEXT.setText(result.get(0));
ReadText();
}
打破
}
}

}

结果\u OK
是活动类的常量,您无法在片段中直接获得它。使用以下内容:

if(result_code == Activity.RESULT_OK)

由于您在
片段上
,因此必须调用
活动。RESULT\u OK
或也可以使用
getActivity()。RESULT\u OK
如下所示:

if(result_code == Activity.RESULT_OK){
     //stuff
}
但是不要忘记调用
getActivity()当您等待结果时,例如,您的
意图应该如下所示:

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();
}
}
由于您正在传递一个
100
,我不知道您是否也想获得该
100
,因此可能这是另一个问题

 /** 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;