在主活动是一个 Jmeter 板,有tablayout和viewpager,并有图标菜单点击和工作良好,但问题是当显示对话框内的片段。如果显示对话框不在片段(正常活动)的代码工作良好,但我的代码不工作在片段内。我尝试了许多建议,从这个论坛,但仍然不知道如何解决这个问题。
下面是我的Fragment的代码:
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
public class ScanTabFragment extends Fragment implements View.OnClickListener, ProjectDialogActivity.DialogListener{
private CardView cardViewCreateProjectBatch;
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_scan_tab, container, false);
cardViewCreateProjectBatch = view.findViewById(R.id.cardViewCreateProjectBatch);
cardViewCreateProjectBatch.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
Intent i;
switch (view.getId()) {
case R.id.cardViewCreateProjectBatch:
newProjectDialog();
break;
}
}
private void newProjectDialog() {
String projectNameAuto;
projectNameAuto = "PJ-Batch-" + android.text.format.DateFormat.format("yyyy.MM.dd.kk.mm.ss", new java.util.Date());
FragmentManager fm = getSupportFragmentManager(); //this not working in fragment but working in normal activity (not fragment) and i have tried other code like this below but app crashed and dialog not show
//FragmentManager fm = getActivity().getSupportFragmentManager();
//FragmentManager fm = getActivity().getSupportFragmentManager();
//FragmentManager fm = this.getParentFragment().getChildFragmentManager();
//FragmentManager fm = this.getParentFragment().getFragmentManager();
//FragmentManager fm = this.getParentFragment().getFragmentManager().beginTransaction();
//FragmentManager fm = this.getChildFragmentManager();
//FragmentManager fm = getFragmentManager();
//FragmentManager fm = getParentFragment();
ProjectDialogActivity alertDialog = ProjectDialogActivity.newInstance("Create New Batch Project", projectNameAuto, "");
alertDialog.setCancelable(false);
alertDialog.show(fm, "fragment_alert");
ProjectDialogActivity alertDialog = ProjectDialogActivity.newInstance("Create New Batch Project", projectNameAuto, "");
alertDialog.setCancelable(false);
alertDialog.show(fm, "fragment_alert");
}
public long insertProject1(String project_name, String created_date, Integer status, String notes) {
mySQLiteAdapterBatch = new DatabaseHelperBatch(view.getContext());
SQLiteDatabase db = mySQLiteAdapterBatch.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("project_name", project_name);
contentValues.put("created_date", created_date);
contentValues.put("status", status);
contentValues.put("notes", notes);
return db.insert("project1", null, contentValues);
}
public long insertProject2(String project_name, String created_date, Integer status, String notes) {
mySQLiteAdapterBatch = new DatabaseHelperBatch(view.getContext());
SQLiteDatabase db = mySQLiteAdapterBatch.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("project_name", project_name);
contentValues.put("created_date", created_date);
contentValues.put("status", status);
contentValues.put("notes", notes);
return db.insert("project2", null, contentValues);
}
@Override
public void applyTexts(String project_name, String created_date, Integer status, String notes) {
if (ProjectDialogActivity.radioButtonProject1.isChecked() == true) {
insertProject1(project_name, created_date, status, notes);
Toast.makeText(view.getContext(), "New Batch project1 has been created", Toast.LENGTH_LONG).show();
;
}
if (ProjectDialogActivity.radioButtonProject2.isChecked() == true) {
insertProject2(project_name, created_date, status, notes);
Toast.makeText(view.getContext(), "New Batch project2 has been created", Toast.LENGTH_LONG).show();
}
}
}
下面是对话框片段的代码:
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDialogFragment;
public class ProjectDialogActivity extends AppCompatDialogFragment {
//Variables
private String projectName;
private String projectCreated;
private String projectNotes;
private Integer projectStatus;
private TextView textViewProjectName;
private TextView textViewNotes;
public static RadioButton radioButtonProject1;
public static RadioButton radioButtonProject2;
public static ProjectDialogActivity newInstance(String title, String getProjectName, String getNotes) {
ProjectDialogActivity frag = new ProjectDialogActivity();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("inputProjectName", getProjectName);
args.putString("inputNotes", getNotes);
frag.setArguments(args);
return frag;
}
private ProjectDialogActivity.DialogListener listener;
//private EditText editTextRack;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
String title = getArguments().getString("title");
String inputProjectName = getArguments().getString("inputProjectName");
String inputNotes = getArguments().getString("inputNotes");
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.activity_projectdialog, null);
radioButtonProject1 = view.findViewById(R.id.radioButtonScan1);
radioButtonProject2 = view.findViewById(R.id.radioButtonScan2);
textViewProjectName = view.findViewById(R.id.editTextProjectName);
textViewNotes = view.findViewById(R.id.editTextNotes);
textViewProjectName.setSingleLine(true);
textViewNotes.setSingleLine(true);
textViewProjectName.setText(inputProjectName);
textViewNotes.setText(inputNotes);
projectName = textViewProjectName.getText().toString();
builder.setView(view)
.setTitle(title)
//.setView(input)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialoginterface, int i) {
dialoginterface.cancel();
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//String Rack = editTextRack.getText().toString();
//String projectName = textProject.getText().toString();
////listener.applyTexts(textProject.getText().toString(), itemfrloc, itemtoloc, textNotes.getText().toString());
//String answer="OK";
//listener.applyOK_insert(answer);
}
});
return builder.create();
}
@Override
public void onResume() {
super.onResume();
final AlertDialog d = (AlertDialog) getDialog();
if (d != null) {
Button positiveButton = (Button) d.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
projectName = textViewProjectName.getText().toString();
projectCreated = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
projectStatus = 0;
projectNotes = textViewNotes.getText().toString();
Boolean wantToCloseDialog;
//Do stuff, possibly set wantToCloseDialog to true then...
if (projectName.isEmpty()) {
Toast.makeText(getContext(), "Cannot left blank, Please input your project name", Toast.LENGTH_LONG).show();
textViewProjectName.requestFocus();
wantToCloseDialog = false;
} else {
wantToCloseDialog = true;
}
if (wantToCloseDialog == true) {
listener.applyTexts(projectName, projectCreated, projectStatus, projectNotes);
d.dismiss();
}
//else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
}
});
}
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
listener = (ProjectDialogActivity.DialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement DialogListener");
}
}
public interface DialogListener {
// This is called when the dialog is completed and the results have been passed
void applyTexts(String project_name, String created_date, Integer status, String Notes);
}
}
2条答案
按热度按时间jecbmhm31#
请尝试
getParentFragmentManager()
或getFragmentManager()
i2loujxw2#
应用程序崩溃/停止不是因为片段管理器,因此如果写入:
然后在下面写上:
在其他类中:公共类ProjectDialogActivity扩展了AppCompatDialogFragment,不需要在onAttach或onAttachFragment中编写侦听器,但需要在其中编写侦听器:
我第一次想到这个问题是因为片段管理器,不知道如何在片段中调用片段管理器的正确代码,其他用户也给出了一些如何调用片段管理器的正确方法的线索,但仍然错误,因为侦听器为空,这就是错误发生的原因。