当我想用2个按钮显示对话框时,我发现了一些错误。我试过以下代码...... 实际上当它只显示吐司时我没有收到错误。
我的arrayadapter.class
package id.mungil.santa;
import java.io.InputStream;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class campaignAdapter extends ArrayAdapter<Campaign> {
ArrayList<Campaign> campaignList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
Context mContext;
public campaignAdapter(Context context, int resource, ArrayList<Campaign> objects){
super(context, resource, objects);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
campaignList = objects;
mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imgAds = (ImageView) v.findViewById(R.id.imgAds);
holder.tTitle = (TextView) v.findViewById(R.id.tTitle);
holder.tShortDEsc = (TextView) v.findViewById(R.id.tDesc);
holder.bPoin = (Button) v.findViewById(R.id.bPoin);
holder.bAction = (Button) v.findViewById(R.id.bAction);
holder.tLongDesc = (TextView) v.findViewById(R.id.tLongdesc);
holder.tIdAds = (TextView) v.findViewById(R.id.tIdAds);
holder.tActionLink = (TextView) v.findViewById(R.id.tActionlink);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.imgAds.setImageResource(R.drawable.ic_launcher);
new DownloadImageTask(holder.imgAds).execute(campaignList.get(position).getImgAds());
holder.tTitle.setText(campaignList.get(position).getTitle());
holder.tShortDEsc.setText(campaignList.get(position).getShortDesc());
holder.bPoin.setText(campaignList.get(position).getPoin());
holder.bAction.setText(campaignList.get(position).getActionTitle());
holder.bAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "test", 1000).show();
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Are you sure you ?");
builder.setMessage("Are you suer you want to remove this item from the cart?");
builder.setPositiveButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
Dialog dd= builder.create();
dd.show();
}
});
holder.tLongDesc.setText(campaignList.get(position).getLongDesc());
holder.tIdAds.setText(campaignList.get(position).getIdAds());
holder.tActionLink.setText(campaignList.get(position).getActionLink());
return v;
}
static class ViewHolder {
public ImageView imgAds;
public TextView tTitle;
public TextView tShortDEsc;
public TextView tLongDesc;
public Button bPoin;
public Button bAction;
public TextView tActionLink;
public TextView tIdAds;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
但我收到以下错误..
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRoot.setView(ViewRoot.java:509)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:241)
at id.mungil.santa.campaignAdapter$1.onClick(campaignAdapter.java:85)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
1)使用此功能或从中获取代码
public void showAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("Your Title");
// Setting Dialog Message
alertDialog.setMessage("Your Message");
// On pressing the Yes button.
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// ToDo here
}
});
// On pressing the No button
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
答案 1 :(得分:0)
不要在
中传递上下文 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
您无法通过非活动的上下文显示对话框。尝试传递有效的活动参考。
答案 2 :(得分:0)
AlertDialog.Builder ad = new AlertDialog.Builder(
getActivity());
ad.setTitle("Error");
ad.setMessage("FinalPolishRate is Empty");
ad.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int button1) {
dialog.cancel();
}
});
AlertDialog alertDialog = ad.create();
alertDialog.show();