2014-09-25 168 views
0

我在Android中創建日期選擇器。但不幸的是,該應用程序單擊按鈕時停止工作。這是我的代碼。我是新的android開發。APP停止工作 - 片段

fragment_1.java

public class fragment_1 extends Fragment { 

    private EditText txtJODate; 
    private DatePickerDialog txtDTJODate; 
    private SimpleDateFormat dateFormat; 
    private View view; 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){ 
     return inflater.inflate(R.layout.fragment_tab1, container, false); 

    } 

    public void onClickDate(View v){ 
     android.supporft.v4.app.DialogFragment newFragment = new DatePickerFragment(); 
     newFragment.show(getFragmentManager(), "datePicker"); 
    } 

} 

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment 
     implements DatePickerDialog.OnDateSetListener { 


    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
// Use the current date as the default date in the picker 
     final Calendar c = Calendar.getInstance(); 
     int year = c.get(Calendar.YEAR); 
     int month = c.get(Calendar.MONTH); 
     int day = c.get(Calendar.DAY_OF_MONTH); 

// Create a new instance of DatePickerDialog and return it 
     return new DatePickerDialog(getActivity(), this, year, month, day); 
    } 

    @Override 
    public void onDateSet(DatePicker view, int year, int month, int day) { 
     Calendar c = Calendar.getInstance(); 
     c.set(year, month, day); 

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
     String formattedDate = sdf.format(c.getTime()); 
    } 
} 

這是我的佈局

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#D9E5F2"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="16dp" 
     android:paddingRight="16dp" 
     > 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="text" 
      android:text="@string/txt_customer" 
      android:id="@+id/txtCustomer" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/txtDate" 
      android:onClick="onClickDate"/> 

    </LinearLayout> 

</LinearLayout> 

這裏是我的logcat:

java.lang.IllegalStateException: Could not find a method onClickDate(View) in the activity class com.apptech_experts.appportal.fragment_main for onClick handler on view class android.widget.Button with id 'txtDate' 
     at android.view.View$1.onClick(View.java:3956) 
     at android.view.View.performClick(View.java:4630) 
     at android.view.View$PerformClick.run(View.java:19339) 
     at android.os.Handler.handleCallback(Handler.java:733) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:157) 
     at android.app.ActivityThread.main(ActivityThread.java:5335) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
     at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NoSuchMethodException: onClickDate [class android.view.View] 
     at java.lang.Class.getConstructorOrMethod(Class.java:472) 
     at java.lang.Class.getMethod(Class.java:857) 
     at android.view.View$1.onClick(View.java:3949) 
            at android.view.View.performClick(View.java:4630) 
            at android.view.View$PerformClick.run(View.java:19339) 
            at android.os.Handler.handleCallback(Handler.java:733) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:157) 
            at android.app.ActivityThread.main(ActivityThread.java:5335) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
            at dalvik.system.NativeStart.main(Native Method) 
+0

請發佈logcat輸出以查看崩潰並提及哪條線路發生崩潰。 – 2014-09-25 08:36:19

+0

嗨@MysticMagic,我認爲在我的按鈕onClickDate有問題。因此我的代碼正確嗎? – 2014-09-25 08:39:12

+0

哪裏是'onClickDate'方法? – 2014-09-25 09:01:05

回答

2

您已經使用

android:onClick="onClickDate" 

您在xml文件按鈕。

但是你的java代碼不包含函數onClickDate()。這就是你崩潰的原因:

Could not find a method onClickDate(View) in the activity class com.apptech_experts.appportal.fragment_main 

請在你的班級裏添加一個函數。

public void onClickDate (View v){ 
    //do something 
} 

請參考onClick,以便您瞭解它是如何工作的。

希望它有幫助。

+0

我跟着你,但仍然停下來。 public void onClickDate(View v){ android.support.v4.app.DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getFragmentManager(),「datePicker」); } – 2014-09-25 08:46:46

+0

請使用更新的代碼和logcat編輯問題。 @GimoGilmore – 2014-09-25 08:47:40

+0

已更新@MysticMagic。請我的logcat – 2014-09-25 08:54:00

2

變化getFragmentManager()getSupportFragmentManager()

public void onClickDate(View v){ 
    android.support.v4.app.DialogFragment newFragment = new DatePickerFragment(); 
    newFragment.show(getSupportFragmentManager(), "datePicker"); 
} 

保持一致性使用片段。要麼使用主API的片段,要麼從支持庫使用它。