FindviewbyId方法无法在Fragments

时间:2016-03-13 10:47:13

标签: android xml

我有片段主页消息和设置。 我正在将我的消息片段替换为具有和edittextview以及微调器的Fat计算器。但是在findviewbyid方法中遇到问题我尝试了“rootview”但没有解决。

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);

这里我在读取值时遇到错误:

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

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

以上两行 并且还有错误,它无法从值文件夹创建资源并在this关键字上出错。

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

- 因为我正在使用来自值的Fat命名数组

    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) {

}
 }

4 个答案:

答案 0 :(得分:3)

您需要对片段视图进行充气,并在其返回的findViewById()上调用View

@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);

答案 1 :(得分:0)

onCreateView片段方法中,获取View,如下所示

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

然后使用

获取元素
editTextAns = (EditText) view.findViewById(R.id.editTextAns);
spinnerFat = (Spinner) view.findViewById(R.id.spinnerFat);

它会起作用。

答案 2 :(得分:0)

你需要做getView()。findViewById()而不是findviewbyid() 对于数组适配器,您需要传递getActivity()来代替'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);

答案 3 :(得分:0)

试试这个:

在片段

的onActivityCreated方法中
editTextAns = (EditText) getActivity().findViewById(R.id.editTextAns);

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