我无法显示Dialog,我尝试但返回空指针。 问题,我认为是背景,我尝试过这个,但获得相同的结果...... 我认为另一种可能性是打开一个线程并在Main Activity里面显示AlertDialog,这是我的代码:
文件Sector1.java 公共类Sector1扩展了Fragment {
private Button btnNew_order,btnAggiungi;
private Spinner spin_prodotto, spin_tipo;
private EditText EtQta;
ListView userList;
static UserCustomAdapter userAdapter;
ArrayList<User> userArray = new ArrayList<User>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sector1, container, false);
userAdapter = new UserCustomAdapter(getActivity().getBaseContext(), R.layout.row, userArray);
userList = (ListView) rootView.findViewById(R.id.listView);
userList.setItemsCanFocus(false);
userAdapter.notifyDataSetChanged();
userList.setAdapter(userAdapter);
UserCustomAdapter.java
public class UserCustomAdapter extends ArrayAdapter<User> {
private static final int INVISIBLE = 4;
private static final int VISIBLE = 0;
Context context;
int layoutResourceId;
ArrayList<User> data = new ArrayList<User>();
public UserCustomAdapter(Context context, int layoutResourceId, ArrayList<User> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
System.out.println("@@@-getView-@@@");
if (row == null) {
//LayoutInflater inflater = ((Activity) context).getLayoutInflater();
LayoutInflater inflater = LayoutInflater.from(context);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.textName = (TextView) row.findViewById(R.id.tv_prodotto);
holder.textQta = (TextView) row.findViewById(R.id.tv_qta);
//holder.textLocation = (TextView) row.findViewById(R.id.textView3);
holder.btnEdit = (ImageButton) row.findViewById(R.id.button1);
holder.btnDelete = (ImageButton) row.findViewById(R.id.button2);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
User user = data.get(position);
holder.Useritem = data.get(position);
holder.textName.setText(user.getName());
holder.textQta.setText(user.getQta());
//holder.textLocation.setText(user.getLocation());
holder.btnDelete.setTag(holder.Useritem);// mi segno la posizione in modo da recuperarla poi in seguito!!
holder.btnEdit.setTag(holder.Useritem);// mi segno la posizione in modo da recuperarla poi in seguito!!
//rendo invisibili i pulsanti per la prima riga
if(position == 0){
holder.btnDelete.setVisibility(INVISIBLE);
holder.btnEdit.setVisibility(INVISIBLE);
}else{
holder.btnDelete.setVisibility(VISIBLE);
holder.btnEdit.setVisibility(VISIBLE);
}
holder.btnEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Edit Button Clicked", "**********");
showDialog();
}
});
holder.btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Delete Button Clicked", "**********");
Toast.makeText(context, "Delete item",
Toast.LENGTH_LONG).show();
User itemToRemove = (User)v.getTag();
Sector1.userAdapter.remove(itemToRemove);
Sector1.userAdapter.notifyDataSetChanged();
}
});
return row;
}
private void showDialog(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
AlertDialog dialog = alertDialog.create();
alertDialog.setTitle("Conformation");
alertDialog.setMessage("Are you sure you want to do ???");
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.show();
}
static class UserHolder {
TextView textName;
TextView textQta;
//TextView textLocation;
ImageButton btnEdit;
ImageButton btnDelete;
User Useritem;
}
}
NoticeDialogFragment.java
公共类NoticeDialogFragment扩展了DialogFragment {
/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
// Use this instance of the interface to deliver action events
NoticeDialogListener mListener;
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Build the dialog and set up the button click handlers
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("ciaoo")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
}
});
return builder.create();
}
}
MainActivity.java上的
public void showNoticeDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new NoticeDialogFragment();
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the NoticeDialogFragment.NoticeDialogListener interface
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
// User touched the dialog's positive button
Toast.makeText(this, "Positive button Clicked",
Toast.LENGTH_LONG).show();
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
// User touched the dialog's negative button
Toast.makeText(this, "Negative button Clicked",
Toast.LENGTH_LONG).show();
}
我想通过调用 showNoticeDialog()来实现Dialog的第二个版本,如果我在MainActivity上调用此函数一切正常,但是如何在上面的getView上调用此函数?
这是我对第一个对话框的日志:
10-10 19:41:49.206: D/MainActivity(9150): MainActivity.onCreate
10-10 19:41:49.218: D/MainActivity(9150): MainActivity.onStart
10-10 19:41:49.218: D/MainActivity(9150): MainActivity.onResume
10-10 19:41:49.278: W/EGL_emulation(9150): eglSurfaceAttrib not implemented
....
10-10 19:41:52.914: I/Edit Button Clicked(9150): **********
10-10 19:41:52.914: D/AndroidRuntime(9150): Shutting down VM
10-10 19:41:52.918: W/dalvikvm(9150): threadid=1: thread exiting with uncaught exception (group=0xa622c288)
10-10 19:41:52.918: E/AndroidRuntime(9150): FATAL EXCEPTION: main
10-10 19:41:52.918: E/AndroidRuntime(9150): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
10-10 19:41:52.918: E/AndroidRuntime(9150): at android.view.ViewRootImpl.setView(ViewRootImpl.java:589)
10-10 19:41:52.918: E/AndroidRuntime(9150): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
10-10 19:41:52.918: E/AndroidRuntime(9150): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
10-10 19:41:52.918: E/AndroidRuntime(9150): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
10-10 19:41:52.918: E/AndroidRuntime(9150): at android.app.Dialog.show(Dialog.java:277)
10-10 19:41:52.918: E/AndroidRuntime(9150): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
10-10 19:41:52.918: E/AndroidRuntime(9150): at com.bandweb.ordinifornitori.UserCustomAdapter.showDialog(UserCustomAdapter.java:148)
10-10 19:41:52.918: E/AndroidRuntime(9150): at com.bandweb.ordinifornitori.UserCustomAdapter.access$0(UserCustomAdapter.java:142)
10-10 19:41:52.918: E/AndroidRuntime(9150): at com.bandweb.ordinifornitori.UserCustomAdapter$1.onClick(UserCustomAdapter.java:121)
10-10 19:41:52.918: E/AndroidRuntime(9150): at android.view.View.performClick(View.java:4084)
10-10 19:41:52.918: E/AndroidRuntime(9150): at android.view.View$PerformClick.run(View.java:16966)
10-10 19:41:52.918: E/AndroidRuntime(9150): at android.os.Handler.handleCallback(Handler.java:615)
10-10 19:41:52.918: E/AndroidRuntime(9150): at android.os.Handler.dispatchMessage(Handler.java:92)
10-10 19:41:52.918: E/AndroidRuntime(9150): at android.os.Looper.loop(Looper.java:137)
10-10 19:41:52.918: E/AndroidRuntime(9150): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-10 19:41:52.918: E/AndroidRuntime(9150): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 19:41:52.918: E/AndroidRuntime(9150): at java.lang.reflect.Method.invoke(Method.java:511)
10-10 19:41:52.918: E/AndroidRuntime(9150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-10 19:41:52.918: E/AndroidRuntime(9150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-10 19:41:52.918: E/AndroidRuntime(9150): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:5)
您尚未显示alertdialog。试试吧
AlertDialog dialog;
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Conformation");
alertDialog.setMessage("Are you sure you want to do ???");
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
dialog = alertDialog .create();
dialog .show();
您只需要显示alertdialog,而不是构建器。在这里,您使用通过构造函数初始化的完美上下文。快乐的编程..
答案 1 :(得分:1)
您是否尝试发送使用上下文的活动?
另一方面,你应该提取类似下面的方法。
private void showDialog(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
AlertDialog dialog = alertDialog.create();
alertDialog.setTitle("Conformation");
alertDialog.setMessage("Are you sure you want to do ???");
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.show();
}
答案 2 :(得分:0)
你必须更换
alertDialog.setTitle("Conformation");
alertDialog.setMessage("Are you sure you want to do ???");
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.show();
与
dialog.setTitle("Conformation");
dialog.setMessage("Are you sure you want to do ???");
dialog.setIcon(android.R.drawable.ic_dialog_alert);
dialog.show();