简体   繁体   中英

Starting an AppCompatActivity Activity from a class that extends Fragment

I have been through a lot of posts here about starting an activity from a fragment, although none of them seem to be solving my problem. As i say, i am trying to start an activity from a class that extends a fragment. At the minute, when i remove the "android:onClick" from the xml, the button simply becomes dead and it does not let me click it. However when i put in the OnClick method to the xml i receive the following error:

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 'buttonViewDetails' at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) at android.view.View.performClick(View.java:5653) at android.view.View$PerformClick.run(View.java:22509) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6144) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

I am not quite sure what this means. Here is the code that i have tried:

Button XML inside the class extending fragment:

 <Button
    android:id="@+id/buttonViewDetails"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:background="@mipmap/info"
    android:layout_marginLeft="250dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="14dp"
    android:layout_marginBottom="14dp" />

Java Code for the class that extends fragment:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View myInflatedView = inflater.inflate(R.layout.line_details, container, false);

    Bundle extras = getArguments();
    if (extras != null) {
        String linetype = extras.getString("LineType");
        TextView LT = (TextView) getActivity().findViewById(R.id.textViewLineType);
        LT.setText(linetype);

        Button InfoButton = (Button) getActivity().findViewById(R.id.buttonViewDetails);
        InfoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), VerifyLine.class);
                startActivity(intent);
            }
        });

    }

    return myInflatedView;
}

Would anyone be able to help me out?

Thanks!

You are inflating a view myInflatedView and doing findViewById of that view using activity reference.So ust change that getActivity and use myInflatedView to find the Views.Also remove onClick for the button from xml.

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View myInflatedView = inflater.inflate(R.layout.line_details, container, false);

        TextView LT = (TextView) myInflatedView.findViewById(R.id.textViewLineType);
        Button InfoButton = (Button) myInflatedView.findViewById(R.id.buttonViewDetails);
        InfoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), VerifyLine.class);
                startActivity(intent);
            }
        });

        Bundle extras = getArguments();
        if (extras != null) {
            String linetype = extras.getString("LineType");
            LT.setText(linetype);
        }

        return myInflatedView;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM