0

I tried many solutions from stackoverflow but still not able to find a way through. I am trying to toast from a fragment of a Tabbed Activity but it looks like the OnClickListener is not working. Please help me out. Here is my code:

Button b;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View ph=inflater.inflate(R.layout.fragment_history, container, false);
    b=(Button)ph.findViewById(R.id.hb);
    b.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            Log.d("test", "test");
            Toast.makeText(getActivity().getApplicationContext(), "HISTORY TAB", Toast.LENGTH_LONG).show();
        }
    });
    return ph;
}

The fragment_history layout code is:

<RelativeLayout 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="molecule.tabfinal.History">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="TAPPAL HISTORY TAB"/>
<Button
    android:id="@+id/hb"
    android:layout_width="75dp"
    android:layout_height="50dp"
    android:text="HISTORY"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="139dp"/>
</RelativeLayout>
Rahul Kumar
  • 99
  • 3
  • 10
  • Use `getActivity()` instead of `getActivity().getApplicationContext()` and getting `Log.d("test", "test");` in logcat? – ρяσѕρєя K Sep 06 '15 at 21:38
  • I have tried using getActivity() but it doesn't works. Yes I was checking whether the OnClickListener is working or not. I guess its not working because there is nothing in the logcat. – Rahul Kumar Sep 06 '15 at 21:41
  • Ok attach debugger in `onCreateView` method then check method is executing or not – ρяσѕρєя K Sep 06 '15 at 21:45
  • i am getting this in the log cat: java.lang.NullPointerException: Attempt to read from field 'android.view.HardwareRenderer android.view.View$AttachInfo.mHardwareRenderer' on a null object reference at android.view.WindowManagerGlobal.dumpGfxInfo(WindowManagerGlobal.java:476) at android.app.ActivityThread$ApplicationThread.dumpGfxInfo(ActivityThread.java:1100) at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:550) at android.os.Binder.execTransact(Binder.java:446) – Rahul Kumar Sep 06 '15 at 21:52
  • post your `fragment_history` xml layout – Rod_Algonquin Sep 06 '15 at 21:52
  • @Rod_Algonquin: have added the code you asked for. Please have a look. – Rahul Kumar Sep 06 '15 at 21:58
  • @RahulKumar for now try to set the width and height of your `textview` to `wrap_content` and set the button `android:clickable="true"` – Rod_Algonquin Sep 06 '15 at 22:02
  • @Rod_Algonquin did what you said, nothing changed. – Rahul Kumar Sep 06 '15 at 22:07

0 Answers0