dimanche 3 janvier 2016

Null P Exception while setting CheckBox with Android Fragments

today I tried implementing Fragments in my Android App. I followed various tutorials and just about everything works, setting my Fragment as main Layout, also finding the Checkbox by ID, but trying to set my Checkbox to false will return a Null Pointer Exception. I looked at different Questions, but I couldn't figure out an Answer. All Code is below. Thank you for your time.

Note that when I am removing this line, it displays the CheckBox normally. But I can't set it in my Main Activity File.

fragmentCheckbox.setChecked(false);

The Error I'm getting:

Attempt to invoke virtual method 'void android.widget.CheckBox.setChecked(boolean)' on a null object reference

Full Log

FATAL EXCEPTION: main
    Process: *...............*.androidfragments, PID: 18658
    java.lang.RuntimeException: Unable to start activity ComponentInfo{*...............*.androidfragments/*...............*.androidfragments.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setChecked(boolean)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
    at android.app.ActivityThread.access$800(ActivityThread.java:148)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5312)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setChecked(boolean)' on a null object reference
    at *...............*.androidfragments.MainActivity.onCreate(MainActivity.java:27)
    at android.app.Activity.performCreate(Activity.java:5953)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388) 
    at android.app.ActivityThread.access$800(ActivityThread.java:148) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5312) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)


Main Activity: Here i just set my Fragment as my Main Layout

public class MainActivity extends AppCompatActivity {

CheckBox fragmentCheckbox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    FragmentTest fragmentTest = new FragmentTest();
    fragmentTransaction.replace(android.R.id.content, fragmentTest);

    fragmentTransaction.commit();

    fragmentCheckbox = (CheckBox) findViewById(R.id.checkBoxInFragment);
    fragmentCheckbox.setChecked(false);
  }
}

FragmentTest: Returning my Fragment

public class FragmentTest extends Fragment{
   @Nullable
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       return inflater.inflate(R.layout.test_fragment, container, false);
   }
}

My test_fragment XML File: Basically giving my Checkbox an ID

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    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"
    >

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New CheckBox"
        android:id="@+id/checkBoxInFragment"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

And my Main XML File

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/test_fragment"/>




Aucun commentaire:

Enregistrer un commentaire