所以我试图从我的工具栏的导航按钮打开一个Dialog,但它没有打开,而只是返回true。
以下是我点击代码的导航:
toolbar_main.setNavigationOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Main.this.runOnUiThread(new Runnable() {
public void run() {
DialogNewAmount dialog = new DialogNewAmount();
dialog.show(getFragmentManager(), "");
}});
}
});
我认为在充气对话框时可能会发生错误
DialogNewAmount类:
public class DialogNewAmount extends DialogFragment {
private MyDatabase database;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
database = MyDatabase.getDatabase(getActivity());
database.isOpen();
//Creating dialog windows using AlertDialog with the name "builder"
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//Giving reference to the layout for this dialog then displaying it
final View dialogView = getActivity().getLayoutInflater().inflate(R.layout.dialog_new_amount, null);
builder.setView(dialogView);
//Getting reference to widgets being displayed in layout
final EditText editA = dialogView.findViewById(R.id.editAmount);
final EditText editD = dialogView.findViewById(R.id.editDescription);
//Labelling the dialog window
builder.setTitle("ADD AMOUNT");
//Handle the cancel button
builder.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dismiss();
}
});
//Handle the Add button
builder.setPositiveButton("ADD",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
String Amount = editA.getText().toString();
String Description = editD.getText().toString();
if (Amount.matches("") || (Description.matches(""))) {
Toast.makeText(getActivity(), "You can't leave any field blank", Toast.LENGTH_LONG).show();
} else {
try {
// Preparing variables
int parsedAmount = Integer.parseInt(editA.getText().toString());
String myString = editD.getText().toString();
//Adding transaction in database
database.transactionDAO().addTransaction
(new Database.Transaction(parsedAmount, myString));
Log.i("Database", "OK");
// Create a new transaction
Transaction newTransaction = new Transaction(parsedAmount, myString);
//Pass new transaction back to main activity
Main.createNewTransaction(newTransaction);
getActivity().runOnUiThread(new Runnable() {
public void run()
{
updateTotalAmount();
}});
//Quit the dialog
dismiss();
Log.i("DialogNewAmount", "OK");
} catch (Error e) {
Log.e("DialogNewAmount", "Fail", e);
}
}
}
}); return builder.create();
}
我也应该发布我的logCat吗?
任何形式的指导都将受到高度赞赏!
答案 0 :(得分:0)
试试这个
修改强>
在您的活动中初始化对话框。
DialogFragment dialog;
toolbar_main.setNavigationOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Main.this.runOnUiThread(new Runnable() {
public void run() {
dialog = (DialogFragment)
dialog.instantiate(getApplicationContext(), DialogNewAmount.class.getName());
dialog.show(getSupportFragmentManager(), "");
}});
}
});
答案 1 :(得分:0)
最后解决了!
问题出现了,因为我在将工具栏设置为支持操作栏之前调用了onClickListener
方法,这就是为什么它从未在logCat
中给出任何致命错误的原因因为我有所有的方法搞砸了。
仍然感谢帮助!