0

Summary

I thought i could create a CheckBox inside a Fragment and control it programmatically within the MainActivity. Check Link below for a tutorial.

Fragments should be used to "modularize" an application. I thought to archieve this I could just put all the code in the MainActivity and have the UI Elements be set in the Fragment as I have very limited expierience with Java/Android.

Generally the Fragment should take care of everything it possess itself. So heres what I did:

In MainActivity.java I removed the Checkbox View and Reference.

I then created a CheckBox inside the Fragment (FragmentTest.java) like so:

public class FragmentTest extends Fragment{

    View rootView;
    CheckBox FragmentCheckbox;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        //Create a View and inflate the Fragment in this View.
        //This gives me the option to reference the Fragments Context like I did with:  rootView.findViewById
        rootView = inflater.inflate(R.layout.test_fragment, container, false);

        //Find the ID of the Checkbox in the Fragment
        FragmentCheckbox = (CheckBox) rootView.findViewById(R.id.checkBoxInFragment);
        //Set CheckBox to true
        FragmentCheckbox.setChecked(true);

        //return inflated View for the MainActivity to use
        return rootView;
    }
}

In activity_main.xml I removed the fragment. It is not needed, because in MainActivity.java //setContentView(R.layout.activity_main); is commented out.

If you want to learn more about Fragments, check this video out. I helped me understand the concept of Fragments and how to use them correctly.

Fragment Tutorial Playlist

Original Question


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://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"
    >

    <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"/>
Ederbit
  • 205
  • 1
  • 12
  • If the view is in a fragment, usually you want the fragment to control that view, not the activity. Otherwise there's no point having the fragment. – Karakuri Jan 03 '16 at 19:06

3 Answers3

0

Fragment views are not created instantly when you call replace, so you'll need to wait until it is actually created to call findViewById.

Buddy
  • 10,874
  • 5
  • 41
  • 58
0

Can you put setChecked() to the Fragment?

public class FragmentTest extends Fragment{
   CheckBox fragmentCheckbox;
   @Nullable
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.test_fragment, container, false);
       fragmentCheckbox = (CheckBox) view.findViewById(R.id.checkBoxInFragment);
       fragmentCheckbox.setChecked(false);
       return view;
   }
}

Edit

Please try UI Thread post for waiting FragmentTransaction in Activity.

new Handler().post(new Runnable () {
    public void run () {
        fragmentCheckbox = (CheckBox) findViewById(R.id.checkBoxInFragment);
        fragmentCheckbox.setChecked(false);
    }
}

If you can't. Try this.

....
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
fragmentCheckbox = (CheckBox) findViewById(R.id.checkBoxInFragment);
fragmentCheckbox.setChecked(false);
...
takahirom
  • 1,934
  • 2
  • 12
  • 21
  • Yes this works, but I have a larger piece of code that i want to split up using Fragments. For example: I am connected to a server and as soon as the connections drops i want a fragment to show up where I can connect to the Server again. Is it possible to have the Code not in the Fragments but in the Main Activity? – Ederbit Jan 03 '16 at 19:17
  • Hey, setting the Checkbox works with the Handler Method. Thank you. But would I have to put every bit of Code thats in my onCreate Method after fragmentTransaction.commit() into this Handler? – Ederbit Jan 03 '16 at 19:48
  • Handler post needs only Immediately after fragmentTransaction.commit(). Or, Can you try third `executePendingTransactions ()` ? – takahirom Jan 04 '16 at 03:03
  • No, .executePendingTransactions(); did not work for me. Only the the Handler way works. But when I have a onClick Listener, it again doesn't work, in the Handler and outside of it. Is there a way to wait for the Fragment to initialize all the Views that it has in its Layout. so that it doenst return null Pointer Exception? – Ederbit Jan 04 '16 at 11:39
  • Mmm... Please use http://stackoverflow.com/a/8122789/4339442 . And create `setChecked()` method to TestFragment. The `setChecked ()` contains handler's post setChecked. And you have to implement `processMessage()` for `checkBox.setChecked()`. – takahirom Jan 04 '16 at 11:55
0

You are trying to set checkbox inside inside activity , but checkbox is inside fragment xml . Hence if you want to set checked your checkbox . Do that inside onActivityCreated method of fragment. And create object of checkbox inside onCreateView method if a fragment. Since checkbox is directly not available to activity it is throwing null pointer exception. Hope it helps.