我有一个对话框,我用它在活动之间切换(当抛出错误时,排序)。我习惯使用Intent(this, class)
来切换活动,但在DialogBox
内容应该是什么,而不是“this
”。
我的代码:
package com.example.partyorganiser;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
public class Attendance_Dialog extends DialogFragment {
Context context;
public Attendance_Dialog(Context c){
this.context = c;
}
public Dialog onCreateDialog(Bundle savedInstanceState, final Context context) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("NEED TO CREATE A PARTY FIRST: MENU OR ADD A PARTY")
.setPositiveButton("MENU", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent switch_menu = new Intent(context, MenuList.class);
}
})
.setNegativeButton("ADD A PARTY", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent switch_addparty = new Intent(context , AddParty.class);
startActivity(switch_addparty);
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
答案 0 :(得分:1)
将上下文传递给对话框。
public class MyDialog extends Dialog{
Context context;
//Constructor
public MyDialog(Context c){
this.context = c;
}
//Example of using the context
public void doSomethingWithContext(){
Intent i = new Intent(this, MyClass.class);
}
}
//Creating MyDialog and giving it a context
MyDialog md = new MyDialog(this);
md.show();
在片段中,您可以使用getActivity()
从启动片段的活动中获取上下文。