-2

I am looking to click a button inside a fragment and an Activity. The destination activity "GoogleMaps.class" is already inside my manifest and been used inside an onClick in another Activity so that is not the problem. I have tried the following code;

private void onClick_nci()

 {
    Intent intent = new Intent(getActivity(), GoogleMaps.class);
    startActivity(intent);

    }

I have also tried every answer that is on StackOverflow and have not found a solution, have you any idea, thanks.

MyFragmentB.

package com.test.finalproject;

import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
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 MyFragmentB extends Fragment implements OnInitListener, OnClickListener{

private TextToSpeech tts;
private Button btnSpeak;
private TextView txtText;
Button nci;

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




  nci = (Button) myFragmentView.findViewById(R.id.nci);

  tts = new TextToSpeech(getActivity(), this);

  btnSpeak = (Button) myFragmentView.findViewById(R.id.btnSpeak);

  txtText = (TextView) myFragmentView.findViewById(R.id.txtText);

    // button on click event
    btnSpeak.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            speakOut();
        }



    });
    return myFragmentView;
    }
@Override
public void onDestroy() {
    // Don't forget to shutdown!
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
  }



private void onClick_nci(View v)

 {
    Intent intent = new Intent(getActivity(), GoogleMaps.class);
    startActivity(intent);

    }







private void speakOut() {

    String text = txtText.getText().toString();

    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}
@Override
public void onInit(int status) {
    // TODO Auto-generated method stub

}




}

LogCat.

12-09 17:18:42.046: I/Choreographer(11466): Skipped 43 frames! The application may be doing too much work on its main thread. 12-09 17:18:45.246: D/AndroidRuntime(11466): Shutting down VM 12-09 17:18:45.246: W/dalvikvm(11466): threadid=1: thread exiting with uncaught exception (group=0x41c64da0) 12-09 17:18:45.256: E/AndroidRuntime(11466): FATAL EXCEPTION: main 12-09 17:18:45.256: E/AndroidRuntime(11466): Process: com.test.finalproject, PID: 11466 12-09 17:18:45.256: E/AndroidRuntime(11466): java.lang.IllegalStateException: Could not find a method onClick_nci(View) in the activity class com.test.finalproject.Tabs for onClick handler on view class android.widget.Button with id 'nci' 12-09 17:18:45.256: E/AndroidRuntime(11466): at android.view.View$1.onClick(View.java:3956) 12-09 17:18:45.256: E/AndroidRuntime(11466): at android.view.View.performClick(View.java:4637) 12-09 17:18:45.256: E/AndroidRuntime(11466): at android.view.View$PerformClick.run(View.java:19422) 12-09 17:18:45.256: E/AndroidRuntime(11466): at android.os.Handler.handleCallback(Handler.java:733) 12-09 17:18:45.256: E/AndroidRuntime(11466): at android.os.Handler.dispatchMessage(Handler.java:95) 12-09 17:18:45.256: E/AndroidRuntime(11466): at android.os.Looper.loop(Looper.java:136) 12-09 17:18:45.256: E/AndroidRuntime(11466): at android.app.ActivityThread.main(ActivityThread.java:5586) 12-09 17:18:45.256: E/AndroidRuntime(11466): at java.lang.reflect.Method.invokeNative(Native Method) 12-09 17:18:45.256: E/AndroidRuntime(11466): at java.lang.reflect.Method.invoke(Method.java:515) 12-09 17:18:45.256: E/AndroidRuntime(11466): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 12-09 17:18:45.256: E/AndroidRuntime(11466): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 12-09 17:18:45.256: E/AndroidRuntime(11466): at dalvik.system.NativeStart.main(Native Method) 12-09 17:18:45.256: E/AndroidRuntime(11466): Caused by: java.lang.NoSuchMethodException: onClick_nci [class android.view.View] 12-09 17:18:45.256: E/AndroidRuntime(11466): at java.lang.Class.getConstructorOrMethod(Class.java:472) 12-09 17:18:45.256: E/AndroidRuntime(11466): at java.lang.Class.getMethod(Class.java:857) 12-09 17:18:45.256: E/AndroidRuntime(11466): at android.view.View$1.onClick(View.java:3949) 12-09 17:18:45.256: E/AndroidRuntime(11466): ... 11 more

JamesCgrave
  • 35
  • 1
  • 13

4 Answers4

2

Are you sure, you're code is getting called?

Try this to start your intent with the parent Activity context:

getActivity().startActivity(intent)
carstenbaumhoegger
  • 1,575
  • 1
  • 18
  • 22
1

the startActivity method is for Activity class. then you should have an Activity object to call that function. in your Activity you can use startActivity because this refers to an Activity object but in Fragment class, this refers to Fragment object and there is no method StartActivity in Fragment class. you should use your Activity object or use an interface to communicate between your Fragment and your Activity.

here is an example :

private void onClick_nci()

 {
    Intent intent = new Intent(yourFragmentClass.this.getActivity(), GoogleMaps.class);
    yourFragmentClass.this.getActivity().startActivity(intent);

  }
Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36
0

If you want to start new Activity from fragment then you need activity object/reference (getActivity()) to create Intent & call StartActivity() method.

Intent intent = new Intent(getActivity(), GoogleMaps.class);
getActivity().startActivity(intent);
Ganesh AB
  • 4,652
  • 2
  • 18
  • 29
0

Make sure button has onClick attribute in xml.

<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="onClick_nci" />
Mohammed Ali
  • 2,758
  • 5
  • 23
  • 41