点击时片段活动崩溃

时间:2016-09-25 01:32:44

标签: java android navigation-drawer

当我尝试在我的应用程序中打开“测试”活动时,我遇到了崩溃,我不明白为什么......

有关信息:我的应用程序处于活动状态,但现在我正在转换片段(导航抽屉活动)。

package ssip.ssip;

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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class test extends Fragment{

    View rootview;

    private Button btn_search;

    ArrayAdapter adapterSpinner_1,adapterSpinner_2;
    Spinner spin1,spin2;
    String spn_1_Selected_item;
    String[] spinn_marque = {"Veuillez choisir une centrale","ACTIVA 128"};
    String[] spinn_code = {"Veuillez choisir un niveau","NIVEAU 3","NIVEAU 4"};
    TextView txtResult;

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

        adapterSpinner_1=new ArrayAdapter(getActivity().getApplication(),android.R.layout.simple_spinner_dropdown_item,spinn_marque);
        spin1.setAdapter(adapterSpinner_1);
        spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                spn_1_Selected_item = parent.getItemAtPosition(position).toString();

                if (spn_1_Selected_item.equals("Veuillez choisir une centrale"))
                    txtResult.setText("");

                if (spn_1_Selected_item.equals("ACTIVA 128")) {
                    String code1[] = {spinn_code[0], spinn_code[2], spinn_code[3]};
                    adapterSpinner_2 = new ArrayAdapter(getActivity().getApplication(), android.R.layout.simple_spinner_dropdown_item, code1);
                    spin2.setAdapter(adapterSpinner_2);
                    spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            String codeSelected = parent.getItemAtPosition(position).toString();
                            if (codeSelected.equals("Veuillez choisir un niveau"))
                                txtResult.setText("");
                            if (codeSelected.equals("NIVEAU 3"))
                                btn_search.setOnClickListener(new View.OnClickListener() {
                                    public void onClick(View v) {
                                        txtResult.setText("7677");
                                    }
                                });
                            if (codeSelected.equals("NIVEAU 4"))
                                btn_search.setOnClickListener(new View.OnClickListener() {
                                    public void onClick(View v) {
                                        txtResult.setText("7780");
                                    }
                                });
                        }
                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                        }
                    });
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        return rootview;
    }
}

1 个答案:

答案 0 :(得分:1)

您正在使用TextView并且尚未对其进行初始化

TextView = (TextView) rootView.findViewById(R.id.your-id);

而不是使用getActivity.getApplication()而只使用getActivity();