1

I am very new to Android Development and have been trying to get this to work for a while now. The basic structure of my Activity is two fragments between which you can switch by swiping. Both fragments have different layouts and contain different buttons and I am in the process of getting the buttons to work.

When I try to set the OnClickListener for any button in onCreate(), the App no longer starts. Here is my onCreate() Code:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    imgButton = (ImageButton)findViewById(R.id.imageButton);
    OnClickListener btnListener = new OnClickListener() {
        @Override
        public void onClick(android.view.View view) {
            //Do Something

        }
    };
    imgButton.setOnClickListener(btnListener);

}

The App Crashes with a null object reference, saying that imgButton's value is null. I certainly am doing this wrong, but I wasn't able to find the correct way of setting up button listeners for two fragments. Can anybody point me in the right direction?

EDIT: activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.dansoft.daily_surprise.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>

fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/background"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.dansoft.daily_surprise.MainActivity$PlaceholderFragment">



    <ImageButton

        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="10dp"
        android:src="@drawable/box_1"
        android:background="@android:color/transparent"
        />




    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/img_1"
        />


</RelativeLayout>

fragment_sec.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/background"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.dansoft.daily_surprise.MainActivity$PlaceholderFragment">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/img_2"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="290dp"
        android:layout_height="220dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="100dp"
        android:background="@android:color/transparent"
        />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="151dp"
        android:text="22"
        android:textColor="@android:color/black"
        android:textSize="200sp" />


</RelativeLayout>
Evorionos
  • 13
  • 3

1 Answers1

0

Your should track button clicks on your fragments, not on your activities. Your fragments are not laid-out after onCreate. So your findViewById returns null.

Move your listener code to your fragments.

Valentin Baryshev
  • 2,195
  • 3
  • 15
  • 24