Issue
Re-Edit: For those interested the method for onClick was resolved below. Turns out you cannot create separate classes for controls so the button controls need to be inside the current active class.
The issue: In my fragment I have included several controls, one of which is a button that should open a new activity. When clicking the button I get the following crash...
This is the LogCat error when clicking the button in my current active Fragment
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.paymentdemo, PID: 14352
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button_next_trans'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
This is the fragment hosting the controls
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/transaction_header"
style="?textGroupheader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="20dp"
android:text="@string/group_header"
android:textStyle="bold" />
<!-- Spinner displays accounts available using SpinnerActivity -->
<include
layout="@layout/fragment_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Radio checks on or off. Still need to link selected as fragments for button on click behaviour -->
<include
layout="@layout/fragment_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Button will take selected radio and open related activity, for now we just go to MainActivity -->
<include
layout="@layout/fragment_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The button control code
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ButtonActivity">
<Button
android:id="@+id/button_next_trans"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="@drawable/action_button"
android:paddingStart="24dp"
android:paddingLeft="24dp"
android:paddingTop="8dp"
android:paddingEnd="24dp"
android:paddingRight="24dp"
android:paddingBottom="8dp"
android:text="@string/next"
android:textColor="@color/colorWhite"
android:onClick="goToPayments"/>
</FrameLayout>
The button activity
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class ButtonActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_button);
}
public void goToPayments(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
I'm not experienced so my implementation may be a bit rusty. If you have any advice on how to properly implement a button onClick method I would be grateful.
Solution
You need to declare the button and View for framgment Like this
Button button;
public View myView;
Then in onCreate initialize like this
myView = inflater.inflate(R.layout.fragment_button,container,false);
button = (Button) myView.findViewById(R.id.button_next_trans);
then create method like this this,
public void payments(){
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(),MainActivity.class)
startActivity(i);
}
}
}
And at end of onCreate() method return view.
return myView;
And call payments(); in onCreate
Answered By - JJIqbal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.