我在加载类offline_day时收到NULLPointer异常。 我试图解决问题,但我无法找到导致NULLPointer异常发生的根本原因。 我在一些有用的建议之后编辑了我的代码。但我仍然得到了NPEeception。 我的班级是这样的: Offline_day.java:-
public class Offline_day extends DialogFragment implements OnDateChangedListener, OnTimeChangedListener
{
public final static int DATE_PICKER = 1;
public final static int TIME_PICKER = 2;
public final static int DATE_TIME_PICKER = 3;
private DatePicker datePicker;
private TimePicker timePicker1;
private TimePicker timePicker2;
private Calendar mCalendar;
private Activity activity;
private int DialogType;
private View mView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//Activity activity;
View mView = inflater.inflate(R.layout.offline_day, container, false);
mCalendar = Calendar.getInstance();
datePicker = (DatePicker) mView.findViewById(R.id.datePicker1);
// datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), this);
timePicker1 = (TimePicker) mView.findViewById(R.id.timePicker1);
timePicker2 = (TimePicker) mView.findViewById(R.id.timePicker2);
button=(Button)mView.findViewById(R.id.button1);
//setIs24HourView(true);
//setCalendarViewShown(false);
//onDateChanged(datePicker,2015,6,5);
//onTimeChanged(timePicker1,9,00);
//onTimeChanged(timePicker2,19,30);
//datePicker.setVisibility(View.GONE);
//timePicker1.setVisibility(View.GONE);
//timePicker2.setVisibility(View.GONE);
return mView;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
//timePicker1.setOnTimeChangedListener(this);
//timePicker2.setOnTimeChangedListener(this);
}
/*public int get(final int field)
{
return mCalendar.get(field);
}
public long getDateTimeMillis()
{
return mCalendar.getTimeInMillis();
}
public void setIs24HourView(boolean is24HourView)
{
timePicker1.setIs24HourView(is24HourView1());
timePicker2.setIs24HourView(is24HourView2());
}
public boolean is24HourView1()
{
return timePicker1.is24HourView();
}
public boolean is24HourView2()
{
return timePicker2.is24HourView();
}
public void setCalendarViewShown(boolean calendarView)
{
datePicker.setCalendarViewShown(calendarView);
}
public boolean CalendarViewShown()
{
return datePicker.getCalendarViewShown();
}
public void updateDate(int year, int monthOfYear, int dayOfMonth)
{
datePicker.updateDate(year, monthOfYear, dayOfMonth);
}
public void updateTime(int currentHour, int currentMinute)
{
timePicker1.setCurrentHour(currentHour);
timePicker1.setCurrentMinute(currentMinute);
timePicker2.setCurrentHour(currentHour);
timePicker2.setCurrentMinute(currentMinute);
}
public String getDateTime()
{
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
return sdf.format(mCalendar.getTime());
}*/
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
mCalendar.set(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
}
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
{
//mCalendar.set(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
mCalendar.set(mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE));
}
/*private DatePickerDialog.OnDateSetListener datePickerListener=new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay)
{}
};
DatePickerDialog dialog = new DatePickerDialog(getActivity(), datePickerListener, 2000, 1,1); */
}
这是我的班级。
现在,当我加载应用程序时,出现以下错误:
06-11 16:30:54.229: E/AndroidRuntime(1592): FATAL EXCEPTION: main
06-11 16:30:54.229: E/AndroidRuntime(1592): Process: com.example.mobidoot PID: 1592
06-11 16:30:54.229: E/AndroidRuntime(1592): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
06-11 16:30:54.229: E/AndroidRuntime(1592): at android.app.DatePickerDialog.resolveDialogTheme(DatePickerDialog.java:88)
06-11 16:30:54.229: E/AndroidRuntime(1592): at android.app.DatePickerDialog.<init>(DatePickerDialog.java:105)
06-11 16:30:54.229: E/AndroidRuntime(1592): at android.app.DatePickerDialog.<init>(DatePickerDialog.java:82)
06-11 16:30:54.229: E/AndroidRuntime(1592): at android.view.View.performClick(View.java:4780)
06-11 16:30:54.229: E/AndroidRuntime(1592): at android.view.View$PerformClick.run(View.java:19866)
06-11 16:30:54.229: E/AndroidRuntime(1592): at android.os.Handler.handleCallback(Handler.java:739)
06-11 16:30:54.229: E/AndroidRuntime(1592): at android.os.Handler.dispatchMessage(Handler.java:95)
06-11 16:30:54.229: E/AndroidRuntime(1592): at android.os.Looper.loop(Looper.java:135)
06-11 16:30:54.229: E/AndroidRuntime(1592): at android.app.ActivityThread.main(ActivityThread.java:5257)
06-11 16:30:54.229: E/AndroidRuntime(1592): at java.lang.reflect.Method.invoke(Native Method)
06-11 16:30:54.229: E/AndroidRuntime(1592): at java.lang.reflect.Method.invoke(Method.java:372)
06-11 16:30:54.229: E/AndroidRuntime(1592): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-11 16:30:54.229: E/AndroidRuntime(1592): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我仍然在接受NPException。 任何帮助都非常感谢。
答案 0 :(得分:3)
当您初始化DatePickerDialog dialog = new DatePickerDialog(getActivity(), datePickerListener, 2000, 1,1);
getActivity()
时, null
将返回Fragment
。您必须等到片段生命周期中的onAttach()
才能返回非空值。
这就是NPE的原因。
总的来说,片段中没有非默认构造函数。仅在onCreateView()
中为通用片段设置片段视图,或为onCreateDialog()
设置DialogFragment
。
答案 1 :(得分:0)
在片段中使用getActivity()
的{{1}}。