Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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 片段中的可扩展字符串_Java_Android_Regex_Android Fragments_Spannablestring - Fatal编程技术网

Java 片段中的可扩展字符串

Java 片段中的可扩展字符串,java,android,regex,android-fragments,spannablestring,Java,Android,Regex,Android Fragments,Spannablestring,我使用SpannableString为与现有字符串匹配的单词上色 当片段第一次被调用时,这些词就会被发现、匹配并着色,这一点非常有效。但是第二次调用片段时,检查匹配词的匹配和颜色的方法启动,从日志猫中我可以看到这些词被找到并匹配,但没有颜色 我猜这与片段的生命周期以及SpannableString的初始化位置有关 我已尝试在onResume()中初始化SpannableString和中的setUserVisibleHint(布尔值isVisibleToUser) 这是我的方法 public cl

我使用
SpannableString
为与现有字符串匹配的单词上色

当片段第一次被调用时,这些词就会被发现、匹配并着色,这一点非常有效。但是第二次调用片段时,检查匹配词的匹配和颜色的方法启动,从日志猫中我可以看到这些词被找到并匹配,但没有颜色

我猜这与片段的生命周期以及
SpannableString
的初始化位置有关

我已尝试在
onResume()中初始化
SpannableString
和中的
setUserVisibleHint(布尔值isVisibleToUser)

这是我的方法

public class LessonOne extends ActualFragmetHolder  {

@Override
public void init(Bundle savedInstanceState) {
    addSlide(FragmentPronounce.newInstance("The cat is jumping over the     mouse"));

    addSlide(FragmentPronounce.newInstance("This house is really big"));
.....
fragmentSporance.java

public class FragmentPronounce extends Fragment implements View.OnClickListener,RecognitionListener,TextToSpeech.OnInitListener {
  private static final String ARG_OPTION_ONE = "opt1";
  private SpannableString hashText;
  ....


public static FragmentPronounce newInstance(String option1) {

  FragmentPronounce sampleSlide = new FragmentPronounce();

  Bundle args = new Bundle();
  args.putString(ARG_OPTION_ONE, option1);
  sampleSlide.setArguments(args);

  return sampleSlide;
 }


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

 if (getArguments() != null && getArguments().size() != 0) {
  option1 = getArguments().getString(ARG_OPCTION_ONE);

 }
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pronounce_material, container, false);

final TextView the_question = (TextView) v.findViewById(R.id.text_user_must_say);
if (theQuestion != 0) {
  the_question.setText(theQuestion);
}
 //mic button
 fabMic.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    ///starts listening for speech and does its thing
    ///Im using my own custom speech recognition which works fine
    speech.startListening(recognizerIntent);
    ////check for OnResults()
 }
}

@Override
public void onResults(Bundle results) {
//if its a OnResults is fine then
 methodToCheckAnswer(results);
 ....
}

private void methodToCheckAnswer(Bundle results) {
 //checks answer and calls method which counts and colors words in string
 countAndColorWords();
}

//This is the method which check and colors, it triggers fine every time  fragment is called, but it only colors the word the first time is called in the fragment's life cycle
private void countAndColorWords(){
 String strRight = rightanswer;
    .....
    the_question = (TextView) getActivity().findViewById(R.id.text_user_says);
    the_question.setText(theQuestion);
    System.out.println("The question's TEXT" + String.valueOf(the_question.getText().toString()) + "Tokens counted" + tokensCounted + "Option1" + option1 + "What was spoken " + userAnswered);
    hashText = SpannableString.valueOf(the_question.getText().toString());
    System.out.println("hashText Value "+hashText+  "Right Answer "+ rightanswer);
    Matcher matcher = Pattern.compile("\\S+").matcher(strRight);
    Matcher usermatcher = Pattern.compile("\\S+").matcher(userAnswered);
    int groupCounted = 0;
    groupCounted += matcher.groupCount()+1;
    Log.d(TAG, "The position number of groups:"+groupCounted +" "+userAnswered);
    int rightAnswerCount = matcher.groupCount();
    int userAnswerCount = usermatcher.groupCount();
    String rightAnswer;
    String userAnswer;
    int contadorGroup = 0;;
    int matchCounter =0;
    contadorGroup += matcher.groupCount() + 1;

    while (matcher.find() && usermatcher.find()) {
      System.out.println(matcher.start() + ":" + matcher.group());

      rightAnswer = matcher.group();
      userAnswer = usermatcher.group();
      rightAnswerCount += matcher.groupCount();
      userAnswerCount += usermatcher.groupCount();
      String check = "";
      for (int i = 0; i <= rightAnswerCount && i <= userAnswerCount; i++) {

        if (matcher.group(i).equalsIgnoreCase(usermatcher.group(i))) {//matcher.group(i) == usermatcher.group(i)
          matchCounter++;
          check = matcher.group(i);

          hashText.setSpan(new ForegroundColorSpan(Color.parseColor("#64DD17")), matcher.start(), matcher.end(), i);
        }

      }

    }
    //hashText.setSpan(new ForegroundColorSpan(Color.parseColor("#64DD17")), matcher.start(), matcher.end(), palabraColor);
    the_question.setText(hashText);
}
公共类FragmentSproach扩展了片段实现View.OnClickListener、RecognitionListener、TextToSpeech.OnInitListener{
私有静态最终字符串ARG_OPTION_ONE=“opt1”;
私有SpannableString哈希文本;
....
公共静态片段新实例(字符串选项1){
FragmentPrevonce sampleSlide=新的FragmentPrevonce();
Bundle args=新Bundle();
args.putString(ARG_OPTION_ONE,option1);
sampleSlide.setArguments(args);
返回样品滑道;
}
@凌驾
创建时的公共void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
....
如果(getArguments()!=null&&getArguments().size()!=0){
option1=getArguments().getString(ARG\u option\u ONE);
}
}
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图v=充气机。充气(R.layout.fragment\u发音\u材质,容器,假);
final TextView the_question=(TextView)v.findViewById(R.id.text_user_must_say);
如果(问题!=0){
_question.setText(问题);
}
//麦克风按钮
fabMic.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
///开始听演讲并做它的事情
///我使用我自己的自定义语音识别,效果很好
演讲。听讲(识别意图);
////检查OnResults()
}
}
@凌驾
公共结果(捆绑结果){
//如果结果是好的,那么
方法检查(结果);
....
}
私有void方法到checkanswer(捆绑结果){
//检查应答并调用对字符串中的单词进行计数和着色的方法
countAndColorWords();
}
//这是一种检查和着色的方法,每次调用片段时它都会触发,但它只在片段的生命周期中第一次调用单词时着色
私有void countAndColorWords(){
字符串strRight=rightanswer;
.....
问题=(TextView)getActivity().findViewById(R.id.text\u user\u说);
_question.setText(问题);
System.out.println(“问题文本”+String.valueOf(The_question.getText().toString())+“已计数的令牌”+tokensconted+“选项1”+Option1+“所说的内容”+用户回答);
hashText=SpannableString.valueOf(the_question.getText().toString());
System.out.println(“hashText值”+hashText+“正确答案”+rightanswer);
Matcher Matcher=Pattern.compile(“\\S+”).Matcher(strRight);
Matcher usermatcher=Pattern.compile(“\\S+”).Matcher(userresponsed);
int-groupCounted=0;
groupCounted+=matcher.groupCount()+1;
Log.d(标记“组的位置编号:”+groupCounted+”+UserResponsed);
int rightAnswerCount=matcher.groupCount();
int userAnswerCount=usermatcher.groupCount();
字符串右键回答;
字符串userAnswer;
int contadorGroup=0;;
int matchCounter=0;
contadorGroup+=matcher.groupCount()+1;
while(matcher.find()&&usermatcher.find()){
System.out.println(matcher.start()+”:“+matcher.group());
rightAnswer=matcher.group();
userAnswer=usermatcher.group();
rightAnswerCount+=matcher.groupCount();
userAnswerCount+=usermatcher.groupCount();
字符串检查=”;
对于(int i=0;i