由于我是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 - 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
答案 0 :(得分:5)
您的DialogFragment导入是针对该类的错误版本。它需要是android.support.v4.app.DialogFragment而不是android.app.DialogFragment,否则它将无法在2.3上正常工作。