2016-01-27 33 views
-3

我在我的MainActivity上有這段代碼,但是當我在HomeFragment的片段上嘗試它時,我在Public void onActivityResult的RESULT_OK處得到了一個錯誤。RESULT_OK上的錯誤public void onActivityResult

我不知道這是怎麼回事,有沒有人知道這件事? 反正here`s我的代碼:

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

RESULT_OK是恆定的Activity類的,你不能在片段得到它的照片直接。使用下列內容:

if(result_code == Activity.RESULT_OK) 
0

既然你是一個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你想要得到的是100了,所以也許這是另外一個問題,我不knowif。

相關問題