我正在制作一个应用程序,作为一个小型项目,用于计算一个已经上下班的人,并显示他/她的出勤状态
但是现在我在尝试用spinner
选择一些选项时遇到了一些空指针异常这是我的Java文件:TimeTableFragment.java
package in.teambhargavinilayam.abh.massbunk;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
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.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class TimeTableFragment extends Fragment {
public TimeTableFragment() {
// Required empty public constructor
}
Spinner hour_1, hour_2, hour_3, hour_4, hour_5, hour_6;
DBHelper myTimeTableDb;
String hour_1_sub, hour_2_sub, hour_3_sub, hour_4_sub, hour_5_sub, hour_6_sub;
TextView day_txt;
int hour_no = 1;
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_time_table, container, false);
myTimeTableDb = new DBHelper(view.getContext());
hour_1 = (Spinner) view.findViewById(R.id.hour_1_subj);
hour_2 = (Spinner) view.findViewById(R.id.hour_2_subj);
hour_3 = (Spinner) view.findViewById(R.id.hour_3_subj);
hour_4 = (Spinner) view.findViewById(R.id.hour_4_subj);
hour_5 = (Spinner) view.findViewById(R.id.hour_5_subj);
hour_6 = (Spinner) view.findViewById(R.id.hour_6_subj);
String[] subj = GetAllSubjects();
// Hour 1 List
List<String> hour_1_list = new ArrayList<String>();
hour_1_list.add("Select Subject..");
hour_1_list.add(subj[0]);
hour_1_list.add(subj[1]);
hour_1_list.add(subj[2]);
hour_1_list.add(subj[3]);
hour_1_list.add(subj[4]);
hour_1_list.add(subj[5]);
ArrayAdapter<String> hour_1_ListAdapter = new ArrayAdapter<String>(view.getContext(), R.layout.support_simple_spinner_dropdown_item, hour_1_list);
hour_1_ListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hour_1.setAdapter(hour_1_ListAdapter);
hour_1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
hour_1_sub = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
hour_1_sub = null;
}
});
// Hour 2 List
List<String> hour_2_list = new ArrayList<String>();
hour_2_list.add("Select Subject..");
hour_2_list.add(subj[0]);
hour_2_list.add(subj[1]);
hour_2_list.add(subj[2]);
hour_2_list.add(subj[3]);
hour_2_list.add(subj[4]);
hour_2_list.add(subj[5]);
ArrayAdapter<String> hour_2_ListAdapter = new ArrayAdapter<String>(view.getContext(), R.layout.support_simple_spinner_dropdown_item, hour_2_list);
hour_2_ListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hour_2.setAdapter(hour_2_ListAdapter);
hour_2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
hour_2_sub = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
hour_2_sub = null;
}
});
// Hour 3 List
List<String> hour_3_list = new ArrayList<String>();
hour_3_list.add("Select Subject..");
hour_3_list.add(subj[0]);
hour_3_list.add(subj[1]);
hour_3_list.add(subj[2]);
hour_3_list.add(subj[3]);
hour_3_list.add(subj[4]);
hour_3_list.add(subj[5]);
ArrayAdapter<String> hour_3_ListAdapter = new ArrayAdapter<String>(view.getContext(), R.layout.support_simple_spinner_dropdown_item, hour_3_list);
hour_3_ListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hour_3.setAdapter(hour_3_ListAdapter);
hour_3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
hour_3_sub = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
hour_3_sub = null;
}
});
// Hour 4 List
List<String> hour_4_list = new ArrayList<String>();
hour_4_list.add("Select Subject..");
hour_4_list.add(subj[0]);
hour_4_list.add(subj[1]);
hour_4_list.add(subj[2]);
hour_4_list.add(subj[3]);
hour_4_list.add(subj[4]);
hour_4_list.add(subj[5]);
ArrayAdapter<String> hour_4_ListAdapter = new ArrayAdapter<String>(view.getContext(), R.layout.support_simple_spinner_dropdown_item, hour_4_list);
hour_4_ListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hour_4.setAdapter(hour_4_ListAdapter);
hour_4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
hour_4_sub = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
hour_4_sub = null;
}
});
// Hour 5 List
List<String> hour_5_list = new ArrayList<String>();
hour_5_list.add("Select Subject..");
hour_5_list.add(subj[0]);
hour_5_list.add(subj[1]);
hour_5_list.add(subj[2]);
hour_5_list.add(subj[3]);
hour_5_list.add(subj[4]);
hour_5_list.add(subj[5]);
ArrayAdapter<String> hour_5_ListAdapter = new ArrayAdapter<String>(view.getContext(), R.layout.support_simple_spinner_dropdown_item, hour_5_list);
hour_5_ListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hour_5.setAdapter(hour_5_ListAdapter);
hour_5.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
hour_5_sub = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
hour_5_sub = null;
}
});
// Hour 6 List
List<String> hour_6_list = new ArrayList<String>();
hour_6_list.add("Select Subject..");
hour_6_list.add(subj[0]);
hour_6_list.add(subj[1]);
hour_6_list.add(subj[2]);
hour_6_list.add(subj[3]);
hour_6_list.add(subj[4]);
hour_6_list.add(subj[5]);
ArrayAdapter<String> hour_6_ListAdapter = new ArrayAdapter<String>(view.getContext(), R.layout.support_simple_spinner_dropdown_item, hour_6_list);
hour_6_ListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hour_6.setAdapter(hour_6_ListAdapter);
hour_6.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
hour_6_sub = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
hour_6_sub = null;
}
});
day_txt = (TextView) view.findViewById(R.id.day_string);
FloatingActionButton nextBtn = (FloatingActionButton) view.findViewById(R.id.nextBtn);
final FloatingActionButton previousBtn = (FloatingActionButton) view.findViewById(R.id.previousBtn);
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (hour_1_sub.isEmpty() || hour_1_sub.equals("Select Subject..")){
Toast.makeText(view.getContext(), "Choose A subject", Toast.LENGTH_SHORT).show();
}
else if (hour_2_sub.isEmpty() || hour_2_sub.equals("Select Subject..")){
Toast.makeText(view.getContext(), "Choose A subject", Toast.LENGTH_SHORT).show();
}
else if (hour_3_sub.isEmpty() || hour_3_sub.equals("Select Subject..")){
Toast.makeText(view.getContext(), "Choose A subject", Toast.LENGTH_SHORT).show();
}
else if (hour_4_sub.isEmpty() || hour_4_sub.equals("Select Subject..")){
Toast.makeText(view.getContext(), "Choose A subject", Toast.LENGTH_SHORT).show();
}
else if (hour_5_sub.isEmpty() || hour_5_sub.equals("Select Subject..")){
Toast.makeText(view.getContext(), "Choose A subject", Toast.LENGTH_SHORT).show();
}
else if (hour_6_sub.isEmpty() || hour_6_sub.equals("Select Subject..")){
Toast.makeText(view.getContext(), "Choose A subject", Toast.LENGTH_SHORT).show();
}
else {
String[] hours = {
hour_1_sub,
hour_2_sub,
hour_3_sub,
hour_4_sub,
hour_5_sub,
hour_6_sub,
};
boolean isSaved = SaveTimeTableRow(hours,hour_no);
if (isSaved){
hour_no++;
if (hour_no == 6) {
((MainActivity) getActivity()).callFragment(4); //calling Current Status Fragment
}
hour_1.setSelection(0);
hour_2.setSelection(0);
hour_3.setSelection(0);
hour_4.setSelection(0);
hour_5.setSelection(0);
hour_6.setSelection(0);
previousBtn.show();
SetDay();
}
else {
Toast.makeText(view.getContext(), "Not Saved", Toast.LENGTH_SHORT).show();
}
}
}
});
previousBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hour_no--;
if (hour_no == 1)
previousBtn.hide();
SetDay();
}
});
return view;
}
public String[] GetAllSubjects(){
String[] subjects = myTimeTableDb.ShowAllSubjects();
return subjects;
}
public boolean SaveTimeTableRow(String[] hours,int hour_no){
boolean isInserted = myTimeTableDb.InsertOrUpdateTimeTable(hours, Integer.toString(hour_no));
return isInserted;
}
public void SetDay(){
switch (hour_no){
case 1:
day_txt.setText("Monday");
break;
case 2:
day_txt.setText("Tuesday");
break;
case 3:
day_txt.setText("Wednesday");
break;
case 4:
day_txt.setText("Thursday");
break;
case 5:
day_txt.setText("Friday");
break;
}
}
}
错误是:
FATAL EXCEPTION: main
Process: in.teambhargavinilayam.abh.massbunk, PID: 24161
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:448)
at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:963)
at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:959)
at android.widget.Spinner.measureContentWidth(Spinner.java:833)
at android.widget.Spinner$DropdownPopup.computeContentWidth(Spinner.java:1194)
at android.widget.Spinner$DropdownPopup.show(Spinner.java:1220)
at android.widget.Spinner.performClick(Spinner.java:758)
at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:438)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
请帮我解决此错误
答案 0 :(得分:-1)
试
hour_1_sub = hour_1_list .get(position).toString();
insted of
hour_1_sub = parent.getItemAtPosition(position).toString();
在每个微调器上的onSelectListner。