0

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);
    }
}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50

3 Answers3

2

You are using electronics inside onClick and in method it is electronic not electronics

Use electronics instead of electronic

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Aditi
  • 455
  • 4
  • 13
1

Finally got the solution for the answer with some eidt's according to my code

thanks @ vrund purohit for the link How to handle button clicks using the XML onClick within Fragments .

It helped me to understand finally the code is below

// Fragment java file updated

public class TopfreeFragment extends Fragment implements View.OnClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_topfree, container, false);

        Button b = (Button) v.findViewById(R.id.button2);
        b.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button2:
                Intent intent = new Intent(getActivity(),Electronicspage.class);
                startActivity(intent);
                break;
        }
    }
}
Community
  • 1
  • 1
0

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

Community
  • 1
  • 1
xxx
  • 3,315
  • 5
  • 21
  • 40