0

I am using fragment layout, so I used onCreateView and then used the onActivityCreated method, but I am getting "APP Stopped Working".

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
    } 
 }
}

here is 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" />

Whenever I am pressing Button , so APP STOPPED RESPONDING. here is 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)

in this logcat I should get Could not find a method speakText(View) in the activity class com.example.textspeech.Text but I am getting Could not find a method speakText(View) in the activity class com.example.textspeech.MainActivity

Manoj ahirwar
  • 1,062
  • 1
  • 10
  • 24

3 Answers3

3

Because on button click you are getting write.getText() in your Button click method and you have initialized your EditText in onActivityCreate but Fragments run onCreateView method so you have to initialize your

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;

}

This might be your problem. Try it.

1

use this for every declaration.

button = (Button) rootView.findViewById(R.id.button1);
write = (EditText) rootView.findViewById(R.id.editText1);
0

it looks like it is looking for the speakText() function in your Activity, and it lives in your Fragment. button1 has an onClick() handler (i don't see it in your code sample that you posted, maybe i'm missing it) that refers to speakText() but it is in your Activity somehow and not in your Fragment. It is looking for the function in MainActivity, you can see.

see Android Fragment onClick button Method

cmaroney
  • 283
  • 3
  • 9