Mohammad Zakriya Mohammad Zakriya - 4 months ago 21
Android Question

How to open new Activity from a fragment java file by clicking the button on the fragment xml file?

I need to open a new activity by clicking the button on the fragment xml file .

I have coded a bit in my fragment java file to call the new activity by clicking the button on my fragment xml file

When i run the app, it crashes

The error am getting is in the fragment java file in the line

"public void electronics(View view)"

08-02 10:58:55.654 3519-3519/com.example.mohammadzakriya.tabhost E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mohammadzakriya.tabhost, PID: 3519
java.lang.IllegalStateException: Could not find method electronics(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button2'
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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


XML

<LinearLayout
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"
android:orientation="vertical"
tools:context=".FolderScale">

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="1"
android:textAlignment="center" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Electronics"
android:onClick="electronics"/>

</LinearLayout>


Java

public class TopfreeFragment extends Fragment {

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

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

public void electronics(View view) {
Intent intent = new Intent(getActivity(),Electronicspage.class);
startActivity(intent);
}
}

Answer

Here is another way you can use to handle click event from button:

public class TopfreeFragment extends Fragment implements View.OnClickListener{

    View view;
    private Button btn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_topfree, container, false);
        btn = (Button) view.findViewById(R.id.button2);
        btn.setOnClickListener(this);
        return view;
    }

    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.button2:
                 startActivity(new Intent(getActivity(),Electronicspage.class));
            break;

            case R.id.XXXXX:
            break;
            // more button......

        }
    }
}

This article will really helpful for you: Android: how to handle button click