简体   繁体   中英

Date Picker is not working in AVD Android 2.2 API 8

I have taken the example Date Picker from http://developer.android.com/guide/topics/ui/controls/pickers.html and it is running fine when AVD setting are as follows

Target platform 4.22 and API Level 17 . But when I am testing the same code with AVD setting as follows it is crashing

Target platform 2.2 and API Level 8

My code looks like this

MainActivity.java

package com.example.datepicker;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new ShowCalendar();
        newFragment.show(getFragmentManager(), "datePicker");
    }

    @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;
    }

}

ShowCalendar.java

package com.example.datepicker;

import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.DatePicker;

@SuppressLint("NewApi")
public class ShowCalendar 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);
    }

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

}   

Please note that I have treid to replace the Activity class with FragmentActivity class in MainActivity.java as it is hinted in the example. But the still the same issue. Where I am going wrong?

Import

import android.support.v4.app.DialogFragment;

instead of

import android.app.DialogFragment;

If you want to use Fragment below API 11 you need to import android.support.v4.app which provides backward compatibility

Use following steps-

1.Change the Activity to extend the FragmentActivity class

2.Change the call to the getFragmentManager() method to the getSupportFragmentManager() method

3.Organize or update the imports

Source-

http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM