0
I have a button in my Ragment
fragment_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="16dp"
tools:context="makerapp.android.minhastarefas.MainActivity.ListItensFragment">
<!-- Exibe nome da lista atual -->
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="1">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="54dp">
        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/stringVolta"
            android:id="@+id/anterior" />
    </RelativeLayout>
</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:background="@color/md_grey_300"/>
<!-- Lista de itens -->
<makerapp.android.minhastarefas.ui.DynamicListView
    android:id="@+id/itens_listview"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:divider="@color/md_grey_300"
    android:dividerHeight="1dp"
    android:choiceMode="singleChoice"
    android:headerDividersEnabled="true"/>
<!-- Layout a ser exibido quando a lista estiver vazia -->
<FrameLayout
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:drawablePadding="16dp"
        android:drawableTop="@drawable/ic_empty_list"
        android:fontFamily="sans-serif-light"
        android:gravity="center"
        android:text="@string/empty_list_text"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@color/secondary_text_color"
        android:textSize="20dp"
        android:textStyle="italic" />
</FrameLayout>
<!-- Layout destinado a publicidade-->
<FrameLayout
    android:id="@+id/ad_framelayout"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/ad_linear_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </LinearLayout>
</FrameLayout>
and want to access this button in the Mainactivity class
Mainactivity
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btAnterior = (Button)findViewById(R.id.anterior);
    btAnterior.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("TAG","ta aki");
        }
    });
}
This way there is giving this mistake.
java.lang.RuntimeException: Unable to start activity ComponentInfo{makerapp.android.minhastarefas/makerapp.android.minhastarefas.MainActivity}: java.lang.NullPointerException
How do I catch the event by clicking this button in this class?
You can catch this event inside
Fragment, after inflating the layout, and notify yourActivity.– Wakim
i even managed to use inside the Fragment more I needed a method of this class ai when I tried to use so Mainactivity main = new Mainactivity(); main.metodo() gave error because some variables used in this method are started in oncreate
– Ilgner de Oliveira
So it won’t work, never install a
Activity, Android already does this and manages the life cycle. Use the Observer standard to notifyActivity, I’ll create an answer.– Wakim
ta good ........
– Ilgner de Oliveira