Android版的DatePicker在Android

Android版的DatePicker在Android

本文介绍了Android版的DatePicker在Android 2.3.3失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是新来的Andr​​oid开发我想显示在Android 2.3.3模拟器的DatePicker对话框,但是当它崩溃。我跟着我缺少什么在我的code到包括哪些内容?但是,这code是工作在Android 4.0的。
DatePickerFragment类

 公共类DatePickerFragment扩展DialogFragment实现OnDateSetListener { @覆盖
    公共对话框onCreateDialog(捆绑savedInstanceState){
        //使用当前日期作为选择器的默认日期
        最后的日历C = Calendar.getInstance();
        年整型= c.get(Calendar.YEAR);
        INT月= c.get(的Calendar.MONTH);
        INT天= c.get(Calendar.DAY_OF_MONTH);        //创建DatePickerDialog的新实例,并将其返回
        返回新DatePickerDialog(getActivity(),这,年,月,日);
    }    公共无效onDateSet(查看的DatePicker,年整型,诠释月,日整型){
        //做一些与用户所选择的日期
    }

}

这是启动的DatePicker对话活动类:

 进口android.os.Bundle;
进口android.annotation.Sup pressLint;
进口android.app.Activity;
进口android.app.DialogFragment;进口android.support.v4.app.FragmentActivity;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
@燮pressLint({NewApi,NewApi,NewApi})
公共类FormAnalysis扩展FragmentActivity {@覆盖公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.fanalysis);
    //getActionBar().setDisplayHomeAsUpEnabled(true);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    。getMenuInflater()膨胀(R.menu.fanalysis,菜单);
    返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //开关(item.getItemId()){
      //情况android.R.id.home:
        // NavUtils.navigateUpFromSameTask(本);
            返回true;
    //}
    //返回super.onOptionsItemSelected(项目);
}公共无效生成(查看视图){
}
  公共无效startDialog(查看视图){
  DialogFragment newFragment =新DatePickerFragment();
    newFragment.show(getSupportFragmentManager(),日期选择器);}}

更新
这里是堆栈跟踪

[2012年11月16日零时31分55秒 - 粮食安全] ActivityManager:开始:意向{行动= android.intent.action.MAIN猫= [android.intent.category.LAUNCHER] CMP = kz.bimash.food.security /.Starter}
[2012年11月16日零时31分55秒 - 粮食安全] ActivityManager:警告:活动尚未开始,目前的任务已被带到前面


解决方案

您DialogFragment进口是类的版本不正确。它需要android.support.v4.app.DialogFragment代替android.app.DialogFragment,否则将无法在2.3正常工作。

As I am new to Android development I am trying to display DatePicker dialog on Android 2.3.3 emulator,but while it is crashing. I am followed what am I missing to include in my code? But this code is working in Android 4.0.DatePickerFragment class

public class DatePickerFragment extends DialogFragment implements 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);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
    }

}

The activity class that starts the DatePicker Dialog:

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DialogFragment;

import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


@SuppressLint({ "NewApi", "NewApi", "NewApi" })
public class FormAnalysis extends FragmentActivity{

@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fanalysis);
    //getActionBar().setDisplayHomeAsUpEnabled(true);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.fanalysis, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //switch (item.getItemId()) {
      //  case android.R.id.home:
        //    NavUtils.navigateUpFromSameTask(this);
            return true;
    //}
    //return super.onOptionsItemSelected(item);
}

public void generate(View view){


}
  public void startDialog(View view){
  DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");} }

UpdateHere is the Stack Trace


[2012-11-16 00:31:55 - Food Security] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=kz.bimash.food.security/.Starter }
[2012-11-16 00:31:55 - Food Security] ActivityManager: Warning: Activity not started, its current task has been brought to the front
解决方案

Your DialogFragment import is for the incorrect version of the class. It needs to be android.support.v4.app.DialogFragment instead of android.app.DialogFragment, otherwise it will not work correctly on 2.3.

这篇关于Android版的DatePicker在Android 2.3.3失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:51