2014-03-06 46 views
-1

我想打開壓光機上點擊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())); 

    } 
} 
+1

** showDialog(int)**方法已棄用。使用Dialogfragment。 – Piyush

+0

仍然我得到相同的例外 –

+0

哪條線你得到NPE? – Piyush

回答

0

一切都OK了你的代碼中有兩處改變,你必須在你的代碼

首先在onCreateView

myCalendar = Calendar.getInstance(); 

做到這一點做的,添加此

_date.show(); 

return _date; 
1

變化從

myCalendar.get(Calendar.YEAR), 
myCalendar.get(Calendar.MONTH), 
myCalendar.get(Calendar.DAY_OF_MONTH)) 

now.get(Calendar.YEAR), 
now.get(Calendar.MONTH), 
now.get(Calendar.DAY_OF_MONTH)) 

in showDialog()方法。

您忘了初始化Calendar myCalendar;上onCreateView()

myCalendar = Calendar.getInstance(); 
+0

的onclick編輯文本我的日曆是不開放 –

+0

刪除此的。 from_date.setFocusableInTouchMode(假); to_date.setFocusableInTouchMode(false); – Piyush

+0

仍然我日曆不開放 –

0

您正在使用的未初始化myCalendar的實例。嘗試使用現在,而不是myCalendar。

0

片段showdialog()方法不起作用。你可以這樣做,在from_date上設置setonclicklistener並在其中調用DatePickerDialog。做其他事情和你在活動課上一樣。 希望能幫到你。