"错误:无法解析符号findViewById"

时间:2018-02-13 20:22:20

标签: android

我在001938 Note.pdf遇到问题,但我无法找到问题所在。

这是我的FirstFragment类代码:

findViewById

}

同时在线显示错误

public class VehicleInformation extends Fragment implements AdapterView.OnItemSelectedListener {

    private TextView text_vehicle_no, text_chassis_no;
    private TextView text_company_name,text_insurance_id;
    RadioGroup radioGroup;
    RadioButton radioButton;
    private SpinnerAdapter text_vehicle_class;

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

        radioGroup = (RadioGroup) rootView.findViewById(R.id.insuranceYesNo);

        text_vehicle_no = (TextView) rootView.findViewById(R.id.vehicle_no);
        text_chassis_no = (TextView) rootView.findViewById(R.id.chassis_no);


        Spinner spinner_vehicle_class = (Spinner) rootView.findViewById(R.id.vehicle_class);
        Spinner spinner_vehicle_company = (Spinner) rootView.findViewById(R.id.vehicle_company);
        Spinner spinner_vehicle_model = (Spinner) rootView.findViewById(R.id.vehicle_model);
        Spinner spinner_vehicle_fuel = (Spinner) rootView.findViewById(R.id.vehicle_fuel);
        Spinner spinner_vehicle_color = (Spinner) rootView.findViewById(R.id.vehicle_color);
        Spinner spinner_vehicle_types = (Spinner) rootView.findViewById(R.id.vehicle_types);

        ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.vehicle_class_array, android.R.layout.simple_spinner_item);
        ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,R.array.vehicle_company_array, android.R.layout.simple_spinner_item);
        ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this,R.array.vehicle_model_array, android.R.layout.simple_spinner_item);
        ArrayAdapter adapter3 = ArrayAdapter.createFromResource(this,R.array.vehicle_fuel_array, android.R.layout.simple_spinner_item);
        ArrayAdapter adapter4 = ArrayAdapter.createFromResource(this,R.array.vehicle_color_array, android.R.layout.simple_spinner_item);
        ArrayAdapter adapter7 = ArrayAdapter.createFromResource(this,R.array.vehicle_type_array, android.R.layout.simple_spinner_item);

        //specify the layout to use when the list of choices appears

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter7.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //aaply the adapter to the spinner
        spinner_vehicle_class.setAdapter(adapter);
        spinner_vehicle_company.setAdapter(adapter1);
        spinner_vehicle_model.setAdapter(adapter2);
        spinner_vehicle_fuel.setAdapter(adapter3);
        spinner_vehicle_color.setAdapter(adapter4);
        spinner_vehicle_types.setAdapter(adapter7);


        spinner_vehicle_class.setOnItemSelectedListener(this);
        spinner_vehicle_company.setOnItemSelectedListener(this);
        spinner_vehicle_model.setOnItemSelectedListener(this);
        spinner_vehicle_fuel.setOnItemSelectedListener(this);
        spinner_vehicle_color.setOnItemSelectedListener(this);
        spinner_vehicle_types.setOnItemSelectedListener(this);


    }

    public void onStart(){
        super.onStart();

        EditText datePicker=(EditText) findViewById(R.id.datePicker);
        datePicker.setOnFocusChangeListener(new View.OnFocusChangeListener(){
            public void onFocusChange(View view, boolean hasfocus){
                if(hasfocus){
                    DateDialog dialog=new DateDialog(view);
                    android.support.v4.app.FragmentTransaction ft =getFragmentManager().beginTransaction();
                    dialog.show(ft, "DatePicker");

                }
            }
        });
    }

    public void radiobtn(View view) {
        int radiobtnselect = radioGroup.getCheckedRadioButtonId();
        radioButton = (RadioButton) view.findViewById(radiobtnselect);
        Toast.makeText(VehicleInformation.this, radioButton.getText(),         Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
       String sSelected=adapterView.getItemAtPosition(i).toString();
        Toast.makeText(VehicleInformation.this,sSelected,Toast.LENGTH_SHORT).show();
    }

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

    }

说错误的第一个参数类型

还显示错误在线Toast messsage说无法解析方法maketext

1 个答案:

答案 0 :(得分:0)

Activity类不同,Fragment没有内置findViewById()方法。这意味着您需要使用View.findViewById(),除了onStart()方法之外,您已经在使用了EditText datePicker=(EditText) findViewById(R.id.datePicker); 。在这里,你可以改变这一行:

EditText datePicker=(EditText) getView().findViewById(R.id.datePicker);

到此:

Activity

至于你的适配器和toast问题,问题是(再次,与Fragment不同),Context类不是getContext()的子类,所以你需要传递一个不同的第一个论点尝试传递VehicleInformation.this而不是{{1}}。