具有多个片段的两个活动之间的空对象引用

时间:2016-04-11 13:58:34

标签: java android android-fragments android-fragmentactivity

错误是: 'null对象引用上的void android.widget.TableRow.setVisibility(int)'

android studio和java非常新,并且已经使用了几个星期。基本上问题是我有多个活动,每个活动都有多个片段。其中一个名为Calculator有三个片段,在fragemntCalc2中,我有很多表行,我希望通过从不同活动片段的下拉列表中看不到它,特别是一个名为profile的片段和它的第二个片段。

public class Fragment2 extends Fragment {

Context context;
Spinner CourseNameSpinner1;
Spinner QualificationSpinner1;
Spinner ExamboardSpinner1;
Spinner spinner_switch;

public static Fragment2 newInstance()
{
    Fragment2 fragment = new Fragment2();
    return fragment;
}

public Fragment2() {}

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


    CourseNameSpinner1 = (Spinner) rootView.findViewById(R.id.CourseNameSpinner1);
    QualificationSpinner1 = (Spinner) rootView.findViewById(R.id.QualificationSpinner1);
    ExamboardSpinner1 = (Spinner) rootView.findViewById(R.id.ExamboardSpinner1);
    spinner_switch = (Spinner) rootView.findViewById(R.id.spinner_switch);

    //Course Dropdown
    Spinner spinner2 = (Spinner) rootView.findViewById(R.id.CourseNameSpinner1);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(context, R.array.Course_Array, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
    spinner2.setAdapter(adapter2); // Apply the adapter to the spinner

    //Qualification Dropdown
    Spinner spinner3 = (Spinner) rootView.findViewById(R.id.QualificationSpinner1);
    ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(context, R.array.Qualification_Array, android.R.layout.simple_spinner_item);
    adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
    spinner3.setAdapter(adapter3); // Apply the adapter to the spinner

    //ExamBoard Dropdown
    Spinner spinner4 = (Spinner) rootView.findViewById(R.id.ExamboardSpinner1);
    ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(context, R.array.Exam_Array, android.R.layout.simple_spinner_item);
    adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
    spinner4.setAdapter(adapter4); // Apply the adapter to the spinner

    Spinner spinner23 = (Spinner) rootView.findViewById(R.id.spinner_switch);
    ArrayAdapter<CharSequence> adapter23 = ArrayAdapter.createFromResource(context, R.array.Switch_Array, android.R.layout.simple_spinner_item);
    adapter23.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
    spinner23.setAdapter(adapter23); // Apply the adapter to the spinner

    //ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(context, R.array.Course_Array, android.R.layout.simple_spinner_item);
    //Where context is, originally it was a this.
    //Changed as the spinner isn't declared in main activity
    //this is fragment java, therefore it needs context to declare the spinner

    SharedPreferences sharedPref = context.getSharedPreferences("Units", context.MODE_PRIVATE);
    int spinnerValue = sharedPref.getInt("UserCourseChoiceSpinner1", -1);
    int spinnerValue2 = sharedPref.getInt("UserCourseChoiceSpinner2",-1);
    int spinnerValue3 = sharedPref.getInt("UserCourseChoiceSpinner3",-1);
    if(spinnerValue != -1)

    CourseNameSpinner1.setSelection(spinnerValue);
    QualificationSpinner1.setSelection(spinnerValue2);
    ExamboardSpinner1.setSelection(spinnerValue3);


    Button button = (Button) rootView.findViewById(R.id.button_savecourse);
    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            int userChoice = CourseNameSpinner1.getSelectedItemPosition();
            int userChoice2 = QualificationSpinner1.getSelectedItemPosition();
            int userChoice3 = ExamboardSpinner1.getSelectedItemPosition();

            SharedPreferences sharedPref = context.getSharedPreferences("Units", Context.MODE_PRIVATE);
            SharedPreferences.Editor prefEditor = sharedPref.edit();

            prefEditor.putInt("UserCourseChoiceSpinner1",userChoice);
            prefEditor.putInt("UserCourseChoiceSpinner2",userChoice2);
            prefEditor.putInt("UserCourseChoiceSpinner3",userChoice3);

            prefEditor.commit();

            Toast.makeText(context, "Data was saved successfully", Toast.LENGTH_LONG).show();
        }
    });

    Spinner spinner_switch = (Spinner)rootView.findViewById(R.id.spinner_switch);
    spinner_switch.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        {
            TableRow tableRow;
            if (position != 1)
            {
                tableRow = (TableRow) getActivity().findViewById(R.id.table13);
                tableRow.setVisibility(View.GONE);
                tableRow = (TableRow) getActivity().findViewById(R.id.table14);
                tableRow.setVisibility(View.GONE);
                tableRow = (TableRow) getActivity().findViewById(R.id.table15);
                tableRow.setVisibility(View.GONE);
                tableRow = (TableRow) getActivity().findViewById(R.id.table16);
                tableRow.setVisibility(View.GONE);
                tableRow = (TableRow) getActivity().findViewById(R.id.table17);
                tableRow.setVisibility(View.GONE);
                tableRow = (TableRow) getActivity().findViewById(R.id.table18);
                tableRow.setVisibility(View.GONE);

                tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade13);
                tableRow.setVisibility(View.GONE);
                tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade14);
                tableRow.setVisibility(View.GONE);
                tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade15);
                tableRow.setVisibility(View.GONE);
                tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade16);
                tableRow.setVisibility(View.GONE);
                tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade17);
                tableRow.setVisibility(View.GONE);
                tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade18);
                tableRow.setVisibility(View.GONE);

            }
            else
            {
                tableRow = (TableRow) getActivity().findViewById(R.id.table13);
                tableRow.setVisibility(View.VISIBLE);
                tableRow = (TableRow) getActivity().findViewById(R.id.table14);
                tableRow.setVisibility(View.VISIBLE);
                tableRow = (TableRow) getActivity().findViewById(R.id.table15);
                tableRow.setVisibility(View.VISIBLE);
                tableRow = (TableRow) getActivity().findViewById(R.id.table16);
                tableRow.setVisibility(View.VISIBLE);
                tableRow = (TableRow) getActivity().findViewById(R.id.table17);
                tableRow.setVisibility(View.VISIBLE);
                tableRow = (TableRow) getActivity().findViewById(R.id.table18);
                tableRow.setVisibility(View.VISIBLE);

                tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade13);
                tableRow.setVisibility(View.VISIBLE);
                tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade14);
                tableRow.setVisibility(View.VISIBLE);
                tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade15);
                tableRow.setVisibility(View.VISIBLE);
                tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade16);
                tableRow.setVisibility(View.VISIBLE);
                tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade17);
                tableRow.setVisibility(View.VISIBLE);
                tableRow = (TableRow) getActivity().findViewById(R.id.tablegrade18);
                tableRow.setVisibility(View.VISIBLE);
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {

        }
    });
    return rootView;
}

}

空对象引用在行

tableRow = (TableRow) getActivity().findViewById(R.id.table13);
                tableRow.setVisibility(View.GONE);

所以我认为其余部分不起作用。再次作为信息,表格是一个完全不同的活动片段,所以我怎么做才能让片段找到表格,即使我正在声明上下文。

1 个答案:

答案 0 :(得分:0)

像这样改变

tableRow = (TableRow) rootView.findViewById(R.id.table13);
            tableRow.setVisibility(View.GONE);

并声明rootView全局,并为所有TableRow采用单独的变量