Android:无法找到NullPointerException的原因

时间:2016-04-04 05:50:31

标签: android nullpointerexception logcat

我正在使用android中的一个表单,我通过选择另一个下拉列表的值来渲染一个下拉列表的值,该下拉列表位于当前下拉列表的顶部。 所以在这里,我从大学下拉列表中选择一所大学,然后选择我正在渲染机构的价值并将其添加到机构下拉​​列表中,然后我从机构下拉列表中选择一个机构。 直到这里一切都很好,但在那之后,如果我再次改变大学,那么应用程序正在崩溃。 问题是,在logcat中它显示NullPointerException,但它没有显示崩溃或导致崩溃的行号。

logcat的:

FATAL EXCEPTION: main
java.lang.NullPointerException
    at android.widget.Spinner.makeAndAddView(Spinner.java:548)
    at android.widget.Spinner.layout(Spinner.java:496)
    at android.widget.Spinner.onLayout(Spinner.java:460)
    at android.view.View.layout(View.java:14243)
    at android.view.ViewGroup.layout(ViewGroup.java:4490)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
    at android.view.View.layout(View.java:14243)
    at android.view.ViewGroup.layout(ViewGroup.java:4490)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.widget.ScrollView.onLayout(ScrollView.java:1470)
    at android.view.View.layout(View.java:14243)
    at android.view.ViewGroup.layout(ViewGroup.java:4490)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14243)
    at android.view.ViewGroup.layout(ViewGroup.java:4490)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
    at android.view.View.layout(View.java:14243)
    at android.view.ViewGroup.layout(ViewGroup.java:4490)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14243)
    at android.view.ViewGroup.layout(ViewGroup.java:4490)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
    at android.view.View.layout(View.java:14243)
    at android.view.ViewGroup.layout(ViewGroup.java:4490)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14243)
    at android.view.ViewGroup.layout(ViewGroup.java:4490)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2230)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1994)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1181)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4942)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
    at android.view.Choreographer.doCallbacks(Choreographer.java:579)
    at android.view.Choreographer.doFrame(Choreographer.java:548)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
    at android.os.Handler.handleCallback(Handler.java:800)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5370)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)

EducationalSchoolCollege.java

public class EducationalSchoolCollege extends AppCompatActivity {


    private RadioGroup rg__educational_type;
    private RadioButton radioeduButton;
    Spinner university,instituition,degree,stream,tenureType,tenureLevel,cycleSpinner;
    String student_uuid,student_name,email_id,selectedTypeIs="";

    Map<String,String> educationSubmitMapData = new HashMap<>();
    Map<String,String> universityListMap = new HashMap<>();
    Map<String,String> instituteListMap = new HashMap<>();


    List<String> universityList = new ArrayList<>();
    List<String> instituteList = new ArrayList<>();
    List<String> degreeList = new ArrayList<>();
    List<String> streamList = new ArrayList<>();
    List<String> tenureTypeList = new ArrayList<>();
    List<String> tenureLevelList = new ArrayList<>();
    List<String> cycleList = new ArrayList<>();

    List<String> universityNameList = new ArrayList<>();
    List<String> instituteNameList = new ArrayList<>();
    List<String> degreeNameList = new ArrayList<>();
    List<String> streamNameList = new ArrayList<>();
    List<String> tenureNameList = new ArrayList<>();
    List<String> tenureLevelNameList = new ArrayList<>();
    List<String> cycleNameList = new ArrayList<>();


    String university_uuid,institute_uuid,degree_uuid,stream_uuid,tenure_uuid,tenure_level_uuid,cycle_uuid,degree_name;
    Button submitEducationalBtn;
    SharedPreferences afterClassPref;
    AfterClassApp controller;
    public ProgressDialog progdialog;
    TextView clickHereId;
    String fromProfile = "",genreic_user="";
    //avinash
   // TextView textView4;
    TextView degreeText,streamText,tenureText,cycleText;
    EditText edt_instituition_other;
    boolean selectedIsOther=false;
    boolean isCheckedRadio = false;
    //avinash

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.educational_school_college);

        controller = (AfterClassApp) getApplicationContext();
        afterClassPref = getApplicationContext().getSharedPreferences(Constants.SHARED_PREF_NAME, Context.MODE_PRIVATE);
        student_uuid = afterClassPref.getString("studentUUID", null);
        student_name = afterClassPref.getString("studentName", null);
        email_id = afterClassPref.getString("studentEmail", null);

        university = (Spinner) findViewById(R.id.university);
        instituition = (Spinner) findViewById(R.id.instituition);
        degree = (Spinner) findViewById(R.id.degree);
        stream = (Spinner) findViewById(R.id.stream);
        tenureType = (Spinner) findViewById(R.id.tenure);
        tenureLevel = (Spinner) findViewById(R.id.tenureLevel);
        cycleSpinner = (Spinner) findViewById(R.id.cycle);
        submitEducationalBtn = (Button) findViewById(R.id.submitEducationalBtn);
        clickHereId = (TextView) findViewById(R.id.clickHereId);

        //avinash
        edt_instituition_other = (EditText)findViewById(R.id.edt_instituition_other);
        degreeText=(TextView)findViewById(R.id.degreeText);
        streamText=(TextView)findViewById(R.id.streamText);
        tenureText=(TextView)findViewById(R.id.tenureText);
        cycleText=(TextView)findViewById(R.id.cycleText);

       // textView4 = (TextView)findViewById(R.id.textView4);
        rg__educational_type = (RadioGroup) findViewById(R.id.rg__educational_type);

        clickHereId.setVisibility(View.GONE);

        if (Build.VERSION.SDK_INT < 21) {
            rg__educational_type.setPadding(3,3,3,3);
        }

        Bundle extras = getIntent().getExtras();

        if (extras != null) {
            if (extras.containsKey("from_profile")){
                fromProfile = extras.getString("from_profile");
            }
            if (extras.containsKey("genreic_user")){
                genreic_user = extras.getString("genreic_user");
            }

        }

        //G Analytics
        if (!Constants.IS_EMAIL_EXIT) {
            Tracker tracker = ((AfterClassApp) getApplication()).getTracker(AfterClassApp.TrackerName.APP_TRACKER);
            tracker.setScreenName("MyAfterClass EducationalSchoolCollege");
            tracker.send(new HitBuilders.AppViewBuilder().build());
        }

        rg__educational_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {


                int selectedId = rg__educational_type.getCheckedRadioButtonId();

                radioeduButton = (RadioButton) findViewById(selectedId);
                instituition.setAdapter(null);
                degree.setAdapter(null);
                stream.setAdapter(null);
                tenureType.setAdapter(null);
                tenureLevel.setAdapter(null);
                cycleSpinner.setAdapter(null);
                if(radioeduButton.getText().toString().equals("School")){
                    edt_instituition_other.setVisibility(View.GONE);
                    isCheckedRadio = true;
                    clickHereId.setVisibility(View.GONE);
                    setAdapter(university,universityNameList);
                    universityList.clear();
                    instituteList.clear();
                    degreeList.clear();
                    streamList.clear();
                    tenureTypeList.clear();
                    tenureLevelList.clear();
                    cycleList.clear();

                    universityNameList.clear();
                    instituteNameList.clear();
                    degreeNameList.clear();
                    streamNameList.clear();
                    tenureNameList.clear();
                    tenureLevelNameList.clear();
                    cycleNameList.clear();
                    firstTimeSchoolUserData("university",null,null,null,null);
                    selectedTypeIs = "board";
                    degreeText.setVisibility(View.GONE);
                    degree.setVisibility(View.GONE);
                    stream.setVisibility(View.GONE);
                    tenureType.setVisibility(View.GONE);
                    cycleSpinner.setVisibility(View.GONE);
                    streamText.setVisibility(View.GONE);
                    tenureText.setVisibility(View.GONE);
                    cycleText.setVisibility(View.GONE);


                }
                if(radioeduButton.getText().toString().equals("College")){
                    edt_instituition_other.setVisibility(View.GONE);
                    isCheckedRadio = true;
                    if ("yes".equalsIgnoreCase(genreic_user)){
                        clickHereId.setVisibility(View.GONE);
                    }else{
                        clickHereId.setVisibility(View.VISIBLE);
                    }

                    setAdapter(university,universityNameList);
                    universityList.clear();
                    instituteList.clear();
                    degreeList.clear();
                    streamList.clear();
                    tenureTypeList.clear();
                    tenureLevelList.clear();
                    cycleList.clear();

                    universityNameList.clear();
                    instituteNameList.clear();
                    degreeNameList.clear();
                    streamNameList.clear();
                    tenureNameList.clear();
                    tenureLevelNameList.clear();
                    cycleNameList.clear();
                    firstTimeUserData("university", null, null,null,null,null,null,null);
                    selectedTypeIs = "college";
                    degreeText.setVisibility(View.VISIBLE);
                    degree.setVisibility(View.VISIBLE);
                    stream.setVisibility(View.VISIBLE);
                    tenureType.setVisibility(View.VISIBLE);
                    cycleSpinner.setVisibility(View.VISIBLE);
                    streamText.setVisibility(View.VISIBLE);
                    tenureText.setVisibility(View.VISIBLE);
                    cycleText.setVisibility(View.VISIBLE);

                }
            }
        });
        // avinash



        clickHereId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EducationalSchoolCollege.this, OtherFirstTimeEducational.class);
                startActivity(intent);
            }
        });

//        setAdapter(university,universityList);
//        firstTimeUserData("university",null,null,null,null,null,null,null);

        university.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
                String seluniversity = (String) university.getSelectedItem();
                instituteList.clear();;
                degreeList.clear();
                streamList.clear();
                tenureTypeList.clear();
                tenureLevelList.clear();
                cycleList.clear();

                instituteNameList.clear();;
                degreeNameList.clear();
                streamNameList.clear();
                tenureNameList.clear();
                tenureLevelNameList.clear();
                cycleNameList.clear();
                Log.d("UNI ","seluniversity "+ seluniversity+ " pos:: "+position);
//                clear on proper selecting



                //                clear on proper selecting

                if ("board".equalsIgnoreCase(selectedTypeIs)){
                    //Todo
                    university_uuid = universityListMap.get(seluniversity);
                    firstTimeSchoolUserData("institute",university_uuid,null,null,null);
                }else{
                    if (!"Select".equalsIgnoreCase(seluniversity)){
                        university_uuid = universityList.get(position-1);
                        educationSubmitMapData.put("university_uuid",university_uuid);
                        Log.d("UNI ", "university_uuid "+university_uuid);
//                    firstTimeUserInstitutionData("institute",university_uuid);
                        firstTimeUserData("institute",university_uuid,null,null,null,null,null,null);
                    }else{
                        university_uuid = null;
                    }
                }

            }

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

            }
        });

        instituition.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
                String selInstitute = (String) instituition.getSelectedItem();
                degreeList.clear();
                streamList.clear();
                tenureTypeList.clear();
                tenureLevelList.clear();
                cycleList.clear();

                degreeNameList.clear();
                streamNameList.clear();
                tenureNameList.clear();
                tenureLevelNameList.clear();
                cycleNameList.clear();

                if ("board".equalsIgnoreCase(selectedTypeIs)) {
                    //Todo
                    institute_uuid = instituteListMap.get(selInstitute);
                    if ("other".equalsIgnoreCase(selInstitute)){
                        // TODO: Hide edittext box and show here
                        edt_instituition_other.setVisibility(View.VISIBLE);
                        selectedIsOther=true;
                    }else{
                        selectedIsOther=false;
                        edt_instituition_other.setVisibility(View.GONE);
                    }
                    tenureLevelList.clear();
                    firstTimeSchoolUserData("tenure_level", university_uuid, institute_uuid, null, null);
                } else {
                    if (!"Select".equalsIgnoreCase(selInstitute)) {
//            int position = university.getSelectedItemPosition();
                        if(instituteList.size()==0){
                            return;
                        }
                        edt_instituition_other.setVisibility(View.GONE);
                        institute_uuid = instituteList.get(position - 1);
                        educationSubmitMapData.put("institute_uuid", institute_uuid);
//                    firstTimeUserDegreeData("degree", university_uuid, institute_uuid);
                        firstTimeUserData("degree", university_uuid, institute_uuid, null, null, null, null, null);
                    } else {
                        institute_uuid = null;
                    }
                }

            }

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

            }
        });

        degree.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
                String selInstitute = (String) degree.getSelectedItem();
                streamList.clear();
                tenureTypeList.clear();
                tenureLevelList.clear();
                cycleList.clear();

                streamNameList.clear();
                tenureNameList.clear();
                tenureLevelNameList.clear();
                cycleNameList.clear();
                if (!"Select".equalsIgnoreCase(selInstitute)){
//            int position = university.getSelectedItemPosition();
                    if(degreeList.size()==0){
                        return;
                    }
                    degree_uuid = degreeList.get(position-1);
                    degree_name = degreeNameList.get(position-1);
//                    Log.d("degree_name",""+degree_name);
                    educationSubmitMapData.put("degree_uuid",degree_uuid);
//                    firstTimeUserDegreeData("degree", university_uuid, institute_uuid);
                    firstTimeUserData("stream",university_uuid,institute_uuid,degree_uuid,null,null,null,null);
                }else{
                    degree_uuid = null;
                }
            }

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

            }
        });

        stream.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
                String selInstitute = (String) stream.getSelectedItem();
                tenureTypeList.clear();
                tenureLevelList.clear();
                cycleList.clear();

                tenureNameList.clear();
                tenureLevelNameList.clear();
                cycleNameList.clear();
                if (!"Select".equalsIgnoreCase(selInstitute)){
//            int position = university.getSelectedItemPosition();
                    if(streamList.size()==0){
                        return;
                    }
                    stream_uuid = streamList.get(position-1);
                    educationSubmitMapData.put("stream_uuid",stream_uuid);
//                    firstTimeUserDegreeData("degree", university_uuid, institute_uuid);
                    firstTimeUserData("tenure_type",university_uuid,institute_uuid,degree_uuid,stream_uuid,null,null,null);
                }else{
                    stream_uuid = null;
                }
            }

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




            }
        });

        tenureType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
                String selInstitute = (String) tenureType.getSelectedItem();
                tenureLevelList.clear();
                cycleList.clear();

                tenureLevelNameList.clear();
                cycleNameList.clear();
                if (!"Select".equalsIgnoreCase(selInstitute)){
//            int position = university.getSelectedItemPosition();
                    if(tenureTypeList.size()==0){
                        return;
                    }
                    tenure_uuid = tenureTypeList.get(position-1);
                    educationSubmitMapData.put("tenure_type",tenure_uuid);
//                    firstTimeUserDegreeData("degree", university_uuid, institute_uuid);
                    firstTimeUserData("tenure_level",university_uuid,institute_uuid,degree_uuid,stream_uuid,tenure_uuid,null,null);
                }else{
                    tenure_uuid = null;
                }
            }

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

            }
        });

        tenureLevel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
                String selInstitute = (String) tenureLevel.getSelectedItem();
                cycleList.clear();

                cycleNameList.clear();
                if (!"Select".equalsIgnoreCase(selInstitute)){
//            int position = university.getSelectedItemPosition();
                    if(tenureLevelList.size()==0){
                        return;
                    }
                    tenure_level_uuid = tenureLevelList.get(position-1);
                    educationSubmitMapData.put("tenure_level",tenure_level_uuid);
//                    firstTimeUserDegreeData("degree", university_uuid, institute_uuid);
                    firstTimeUserData("cycle",university_uuid,institute_uuid,degree_uuid,stream_uuid,tenure_uuid,tenure_level_uuid,null);
                }else{
                    tenure_level_uuid = null;
                }
            }

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

            }
        });

        cycleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
                String selInstitute = (String) cycleSpinner.getSelectedItem();
                if (!"Select".equalsIgnoreCase(selInstitute)){
//            int position = university.getSelectedItemPosition();
                    if(cycleList.size()==0){
                        return;
                    }
                    cycle_uuid = cycleList.get(position-1);
                    educationSubmitMapData.put("cycle",cycle_uuid);
//                    firstTimeUserDegreeData("degree", university_uuid, institute_uuid);
//                    firstTimeUserData("cycle",university_uuid,institute_uuid,degree_uuid,stream_uuid,tenure_uuid,cycle_uuid,null);
                }else{
                    cycle_uuid = null;
                }
            }

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

            }
        });
}

2 个答案:

答案 0 :(得分:1)

发生NullPointerException,因为您设置的适配器为null。在Spinner中删除setAdapter(null)。

            instituition.setAdapter(null);
            degree.setAdapter(null);
            stream.setAdapter(null);
            tenureType.setAdapter(null);
            tenureLevel.setAdapter(null);
            cycleSpinner.setAdapter(null);

答案 1 :(得分:1)

您不应该mySpinner.setEnabled(false)而不是这样,您可以使用{{1}}。虽然它不一样,但你必须妥协