У меня есть полностью работающий текст-на-речь внутри действия с событием onClick, которое работает, я хотел бы переместить функциональность TTS на один из моих фрагментов, возможно ли это? Я видел два вопроса по этому поводу, но не дал правильного ответа, любая помощь была бы высоко оценена.Текст в речь внутри фрагмента
Это моя деятельность в TTS.
package com.test.finalproject;
import java.util.Locale;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class TextTo extends ActionBarActivity implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_to);
// tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText); //
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speakOut();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
// tts.setPitch(5); // set pitch level
// tts.setSpeechRate(2); // set speech speed rate
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed");
}
}
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
MyFragmentA
package com.test.finalproject;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MyFragmentA extends Fragment implements OnClickListener{
private TextToSpeech tts;
private Button btnSpeak;
private TextView txtText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
return myFragmentView;
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (TextView) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speakOut();
}
private void speakOut() {
// TODO Auto-generated method stub
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
private void onClick_TheSpire()
{
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
12-08 21:01:08.717: I/TextToSpeech(1978): Sucessfully bound to com.samsung.SMT
12-08 21: 01: 08,717 D/AndroidRuntime (1978): Выключение ВМ 12-08 21: 01: 08.717: W/dalvikvm (1978) : threadid = 1: выход из потока с неперехваченным исключением (group = 0x41c64da0) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): FATAL EXCEPTION: main 12-08 21: 01: 08.737: E/AndroidRuntime (1978): Процесс: com.test.finalproject, PID: 1978 12-08 21: 01: 08.737: E/AndroidRuntime (1978): java.lang.ClassCastException: android.widget.TextView не может быть cast to android.widget.Button 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at com.test.finalproject.MyFragmentA.onCreateView (MyFragmentA.java:32) 12-08 21: 01: 08.737 : E/AndroidRuntime (1978): at android.support.v4.app.Fragment.performCreateView (Fragment.java:1786) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.support.v4 .app.FragmentManagerImpl.moveToState (FragmentManager.java:947) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1126) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.support.v4.app.BackStackRecord.run (BackStackRecord.java:739) 12-08 21: 01: 08.737: E/AndroidRuntime (1978)): at android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:1489) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions (FragmentManager.java:486) 12-08 21: 01: 08.737: E/AndroidRuntime (1978)): at android.support.v4.app.FragmentPagerAdapter.finishUpdate (FragmentPagerAdapter.java:141) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.support.v4.view.ViewPager.populate (ViewPager.java:1073) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.support.v4.view.ViewPager.populate (ViewPager.java:919) 12-08 21:01 : 08.737: E/AndroidRuntime (1978): at android.support.v4.view.ViewPager.onMeasure (ViewPager.java:1441) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): на android.view .View.measure (View.java:17479) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.view.ViewGroup.measureChildWithMargins (ViewG roup.java:5364) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.widget.FrameLayout.onMeasure (FrameLayout.java:310) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.view.View.measure (View.java:17479) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java: 5364) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure (ActionBarOverlayLayout.java:412) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.view.View.measure (View.java:17479) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java: 5364) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.widget.FrameLayout.onMeasure (FrameLayout. java: 310) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure (PhoneWindow.java:2532) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.view.View.measure (View.java:17479) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.view.ViewRootImpl. performMeasure (ViewRootImpl.java: 2262) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.view.ViewRootImpl.measureHierarchy (ViewRootImpl.java:1391) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:1590) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1249) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.view.ViewRootImpl $ TraversalRunnable.run (ViewRootImpl.java:6585) 12-08 21: 01: 08.737: E/AndroidRuntime (1978) : at android.view.Choreographer $ CallbackRecord.run (Choreographer.java:803) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): на android.view.Choreographer.doCallbacks (Хореограф.ява: 603) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): на android.view.Choreographer. doFrame (Хореограф.java:573) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.view.Choreographer $ FrameDisplayEventReceiver.run (Хореограф.ява: 789) 12-08 21:01: 08.737: E/AndroidRuntime (1978): at android.os.Handler.handleCallback (Handler.java:733) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.os.Handler.dispatchMessage (Handler.java:95) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.os.Looper.loop (Looper.java:136) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at android.app.ActivityThread.main (ActivityThread.java:5586) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at java.lang.reflect.Method.invokeNative (собственный метод) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at java.lang.reflect.Method.invoke (Method.java:515) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1268) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1084) 12-08 21: 01: 08.737: E/AndroidRuntime (1978): at dalvik.system.NativeStart.main (Native Method)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<ScrollView
android:id="@+id/ScrlView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/layoutForScroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom" />
<LinearLayout
android:id="@+id/chart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" />
</LinearLayout>
</ScrollView>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Design Movies Screen"
android:textSize="20dp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/thespire" />
<Button
android:id="@+id/theSpire"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="onClick_TheSpire"
android:text="Directions" />
<Button
android:id="@+id/btnSpeak"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dip"
android:text="Speak" />
<TextView
android:id="@+id/txtText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnSpeak"
android:layout_alignParentLeft="true"
android:text="The Spire - The huge, striking and innovative monument of the Spire stands in the middle of O'Connell Street just across from the famous GPO. 120m high and 3m in diameter at the base, the Spire rises above O’Connell Street, breaking above the roof line with as slender and elegant a movement as is technically possible." />
</RelativeLayout>
где ваш фрагмент ??? Он не отображается в вашем коде !!! –
Ждём добавить сейчас извините – JamesCgrave
@JamesCgrave "android.widget.TextView не может быть отброшен в android.widget.Button" Убедитесь, что ваш идентификатор соответствует вашему расположению правильно. – Razz