2

由于我是 Android 开发的新手,我正在尝试在 Android 2.3.3 模拟器上显示 DatePicker 对话框,但它正在崩溃。我被跟踪了我在我的代码中缺少什么?但是这段代码在 Android 4.0 中运行。DatePickerFragment 类

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
    }

}

启动 DatePicker 对话框的活动类:

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");} }

更新 这里是堆栈跟踪

[2012-11-16 00:31:55 - 食品安全] ActivityManager:开始:Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=kz.bimash.food.security /.启动器 }
[2012-11-16 00:31:55 - 食品安全] ActivityManager:警告:活动未启动,其当前任务已被置于最前面
4

1 回答 1

5

您的 DialogFragment 导入适用于该类的不正确版本。它需要是 android.support.v4.app.DialogFragment 而不是 android.app.DialogFragment,否则在 2.3 上将无法正常工作。

于 2012-11-15T18:46:42.017 回答