APP停止工作 - 片段

时间:2014-09-25 08:35:37

标签: java android android-fragments

我正在Android中创建日期选择器。但不幸的是,点击按钮时应用程序停止工作。这是我的代码。我是android开发的新手。

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)

2 个答案:

答案 0 :(得分: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,以便了解其工作原理。

希望它有所帮助。

答案 1 :(得分:2)

getFragmentManager()更改为getSupportFragmentManager()

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

保持使用片段的一致性。使用主API中的片段或从支持库中使用它。