Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在Android中创建日期选择器_Java_Android_Android Datepicker - Fatal编程技术网

Java 无法在Android中创建日期选择器

Java 无法在Android中创建日期选择器,java,android,android-datepicker,Java,Android,Android Datepicker,我刚刚编写了一个Android程序,它包含在一个日期选择器中,但它不起作用 Java代码是: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { CurDateTv2 = (TextView)findViewById(R.id.textView2); Picker = (DateP

我刚刚编写了一个Android程序,它包含在一个日期选择器中,但它不起作用

Java代码是:

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        CurDateTv2 = (TextView)findViewById(R.id.textView2);
        Picker = (DatePicker)findViewById(R.id.calendarView1);
        btnChangeDate = (Button)findViewById(R.id.button1);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         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);
            Picker.init(year, month,day, onChangedListener())
            }

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


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long

        int id = item.getItemId();
        if (id == R.id.action_settings) 
            return true;

        return super.onOptionsItemSelected(item);
    }
   public void onDateChangedListener(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
       CurDateTv2.setText(new String Builder()
               .append(dayOfMonth).append("/").append(monthOfYear + 1)
               .aapend("/").append(year).append(" "));

   }
}
布局是

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

    <Button 
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Date" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Current Date"
            android:textAppearance="?android:attr/textAppearanceLarge"
            />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="08/08/2016"
                android:textAppearance="?android:attr/textAppearanceLarge" 
                />

<CalendarView
                android:id="@+id/calendarView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
 />

    </LinearLayout>

不幸的是,我不太懂Java,所以我无法确定错误在哪里。

我真的希望您能帮助我。

考虑在日期和时间选择器中使用DialogFragment类,使其在整个应用程序中都可用,并便于处理选择器

请参阅此官方developers.android指南:


考虑在日期和时间选择器中使用DialogFragment类,以使其在整个应用程序中可用,并便于处理选择器

请参阅此官方developers.android指南:


这不是压延视图所必需的。编辑文本是可能的

参考:


这不是压延视图所必需的。编辑文本是可能的

参考:


如果您声明了这个“Picker”变量,并且每次初始化资源都应该在setContentView()方法之后,那么您必须首先初始化资源,然后调用setContentView(),因此更改它声明您的Picker,即Picker=(DatePicker)findViewById(R.id.calendarView1);以及super.onCreate(savedInstanceState)下面的其他变量;setContentView(R.layout.activity_main);如果您已声明此“Picker”变量,并且第二次资源初始化应仅在setContentView()方法之后进行,则您必须首先初始化资源,然后调用setContentView(),因此更改它以声明您的Picker,即Picker=(DatePicker)findViewById(R.id.calendarView1);以及super.onCreate(savedInstanceState)下面的其他变量;setContentView(R.layout.activity_main);
            //Initialize the variables
            Calendar myCalendar = Calendar.getInstance();
            DatePickerDialog.OnDateSetListener date;
            DatePickerDialog d1;



                //To be written in oncreate method

        //define
        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);

        //It is a function that sets the value to the edittext after selection
                updateLabel();
            }
        };


    //onClick listener to the button which open the calendar
    fromDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            d1= new DatePickerDialog(className.this, date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH));
            d1.show();
        }
    });

    //finally the function 
    private void updateLabel() {
        String myFormat = "MM/dd/yy"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    //Display the selection
        fromDateEdit.setText(sdf.format(myCalendar.getTime()));
    }