我正在使用片段布局,因此我使用了onCreateView
然后使用了onActivityCreated
方法,但我得到了“APP停止工作”。
package com.example.textspeech;
public class Text extends Fragment{
TextToSpeech t;
private EditText write;
Spinner spi;
SeekBar sb;
public Text(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.text_to_speech, container, false);
return rootView;
}
public void onActivityCreated(Bundle savedInstanceState1) {
super.onActivityCreated(savedInstanceState1);
spi = (Spinner) getActivity().findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> apt = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.name,android.R.layout.simple_list_item_single_choice);
apt.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
spi.setAdapter(apt);
spi.setOnItemSelectedListener(new function1());
write = (EditText) getActivity().findViewById(R.id.editText1);
}
@Override
public void onPause(){
if(t !=null){
t.stop();
t.shutdown();
}
super.onPause();
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getActivity().getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void speakText(View view){
String toSpeak = write.getText().toString();
t.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
public class function1 implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
if(pos==0){
t=new TextToSpeech(getActivity(),new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
t.setLanguage(Locale.US);
t.setSpeechRate(1);
t.setPitch(1);
}
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
这里是Button xml
<Button
android:id="@+id/button1"
android:layout_width="240dp"
android:layout_height="60dp"
android:text="Speak"
android:textColor="#AD2A10"
android:textSize="35sp"
android:onClick="speakText" />
每当我按下按钮时,APP停止响应。 这是logcat
03-29 06:52:45.876: E/AndroidRuntime(816): FATAL EXCEPTION: main
**03-29 06:52:45.876: E/AndroidRuntime(816): java.lang.IllegalStateException: Could not find a method speakText(View) in the activity class com.example.textspeech.MainActivity for onClick handler on view class android.widget.Button with id 'button1'**
03-29 06:52:45.876: E/AndroidRuntime(816): at android.view.View$1.onClick(View.java:3620)
03-29 06:52:45.876: E/AndroidRuntime(816): at android.view.View.performClick(View.java:4240)
03-29 06:52:45.876: E/AndroidRuntime(816): at android.view.View$PerformClick.run(View.java:17721)
03-29 06:52:45.876: E/AndroidRuntime(816): at android.os.Handler.handleCallback(Handler.java:730)
03-29 06:52:45.876: E/AndroidRuntime(816): at android.os.Handler.dispatchMessage(Handler.java:92)
03-29 06:52:45.876: E/AndroidRuntime(816): at android.os.Looper.loop(Looper.java:137)
03-29 06:52:45.876: E/AndroidRuntime(816): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-29 06:52:45.876: E/AndroidRuntime(816): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 06:52:45.876: E/AndroidRuntime(816): at java.lang.reflect.Method.invoke(Method.java:525)
03-29 06:52:45.876: E/AndroidRuntime(816): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-29 06:52:45.876: E/AndroidRuntime(816): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-29 06:52:45.876: E/AndroidRuntime(816): at dalvik.system.NativeStart.main(Native Method)
03-29 06:52:45.876: E/AndroidRuntime(816): Caused by: java.lang.NoSuchMethodException: speakText [class android.view.View]
03-29 06:52:45.876: E/AndroidRuntime(816): at java.lang.Class.getConstructorOrMethod(Class.java:423)
03-29 06:52:45.876: E/AndroidRuntime(816): at java.lang.Class.getMethod(Class.java:787)
03-29 06:52:45.876: E/AndroidRuntime(816): at android.view.View$1.onClick(View.java:3613)
在这个logcat中我应该在活动类com.example.textspeech.Text 中找不到方法speakText(View) 但我得到无法在活动类com.example.textspeech.MainActivity中找到方法speakText(View)
答案 0 :(得分:1)
因为在单击按钮时,您在Button单击方法中获得了write.getText(),并且您已在onActivityCreate中初始化了EditText,但是Fragments运行onCreateView方法,因此您必须初始化
write = (EditText) getActivity().findViewById(R.id.editText1);
in
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.text_to_speech, container, false);
write = (EditText)rootView.findViewById(R.id.editText1);
return rootView;
}
这可能是你的问题。试试吧。
答案 1 :(得分:1)
将此用于每个声明。
button = (Button) rootView.findViewById(R.id.button1);
write = (EditText) rootView.findViewById(R.id.editText1);