因此,我们的想法是允许用户点击按钮(或文本视图),显示日期选择器,选择日期,将日期保存到数据库(在TODO列表中)并通过Toast显示所选日期到用户(如果我采取该路线,则在textview上)。所有这些都是标签活动的片段
现在我刚刚使用了一个按钮,当我点击它时,应用程序就会崩溃。 catlog显示以下内容
08-19 09:50:57.422 21472-21472/com.example.alibasmaci.maralapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.alibasmaci.maralapplication, PID: 21472
java.lang.NullPointerException
at com.example.alibasmaci.maralapplication.OneWayFragment.showDialogOnButtonClick(OneWayFragment.java:49)
at com.example.alibasmaci.maralapplication.OneWayFragment.onCreateView(OneWayFragment.java:93)
现在有问题的java类在这里:
package com.example.alibasmaci.maralapplication;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class OneWayFragment extends Fragment {
Spinner spinDeparture;
Spinner spinDestination;
Button btnDeparture;
EditText etDepartureDate;
int year_x, month_x, day_x;
static final int DIALOG_ID = 0;
String[] cities = {
"Ottawa",
"Montreal",
"Toronto"
};
public static OneWayFragment newInstance() {
OneWayFragment fragment = new OneWayFragment();
return fragment;
}
public OneWayFragment() {
// Required empty public constructor
}
public void showDialogOnButtonClick(){
btnDeparture = (Button)getActivity().findViewById(R.id.btnDepartureDate);
btnDeparture.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
getActivity().showDialog(DIALOG_ID);
}
}
);
}
protected Dialog onCreateDialog(int id){
if (id == DIALOG_ID)
return new DatePickerDialog(getActivity(),dPickerListener, year_x, month_x, day_x);
return null;
}
private DatePickerDialog.OnDateSetListener dPickerListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
year_x = year;
month_x = monthOfYear;
day_x = dayOfMonth;
Toast.makeText(getActivity(), year_x+"/"+month_x+"/"+day_x,Toast.LENGTH_LONG).show();
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_one_way, container, false);
// Spinner to display departure points
spinDeparture = (Spinner) rootView.findViewById(R.id.spinnerDeparture);
ArrayAdapter adapterDeparture = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,cities);
spinDeparture.setAdapter(adapterDeparture);
// Spinner to display destinations
spinDestination = (Spinner) rootView.findViewById(R.id.spinnerDestination);
ArrayAdapter adapterDestination = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,cities);
spinDeparture.setAdapter(adapterDestination);
// Show date picker when departure button is clicked
showDialogOnButtonClick();
return rootView;
}
}
错误所指的行在这里:
public void showDialogOnButtonClick() {
有关为什么会崩溃的任何想法?
答案 0 :(得分:0)
试试这个:
移动它:
btnDeparture = (Button)rootView.findViewById(R.id.btnDepartureDate);
到onCreateView(){...}
并为onClick试试这个:
public void showDialogOnButtonClick(){
btnDeparture.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
view.getContext().showDialog(DIALOG_ID);
}
}
);
}
试试吧。