Eggsy
Eggsy

Reputation: 133

Fragment to Activity

I have declared a button in a fragment and hooked up with onClick attribute in XML.When I click button in this fragment it should redirect to another class file which extends activity.but the app stops unfortunately.

My Class file

public class ReadingBeginner extends Fragment{

public ReadingBeginner() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


public void Alpha(View v){
    Intent intent = new Intent(this,Alphabet.class);
    startActivity(intent);

}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.activity_reading_beginner, container, false);
}}

My bytton in xml

    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Alpha"
    android:onClick="Alpha"
    android:id="@+id/alphas"
    android:textAlignment="textStart"
    android:background="@drawable/drawable"
    android:drawableLeft="@drawable/ic_alpha"
    android:drawablePadding="15dp"
    android:textAllCaps="false"

    />

This is the class file i need the button to redirect

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Alphabet extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alphabet);
    getIntent();
}

}

Error on Logcat

03-16 03:19:09.061 25450-25450/com.example.world.englishtutor E/AndroidRuntime: FATAL EXCEPTION: main
  java.lang.IllegalStateException: Could not find method Alpha(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'alphas'
  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
  at android.view.View.performClick(View.java:4240)
  at android.view.View$PerformClick.run(View.java:17721)
  at android.os.Handler.handleCallback(Handler.java:730)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:5103)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:525)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  at dalvik.system.NativeStart.main(Native Method)

Upvotes: 2

Views: 4182

Answers (6)

Android Geek
Android Geek

Reputation: 9225

As Doug suggested. Do the following:

 Button button = (Button) root.findViewById(R.id.alphas);
    button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     Intent intent = new Intent(getActivity(),Alphabet.class);
                     startActivity(intent);
                }
            });

Upvotes: 0

Thang BA
Thang BA

Reputation: 162

Remove

1. android:onClick="Alpha" in xml  
2. 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
3.public void Alpha(View v){
Intent intent = new Intent(this,Alphabet.class);
startActivity(intent);
}

And OnCreateView

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

Button btn1 = (Button) root.findViewById(R.id.alphas);
btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             Intent intent = new Intent(getActivity(),Alphabet.class);
    startActivity(intent);
        }
    });
return root;
}

Try it!

Upvotes: 2

Akshat Vajpayee
Akshat Vajpayee

Reputation: 324

First of all you must intialize your button in the fragment.

You must use getActivity() method instead of this here!

public void Alpha(View v){
    Intent intent = new Intent(getActivity(),Alphabet.class);
    startActivity(intent);

}

Upvotes: 0

Parsania Hardik
Parsania Hardik

Reputation: 4623

remove onClick from xml. then Find Button in onCreateView() method of fragment class. then set onClick() of button and startactivity inside it.

in onCreate():

Button btn = (Button) yourinflatedview.findviewbyid (R.id.alphas);
btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 Intent intent = new Intent(getActivity(),Alphabet.class);
        startActivity(intent);
            }
        });

Upvotes: 2

RushDroid
RushDroid

Reputation: 1600

Just Try this.

remove onclick from xml

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Alpha"
    android:id="@+id/alphas"
    android:textAlignment="textStart"
    android:background="@drawable/drawable"
    android:drawableLeft="@drawable/ic_alpha"
    android:drawablePadding="15dp"
    android:textAllCaps="false"

    />

after this put it into onCreateview.

Button b = (Button) findviewbyid (R.id.alphas);
bb2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 Intent intent = new Intent(getActivity(),Alphabet.class);
        startActivity(intent);
            }
        });

otherwise You change getActivity() instead of this

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

it will move fragment to activity. hope it will help you :)

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317968

Your layout is assuming that the Button will invoke a method called 'alpha' in the activity where it was define. This does not work if the method is defined in a fragment.

Instead, in your fragment, you should find the Button in the view hierarchy and register an OnClickListener on it that does what you want.

Upvotes: 3

Related Questions