想要使用三个DatePicker并在TextView上显示它

时间:2016-09-16 14:26:09

标签: android datepicker fragment

我正在尝试设置三个DatePicker,并希望在TextView中显示所选日期。我尝试了很多,但产生了一个错误,如: -

  ATAL EXCEPTION: main
                                                                         Process: com.example.sachin.datepicker, PID: 5708
                                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setInputType(int)' on a null object reference
                                                                             at com.example.sachin.datepicker.MainActivity.onCreateView(MainActivity.java:40)
                                                                             at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                                                                             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
                                                                             at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758) 

在这里,我发布了它的完整代码..只需看看它,

MainActivity2.java

    package com.example.sachin.datepicker;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity {
    Button next;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        next= (Button)findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mFragmentManager = getSupportFragmentManager();
                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragmentTransaction.replace(R.id.containerView,new MainActivity()).commit();
            }
        });
    }
}

此处, MainActivity.java

       package com.example.sachin.datepicker;

import android.annotation.TargetApi;
import android.app.DatePickerDialog;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;

import java.util.Calendar;
import java.util.Locale;

public class MainActivity extends Fragment implements View.OnClickListener {


        public MainActivity() {};
        EditText openingdate,Birthdate,Anniversary;

        private DatePickerDialog fromDatePickerDialog;
        private DatePickerDialog toDatePickerDialog;
        private DatePickerDialog toDatePickerDialog1;
        private SimpleDateFormat dateFormatter;

                View view;
        @TargetApi(Build.VERSION_CODES.N)
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

                view = inflater.inflate(R.layout.activity_add_wod, container, false);
                dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);

                openingdate = (EditText) view.findViewById(R.id.openingdate);
                openingdate.setInputType(InputType.TYPE_NULL);
                openingdate.requestFocus();

                Birthdate = (EditText)view. findViewById(R.id.Birthdate);
                Birthdate.setInputType(InputType.TYPE_NULL);
                Birthdate.requestFocus();
                Anniversary = (EditText)view. findViewById(R.id.Anniversary);
                Anniversary.setInputType(InputType.TYPE_NULL);

                setDateTimeField();
                return view;
        }



        private void setDateTimeField() {
                openingdate.setOnClickListener(this);
                Birthdate.setOnClickListener(this);
                Anniversary.setOnClickListener(this);
                Calendar newCalendar = Calendar.getInstance();
                fromDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

                        @TargetApi(Build.VERSION_CODES.N)
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                Calendar newDate = Calendar.getInstance();
                                newDate.set(year, monthOfYear, dayOfMonth);
                                openingdate.setText(dateFormatter.format(newDate.getTime()));
                        }

                },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

                toDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

                        @TargetApi(Build.VERSION_CODES.N)
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                Calendar newDate = Calendar.getInstance();
                                newDate.set(year, monthOfYear, dayOfMonth);
                                Birthdate.setText(dateFormatter.format(newDate.getTime()));
                        }

                },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
                toDatePickerDialog1 = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

                        @TargetApi(Build.VERSION_CODES.N)
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                Calendar newDate = Calendar.getInstance();
                                newDate.set(year, monthOfYear, dayOfMonth);
                                Anniversary.setText(dateFormatter.format(newDate.getTime()));
                        }

                },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
        }

        //  @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getActivity().getMenuInflater().inflate(R.menu.main, menu);
                return true;
        }

        @Override
        public void onClick(View view) {
                if(view == openingdate) {
                        fromDatePickerDialog.show();
                } else if(view == Birthdate) {
                        toDatePickerDialog.show();
                }  else if(view == Anniversary) {
                        toDatePickerDialog1.show();
                }
        }
}

最后清单文件

     <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sachin.datepicker">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <Activity android:name=".MainActivity" />

        <activity android:name=".Main2Activity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我尝试了很多但总是收到错误...

1 个答案:

答案 0 :(得分:0)

.setInputType(InputType.TYPE_CLASS_DATETIME);

更改.setInputType中的所有内容,同时尝试以下代码。

    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            final View view = inflater.inflate(R.layout.activity_add_wod, container, false);
            dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);

            openingdate = (EditText) view.findViewById(R.id.openingdate);
            openingdate.setInputType(InputType.TYPE_NULL);
            openingdate.requestFocus();

            Birthdate = (EditText) view.findViewById(R.id.Birthdate);
            Birthdate.setInputType(InputType.TYPE_NULL);
            Birthdate.requestFocus();
            Anniversary = (EditText) view.findViewById(R.id.Anniversary);
            Anniversary.setInputType(InputType.TYPE_NULL);

            setDateTimeField();
            return view;
    }

    private void setDateTimeField() {
            openingdate.setOnClickListener(this);
            Birthdate.setOnClickListener(this);
            Anniversary.setOnClickListener(this);
            Calendar newCalendar = Calendar.getInstance();


            fromDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

                    @TargetApi(Build.VERSION_CODES.N)
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            Calendar newDate = Calendar.getInstance();
                            newDate.set(year, monthOfYear, dayOfMonth);
                            openingdate.setText(dateFormatter.format(newDate.getTime()));
                    }

            },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));



            toDatePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

                    @TargetApi(Build.VERSION_CODES.N)
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            Calendar newDate = Calendar.getInstance();
                            newDate.set(year, monthOfYear, dayOfMonth);
                            Birthdate.setText(dateFormatter.format(newDate.getTime()));
                    }

            },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));


            toDatePickerDialog1 = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

                    @TargetApi(Build.VERSION_CODES.N)
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            Calendar newDate = Calendar.getInstance();
                            newDate.set(year, monthOfYear, dayOfMonth);
                            Anniversary.setText(dateFormatter.format(newDate.getTime()));
                    }

            },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    }


    @Override
    public void onClick(View view) {
            if(view == openingdate) {
                    fromDatePickerDialog.show();
            } else if(view == Birthdate) {
                    toDatePickerDialog.show();
            }  else if(view == Anniversary) {
                    toDatePickerDialog1.show();
            }
    }