I have a button in my fragment
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 wanting 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 you're giving this error.
java.lang.RuntimeException: Unable to start activity ComponentInfo{makerapp.android.minhastarefas/makerapp.android.minhastarefas.MainActivity}: java.lang.NullPointerException
How do I get the event to click this button in this class?