我想打開壓光機上點擊Fragment.But內的編輯文本時,當我點擊編輯文本它是拋出空指針例外給我。 相同的代碼在Activity類中正常工作,但在Fargement中不能正常工作。打開日曆片段上EditText
異常
03-05 19:02:39.888: E/AndroidRuntime(1092): FATAL EXCEPTION: main
03-05 19:02:39.888: E/AndroidRuntime(1092): java.lang.NullPointerException
03-05 19:02:39.888: E/AndroidRuntime(1092): at com.test.newapp.AppTrip.showDialog(AppTrip.java:75)
03-05 19:02:39.888: E/AndroidRuntime(1092): at com.test.newapp.AppTrip$2.onClick(AppTrip.java:54)
03-05 19:02:39.888: E/AndroidRuntime(1092): at android.view.View.performClick(View.java:4212)
03-05 19:02:39.888: E/AndroidRuntime(1092): at android.view.View$PerformClick.run(View.java:17476)
03-05 19:02:39.888: E/AndroidRuntime(1092): at android.os.Handler.handleCallback(Handler.java:800)
03-05 19:02:39.888: E/AndroidRuntime(1092): at android.os.Handler.dispatchMessage(Handler.java:100)
03-05 19:02:39.888: E/AndroidRuntime(1092): at android.os.Looper.loop(Looper.java:194)
03-05 19:02:39.888: E/AndroidRuntime(1092): at android.app.ActivityThread.main(ActivityThread.java:5371)
03-05 19:02:39.888: E/AndroidRuntime(1092): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 19:02:39.888: E/AndroidRuntime(1092): at java.lang.reflect.Method.invoke(Method.java:525)
03-05 19:02:39.888: E/AndroidRuntime(1092): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-05 19:02:39.888: E/AndroidRuntime(1092): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-05 19:02:39.888: E/AndroidRuntime(1092): at dalvik.system.NativeStart.main(Native Method)
片段代碼
public class AppTrip extends Fragment {
View rootView;
EditText from_date, to_date;
DatePickerDialog.OnDateSetListener date;
static final int DATE_DIALOG_ID = 999;
Calendar myCalendar;
int val;
Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();
rootView = inflater.inflate(R.layout.main, container, false);
from_date = (EditText) rootView.findViewById(R.id.from_date);
to_date = (EditText) rootView.findViewById(R.id.to_date);
from_date.setFocusableInTouchMode(false);
to_date.setFocusableInTouchMode(false);
date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel(val);
}
};
from_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
val = 1;
}
});
to_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
val = 2;
}
});
return rootView;
}
protected Dialog showDialog(int dateDialogId) {
final Calendar now = Calendar.getInstance();
switch (dateDialogId) {
case DATE_DIALOG_ID:
DatePickerDialog _date = new DatePickerDialog(context, date,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)) {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
if (year - now.get(Calendar.YEAR) > 1) {
view.updateDate(year - 1,
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
}
if (year - now.get(Calendar.YEAR) == 1
&& monthOfYear > now.get(Calendar.MONTH)) {
view.updateDate(year, myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
}
if (year - now.get(Calendar.YEAR) == 1
&& monthOfYear == now.get(Calendar.MONTH)
&& dayOfMonth > now.get(Calendar.DAY_OF_MONTH)) {
view.updateDate(year, myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
}
if (year < now.get(Calendar.YEAR))
view.updateDate(myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
if (monthOfYear < now.get(Calendar.MONTH)
&& year == now.get(Calendar.YEAR))
view.updateDate(myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
if (dayOfMonth < now.get(Calendar.DAY_OF_MONTH)
&& year == now.get(Calendar.YEAR)
&& monthOfYear == now.get(Calendar.MONTH))
view.updateDate(myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
}
};
return _date;
}
return null;
}
private void updateLabel(int val) {
String myFormat = "dd/MM/yy"; // In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
Log.d("Date value ", "===" + sdf.format(myCalendar.getTime()));
if (val == 1)
from_date.setText(sdf.format(myCalendar.getTime()));
else
to_date.setText(sdf.format(myCalendar.getTime()));
}
}
** showDialog(int)**方法已棄用。使用Dialogfragment。 – Piyush
仍然我得到相同的例外 –
哪條線你得到NPE? – Piyush