Ajinkya
Ajinkya

Reputation: 147

Getting A NullPointerException in Checkbox

I get the following error while running a simple CheckBoxdemo App:

06-23 20:06:17.370: E/AndroidRuntime(10927): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.e8.checkboxdemo/com.e8.checkboxdemo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference

Following is the java file:

package com.e8.checkboxdemo;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;


public class MainActivity extends Activity implements OnCheckedChangeListener {
   CheckBox cb; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        cb=(CheckBox)findViewById(R.id.checkBox1);
        cb.setOnCheckedChangeListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if(cb.isChecked())
            cb.setText("Checked");
        else
            cb.setText("UnChecked");
    }

}

And following is the fragment_main.xml file :

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.e8.checkboxdemo.MainActivity$PlaceholderFragment" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="CheckBox" />

</RelativeLayout>

Am I missing something here ? Also I am using ART runtime.

Upvotes: 0

Views: 2154

Answers (2)

TmKVU
TmKVU

Reputation: 3000

The problem is you are trying to use the checkbox in you MainActivity with the content view set to activity_main.xml. You can only use findViewById(); for Viewslocated in the xml file you have set as content view.

You should use the checkbox in the Fragment instead.

Upvotes: 1

Zoran
Zoran

Reputation: 1494

You should work with checkbox inside fragment's onCreateView and not in activity's onCreate.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    CheckBox cb = (CheckBox) rootview.findViewById(R.id.checkBox1);
    cb.setOnCheckedChangeListener(this);
    return rootView;
}

and put onCheckedChanged in fragment too.

Upvotes: 1

Related Questions