Farid Khan
Farid Khan

Reputation: 37

FindviewbyId method is not working in Fragments

I am having the fragments Home Message and setting. I am replacing my message fragment to a Fat calculator that have and edittextview and a spinner. But having problem in findviewbyid method I tried "rootview" but not solved.

import android.os.Bundle; 
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;



public class MessageFragment extends Fragment implements AdapterView.OnItemSelectedListener {

Spinner spinnerFat;
EditText editTextAns;
public MessageFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_message, container, false);

Here I got the error while reading the values:

    editTextAns = (EditText) findViewById(R.id.editTextAns);

    spinnerFat = (Spinner) findViewById(R.id.spinnerFat);

above two lines and having error also that it cannot create Resources from values folder and having error on this keyword.

    ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.Fat, R.layout.support_simple_spinner_dropdown_item);

-- as i am using Fat named array from Values

    spinnerFat.setAdapter(adapter);
    spinnerFat.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {

    switch (i) {
        case 0:
        {
            //please selecet an item
            break;
        }
        case 1:

        {
            //ghee
            editTextAns.setText("65% Saturated Fat, 32% of Mono-unsaturated Fat");
            break;
        }
        case 2:

        {
            //Banaspati
            editTextAns.setText("24% Saturated Fat, 19% of Mono-unsaturated Fat");
            break;
        }
        case 3:

        {
            //Red Palm Oil
            editTextAns.setText("50% Saturated Fat, 40% of Mono-unsaturated Fat");
            break;
        }
        case 4:

        {
            //Palm Oil
            editTextAns.setText("45% Saturated Fat, 44% of Mono-unsaturated Fat");
            break;
        }
        case 5:

        {
            //coconut oil
            editTextAns.setText("90% Saturated Fat, 7% of Mono-unsaturated Fat");
            break;
        }
        case 6:

        {
            //Palm kernal oil
            editTextAns.setText("82% Saturated Fat, 15% of Mono-unsaturated Fat");
            break;
        }
        case 7:

        {
            //olive oil
            editTextAns.setText("13% Saturated Fat, 76% of Mono-unsaturated Fat");
            break;
        }
        case 8:

        {
            //groundunt oil
            editTextAns.setText("24% Saturated Fat, 50% of Mono-unsaturated Fat");
            break;
        }
        case 9:

        {
            //mustard oil
            editTextAns.setText("8% Saturated Fat, 70% of Mono-unsaturated Fat");
            break;
        }
        case 10:

        {
            //sesame oil
            editTextAns.setText("15% Saturated Fat, 42% of Mono-unsaturated Fat");
            break;
        }
        case 11:

        {
            //rice bran oil
            editTextAns.setText("22% Saturated Fat, 41% of Mono-unsaturated Fat");
            break;
        }
        case 12:

        {
            //cotton seed oil
            editTextAns.setText("22% Saturated Fat, 25% of Mono-unsaturated Fat");
            break;
        }
        case 13:

        {
            //corn oil
            editTextAns.setText("12% Saturated Fat, 32% of Mono-unsaturated Fat");
            break;
        }
        case 14:

        {
            //sunflowre
            editTextAns.setText("13% Saturated Fat, 27% of Mono-unsaturated Fat");
            break;
        }

        case 15:

        {
            //saffron oil
            editTextAns.setText("13% Saturated Fat, 17% of Mono-unsaturated Fat");
            break;
        }

    }

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
 }

Upvotes: 1

Views: 501

Answers (4)

Ameya Kulkarni
Ameya Kulkarni

Reputation: 198

You need to do getView().findViewById() instead of only findviewbyid() And for array adapter you need to pass getActivity() in place of 'this'

editTextAns = (EditText)getView().findViewById(R.id.editTextAns); 
spinnerFat = (Spinner)getView().findViewById(R.id.spinnerFat);


ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Fat, R.layout.support_simple_spinner_dropdown_item);

Upvotes: 0

Prakash Gajera
Prakash Gajera

Reputation: 563

try this:

in onActivityCreated method of fragment

editTextAns = (EditText) getActivity().findViewById(R.id.editTextAns);

spinnerFat = (Spinner) getActivity().findViewById(R.id.spinnerFat);

Upvotes: 0

dieter_h
dieter_h

Reputation: 2727

You need to inflate the Fragment's view and call findViewById() on the View it returns:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_message, container, false);

    editTextAns = (EditText) v.findViewById(R.id.editTextAns);

    spinnerFat = (Spinner) v.findViewById(R.id.spinnerFat);
    return v;
}

ArrayAdapter:

ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Fat, R.layout.support_simple_spinner_dropdown_item);

Upvotes: 3

ELITE
ELITE

Reputation: 5940

in onCreateView method of fragment, get View like below

View view = inflater.inflate(R.layout.fragment_message, container, false);

and then get elements using

editTextAns = (EditText) view.findViewById(R.id.editTextAns);
spinnerFat = (Spinner) view.findViewById(R.id.spinnerFat);

It'll work.

Upvotes: 0

Related Questions