-1

So today I created tab view bar and fragments and added some image buttons to one of the fragments. When I add android:onClick="name"on the button code and write Intent in .java file, it gives me force close error when I click on that button.

Here's my code:

fragment.xml

<ImageButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnProgramiPL5x5"
            android:minHeight="80dp"
            android:background="@mipmap/btn_programi_pl_5x5"
            android:onClick="ProgramiPowerlifting5x5"/>

fragment.java

   package hr.app.liftme.liftmehr;


import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;


/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentPowerlifting extends Fragment {



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




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


    }

ProgramiPowerlifting5x5.java

    package hr.app.liftme.liftmehr;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class ProgramiPowerlifting5x5 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_programi_powerlifting5x5);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Intent intent = getIntent();
    }

}

Error i get is this:

01-26 23:00:44.766 14491-14491/hr.app.liftme.liftmehr E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: hr.app.liftme.liftmehr, PID: 14491
                                                                        java.lang.IllegalStateException: Could not find method ProgramiPowerlifting5x5(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageButton with id 'btnProgramiPL5x5'
                                                                            at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)
                                                                            at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
                                                                            at android.view.View.performClick(View.java:4856)
                                                                            at android.view.View$PerformClick.run(View.java:19956)
                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:211)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5389)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

I don't know what is the problem, when I link two activities I use this, name.class, but that didn't worked in this case so i used getActivity and it still does not work.

I would really appreciate help!

Thanks!

DaxHR
  • 683
  • 1
  • 11
  • 30
  • Can you show all the constructors of ProgramiPowerlifting5x5 ? – Gavriel Jan 26 '16 at 22:05
  • Android Studio is an IDE. Who cares what text editor you use? It's irrelevangt for the question. Don't you understand the difference? – Marcin Orlowski Jan 26 '16 at 22:10
  • That's not a constructor, it returns a void and it is probably not even in the class – Gavriel Jan 26 '16 at 22:11
  • Along with what Gavriel said, I would strongly advise against naming methods the same as the class name. It is very confusing for people who read the code. – OneCricketeer Jan 26 '16 at 22:19
  • Possible duplicate of [Android Button Onclick](http://stackoverflow.com/questions/10231309/android-button-onclick) – Barett Jan 27 '16 at 19:58

1 Answers1

0

The problem here is:

Could not find method ProgramiPowerlifting5x5(View) in a parent or ancestor Context

The onClick attribute refers to the parent Context (as parent Activity) so the fragment's method is not triggered and not handled by this attribute.
You have to handle the click in the Activity:

public class ProgramiPowerlifting5x5 extends AppCompatActivity {
    ...
    // change the method's name to avoid confusion with constuctor
    public void ProgramiPowerLifting(View view){
        Intent intent = new Intent(this, ProgramiPowerlifting5x5.class);
        startActivity(intent);
    }
}

Or trigger the same method into your fragment:

public class ProgramiPowerlifting5x5 extends AppCompatActivity {
    private ProgramPowerFragment fragment;
    ...
    public void ProgramiPowerLifting(View view){
        fragment.ProgramiPowerLifting(view);
    }
}

This great answer uses a callback interface which, personally, I'd suggest. Or you can easily forget the onClick xml attribute and do it dynamically with setOnClickListener().
So, let's try the easy way:

public class FragmentPowerlifting extends Fragment {

    public FragmentPowerlifting() { }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // store the layout in a view variable
        View v = inflater.inflate(R.layout.fragment_powerlifting, container, false);
        // use this view ("v") to retrieve elements on the layout by its id
        ImageButton imgButton = (ImageButton) v.findViewById(R.id.btnProgramiPL5x5);
        // set a listener to the button
        imgButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // open a new activity when onClick is triggered
                Intent intent = new Intent(getActivity(), ProgramiPowerLiftingActivity.class);
                startActivity(intent);
            }
        }
        // return the inflated view to display it
        return v;
   }
} 
Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
  • I'm sorry, but none of that helped. Still getting the same error – DaxHR Jan 26 '16 at 22:48
  • Sorry to ear that @DaxHR. But even by testing the other answers in the link posted? – Blo Jan 26 '16 at 23:04
  • No. Don't know why, why isn't the procedure same as when starting new activity from activity? Is maybe onClick responisble for error? Should I use onClickListener? – DaxHR Jan 26 '16 at 23:29
  • @DaxHR this shouldn't be the starting activity part where the problem is. Try with onClickListener yes, without xml attribute. – Blo Jan 26 '16 at 23:31
  • I'm getting errors when trying to add onClickListener in Fragment.java.. it does not recognize fingViewById – DaxHR Jan 26 '16 at 23:35
  • @DaxHR I hope it's a typo because it's fin**D**ViewById(), but in fragment you need to attach the view inflated in onCreateView like `View v = inflater.inflate...; Button btn = (Button) v.findViewById();` – Blo Jan 26 '16 at 23:38
  • I'm sorry, that was a typo. Would you like me to post whole .java files so you can correct them? – DaxHR Jan 26 '16 at 23:41
  • I uploaded all the text so if you would like to help that would be cool! – DaxHR Jan 26 '16 at 23:56
  • And me my answer @DaxHR, hope you'll get it. Don't forget to delete the button's onClick attribute if you are using the onClickListener dynamically – Blo Jan 27 '16 at 00:03
  • Thank you! Another problem. It wont recognize my .class on new Intent get activity. No typos though – DaxHR Jan 27 '16 at 00:26
  • NEVER MIND! You solved it! You are a God!!! Thank you so much man, I really apreciate it! – DaxHR Jan 27 '16 at 00:29
  • Glad to read that @DaxHR! You could [accept my answer](http://meta.stackexchange.com/a/5235/253846) to inform that your problem no longer exists. Happy coding! – Blo Jan 27 '16 at 00:33
  • Did that! Another question, I have multiple image buttons in that fragment, when I want to set onClickListener to another one, do I have to create another method or? – DaxHR Jan 27 '16 at 00:40
  • Thank you @DaxHR. Yes, you have to. But to be more convenient, you can implement the listener to the fragment, set the event on the context (fragment class) `setOnClickListener(this)` and handle all clicks in `onClick` method. Described in [this post](http://stackoverflow.com/a/25905313/2668136) and [this one](http://stackoverflow.com/a/3795472/2668136). – Blo Jan 27 '16 at 00:45
  • That didn't work, had set fragment class to abstract and then it wouldn't run because fragment can't be abstract. Weird But I've found a solution. Thank you so much for your time and help! – DaxHR Jan 27 '16 at 01:03