5

I have looked around and implemented some of the things that I have found in order to get the findViewById to work inside my Fragment. However, I get either the cannot resolve method findViewById or I get "Unreachable Statement." I am not entirely sure where I am going wrong.

Here is the code for my Fragment.

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

/**
 * Created by James Singleton on 8/8/2016.
 */

public class SettingsFragment extends Fragment
{
    View myView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.settings_layout, container, false);
        return myView;

        RadioGroup radioGroup = (RadioGroup) getView().findViewById(R.id.radioGroup);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // checkedId is the RadioButton selected

                switch(checkedId) {
                    case R.id.radioButton7:
                        // switch to fragment 1
                        break;
                    case R.id.radioButton6:
                        // Fragment 2
                        break;
                }
            }
        });
    }
}

And here is my code for the layout

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:text="Use Cell Data to retrieve driving data:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:textSize="18sp"
    android:textColor="@color/cast_expanded_controller_background_color"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<RadioGroup
    android:layout_width="89dp"
    android:layout_height="69dp"
    android:weightSum="1"
    android:layout_below="@+id/textView"
    android:id="@+id/radioGroup">

    <RadioButton
        android:text="Enable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioButton7"
        android:layout_weight="1" />

    <RadioButton
        android:text="Disable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioButton6"
        android:layout_weight="1"
        android:checked="true" />
</RadioGroup>

3 Answers3

11

Change your code like this:

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


        RadioGroup radioGroup = (RadioGroup) myView .findViewById(R.id.radioGroup);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // checkedId is the RadioButton selected

                switch(checkedId) {
                    case R.id.radioButton7:
                        // switch to fragment 1
                        break;
                    case R.id.radioButton6:
                        // Fragment 2
                        break;
                }
            }
        });

        return myView;
    }
AbhayBohra
  • 2,047
  • 24
  • 36
  • This one worked. Will accept when the 7 min is up! Thank you! – James Robert Singleton Aug 12 '16 at 05:15
  • Yea trying to do a thing where I do a pop up upon starting the app asking whether or not the user wants to use WiFi or Mobile Data along with a Do Not Show Again. However, what if the user wants to change their decision? I implemented a settings button with a radio group. Now I just have to figure out how to save that change... – James Robert Singleton Aug 12 '16 at 05:23
  • get the value of radiobutton and save it in sharedpreferences .and use this value in your setting activity.For detail regarding sharedpreferences check this link--https://developer.android.com/training/basics/data-storage/shared-preferences.html – AbhayBohra Aug 12 '16 at 05:38
  • Can I do that in an onClick? Furthermore, where would I add that onClick in the above code? Since doing it after the return myView; wouldn't result in anything. – James Robert Singleton Aug 12 '16 at 05:43
  • return myView should be the last statement in your fragment class.See the comment of @Sabeer Mohammed – AbhayBohra Aug 12 '16 at 05:46
  • Hmm ok, still don't follow. But maybe tomorrow it will click haha – James Robert Singleton Aug 12 '16 at 05:52
0

Before returning a view in onCreateView(), use that view you inflated like :

view.findViewById(R.id.your_id);

So in your case it will be like :

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.settings_layout, container, false);

    RadioGroup radioGroup = (RadioGroup) myView.findViewById(R.id.radioGroup);

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected

            switch(checkedId) {
                case R.id.radioButton7:
                    // switch to fragment 1
                    break;
                case R.id.radioButton6:
                    // Fragment 2
                    break;
            }
        }
    });
    return myView;
}
Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32
0
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.settings_layout, container, false);
    RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
    return myView;
    }
Nirmit
  • 189
  • 8