2014-03-31 54 views
0

這是我的代碼片段沒有錯誤,當它不工作! 每個片段具有語音識別按鈕,inputText的其中接收文本,並給定文本進行比較與inputText的,並強調它紅色如果不匹配..,片段活動不起作用

public class LayoutOne extends Fragment { 

protected static final int RESULT_SPEECH = 1; 

private ImageButton btnSpeak; 
private TextView inputText; 
    private TextView givenText; 

public static Fragment newInstance(Context context) { 
    LayoutOne f = new LayoutOne(); 

    return f; 
} 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.layout_one); 

    inputText = (TextView)getView().findViewById(R.id.inputText); 
    givenText = (TextView)getView().findViewById(R.id.givenText); 
    btnSpeak = (ImageButton)getView().findViewById(R.id.btnSpeak); 
    btnSpeak.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US"); 
      try{ 
       startActivityForResult(intent, RESULT_SPEECH); 
       inputText.setText(""); 
      }catch(ActivityNotFoundException a) { 

      } 
     } 
    }); 
} 

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

    return v; 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
    case RESULT_SPEECH: { 
     if (resultCode == -1 && null != data) { 

      ArrayList<String> text = data 
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      inputText.setText(text.get(0)); 
      if(givenText.getText().toString().equalsIgnoreCase(inputText.getText().toString())){ 

       inputText.setBackgroundColor(Color.parseColor("BLUE")); 
      } 
      else{ 

       {inputText.setBackgroundColor(Color.parseColor("RED"));} 
      } 
     } 
     break; 
     } 
    } 
    } 
} 

什麼想法?

+0

你有一個logcat錯誤stacktrace?嘗試在你的代碼周圍放入一些Log.v(「Test」,「test」),看看你能從中得到什麼信息。 – Tukajo

+0

另外,發佈您的片段充滿的活動。這僅僅是一個片段,而不是一個片段活動。你不會在代碼中的任何地方膨脹這個片段,我可以看到 – Tukajo

+1

的可讀性:'resultCode == -1'應該最好是'resultCode == RESULT_OK' – donfuxx

回答

0

首先,你需要以不同的方式創建片段類作爲您不必吹氣,一個片段不使用通用的onCreate爲其他Android類...

Public class myFragment extends Fragment{ 

TextView testTextView; 
View myFragmentView 
    public View onCreateView(LayoutInflater viewInflation, ViewGroup container, 
      Bundle SavedInstantState) { 

     myFragmentView = viewInflation.inflate(
       R.layout.myfragment_layout, container, false); 
      testTextView = myFragmentView.findViewById(R.id.testTextView); 
    } 
} 

這是一個基本的例子應該如何設置你的片段....然後你可以使用一個事務管理器在FragmentActivity中給它充氣,並創建一個Fragment類的字段。

[更新]

另外,如果你使用Eclipse的可視化編輯器,你可以只需拖放從視覺菜單中選擇一個片段,它應該只問你要附加給它什麼片段類。

+0

這是有道理的@ user3002753 – Tukajo

+0

謝謝。有沒有其他的解決方案,所以我可以在同一個片段中實現比較代碼? – user3002753

+0

我不是在追問你在問什麼? – Tukajo