几个月前,我曾经和android一起工作(主要是为了娱乐),但我记不太清了。我想问一下如何从自定义对话框开始新的片段。我正在尝试,但出现错误。 我没用过很多片段,现在当我重新使用它时,我记不太清了,所以我不确定这是我的愚蠢错误,还是真正的问题。 片段甚至第一次都没有开始,所以我什至无法检查它在情侣使用中的表现
09-13 11:10:00.201 22975-22975/com.example.damianadamski.Service E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.damianadamski.Service, PID: 22975
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2114)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:637)
at com.example.damianadamski.service.ProjectStuff.CustomDialog$1.onDateSet(CustomDialog.java:138)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:171)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:177)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
这就是整个代码:
public class CustomDialog extends Dialog implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
public static final String TAG = "CustomDialog";
Context context;
TextView accept, cancel;
ImageView imageView;
CheckBox acceptCheck, cancelCheck;
SharedPreferences prefs;
Intent intent;
String IdHolder, streetHolder, cityHolder;
AppCompatActivity activity = new AppCompatActivity();
public CustomDialog(@NonNull Context context) {
super(context);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
intent = new Intent(context, taskActivity.class);
prefs = context.getSharedPreferences("DATA", Activity.MODE_PRIVATE);
accept = (TextView)findViewById(R.id.dialogButtonCancel);
cancel = (TextView)findViewById(R.id.dialogButtonOK);
imageView = (ImageView)findViewById(R.id.dialogButtonDownload);
acceptCheck = (CheckBox)findViewById(R.id.accceptCheck);
cancelCheck = (CheckBox)findViewById(R.id.cancelCheck);
accept.setOnClickListener(this);
cancel.setOnClickListener(this);
accept.setOnClickListener(this);
acceptCheck.setOnClickListener(this);
cancelCheck.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == acceptCheck.getId()){
if(acceptCheck.isChecked()) {
cancel.setClickable(false);
cancelCheck.setChecked(false);
cancel.setTextColor(v.getResources().getColor(R.color.ocean));
accept.setTextColor(v.getResources().getColor(R.color.forestGreen));
accept.setClickable(true);
}else{
accept.setTextColor(v.getResources().getColor(R.color.ocean));
accept.setClickable(false);
}
}
if(v.getId() == cancelCheck.getId()){
if(cancelCheck.isChecked()) {
accept.setClickable(false);
acceptCheck.setChecked(false);
accept.setTextColor(v.getResources().getColor(R.color.ocean));
cancel.setTextColor(v.getResources().getColor(R.color.cornellRed));
cancel.setClickable(true);
}else{
cancel.setTextColor(v.getResources().getColor(R.color.ocean));
cancel.setClickable(false);
}
}
if(v.getId() == accept.getId()){
DatePickerDialog dialog = new DatePickerDialog(getContext());
dialog.show();
dialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
SharedPreferences.Editor issueEditor = prefs.edit();
String id = prefs.getString("id", "");issueEditor.putInt("year", year);
if(id.equalsIgnoreCase("")){
Log.e(TAG, "ID puste, dane nie dotarły SharedPreferences");
Toast.makeText(context, "Błąd przesyłania danych", Toast.LENGTH_LONG);
}else{
Bundle bundle = new Bundle();
TaskFragment taskFragment = new TaskFragment();
bundle.putString("id", id);
bundle.putString("street", prefs.getString("street", ""));
bundle.putString("city", prefs.getString("city", ""));
bundle.putInt("year", year);
bundle.putInt("month", month);
bundle.putInt("day", dayOfMonth);
taskFragment.setArguments(bundle);
//FragmentManager fm = activity.getSupportFragmentManager();
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.add(R.id.mainActivity, taskFragment, "TaskFragment");
transaction.addToBackStack(null);
//transaction.replace(R.id.taskFragment, taskFragment);
transaction.commit();
}
}
});
//new dnew ialog with datepicker
}
if(v.getId() == cancel.getId()){
dismiss();
}
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
}
}