我已经创建了一个自定义数组适配器,我有图像视图和onclick的图像视图我正在打开一个Dialogue但它显示我 android.view.WindowManager $ BadTokenException:无法添加窗口 - token null不适用于应用程序 她是我的代码
以下行给出了错误
if (context != null) {
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.spread_dialogue);
dialog.setTitle("New Post");
dialog.show();
}
public class Customfeedadapter extends ArrayAdapter<Rowfeeds> {
Context context;
Rowfeeds feeds;
ViewHolder holder = null;
ImageView imgview, likeimgview;
Vector<String> mfeedid = new Vector<String>();
Vector<String> muserid = new Vector<String>();
public String guestlist;
private ProgressDialog simpleWaitDialog;
public String mssg;
public String likemssg;
Vector<String> mfeedlike = new Vector<String>();
Vector<String> mfeedspread = new Vector<String>();
Vector<String> mfeeddate = new Vector<String>();
Vector<String> mfeedtime = new Vector<String>();
private String likestrng, spreadstng, datestrng, timestrng;
public Customfeedadapter(Context context, int resource, List<Rowfeeds> objects, Vector<String> feedIdvector, Vector<String> userIdvector, Vector<String> feedlikevector, Vector<String> feedspreadvector, Vector<String> feeddatevector, Vector<String> feedtimevector) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
this.context = context;
this.mfeedid = feedIdvector;
this.muserid = userIdvector;
this.mfeedlike = feedlikevector;
this.mfeedspread = feedspreadvector;
this.mfeeddate = feeddatevector;
this.mfeedtime = feedtimevector;
}
private class ViewHolder {
TextView txtTitle;
TextView txtDesc;
TextView feedlike;
TextView feedspread;
TextView feeddate;
TextView feedtime;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// TODO Auto-generated method stub
try {
feeds = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.feeds, null);
holder = new ViewHolder();
// Log.i("imgvectr", ""+mfeedlike.elementAt(position));
likestrng = "" + mfeedlike.elementAt(position);
spreadstng = "" + mfeedspread.elementAt(position);
datestrng = "" + mfeeddate.elementAt(position);
timestrng = "" + mfeedtime.elementAt(position);
holder.txtTitle = (TextView) convertView.findViewById(R.id.textsimple);
holder.txtDesc = (TextView) convertView.findViewById(R.id.showtime);
holder.feedlike = (TextView) convertView.findViewById(R.id.feedlikes);
holder.feedspread = (TextView) convertView.findViewById(R.id.textView1);
holder.feeddate = (TextView) convertView.findViewById(R.id.feeddatetv);
holder.feedtime = (TextView) convertView.findViewById(R.id.feedtime);
} else //Log.v("else", "else");
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtTitle.setTextColor(Color.BLACK);
holder.txtDesc.setTextColor(Color.BLACK);
//Log.v("getMfeedid", ""+feeds.getMfeedid());
holder.txtTitle.setText(feeds.getMsg());
holder.txtDesc.setText("~" + feeds.getUsrname());
holder.feedlike.setText(likestrng);
holder.feedspread.setText(spreadstng + " Spreads");
holder.feeddate.setText(datestrng);
holder.feedtime.setText(timestrng);
} catch (Exception e) {
// TODO Auto-generated catch block
//Log.v("Exception", e.toString());
}
// spread icon
imgview = (ImageView) convertView.findViewById(R.id.imageView2);
imgview.setTag(position);
imgview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//String strng = ""+getItem(position);
try {
//Toast.makeText(context,"hi",Toast.LENGTH_SHORT).show();
if (context != null) {
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.spread_dialogue);
dialog.setTitle("New Post");
dialog.show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
Log.v("Exception", e.toString());
}
/*Log.v("strng", ""+position);
mfeedid.elementAt(position);
Log.v("mfeedid.elementAt(position)", ""+mfeedid.elementAt(position));
String value = ""+mfeedid.elementAt(position);
new SendApproveValue().execute(value.trim());*/
}
});
likeimgview = (ImageView) convertView.findViewById(R.id.imageView1);
likeimgview.setTag(position);
likeimgview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v("strng", "" + position);
mfeedid.elementAt(position);
muserid.elementAt(position);
Log.v("mfeedid.elementAt(position)", "" + mfeedid.elementAt(position));
Log.v("muserid.elementAt(position)", "" + muserid.elementAt(position));
String value = "" + mfeedid.elementAt(position);
String value2 = "" + muserid.elementAt(position);
new SendlikesValue().execute(value.trim(), value2.trim());
}
});
return convertView;
}
private class SendApproveValue extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... param) {
// TODO Auto-generated method stub
Log.v("param", "= " + param[0]);
String approvevalue = "" + param[0];
try {
Log.i("spreadsInBackground", "spreadsInBackground");
HttpClient hc = new DefaultHttpClient();
HttpPost postMethod = new HttpPost("http://192.168.21.74/mynetwork/formapi/spreads");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("feed_id", approvevalue));
//nameValuePairs.add(new BasicNameValuePair("to", "5"));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse rp = hc.execute(postMethod);
if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result3 = EntityUtils.toString(rp.getEntity());
Log.v("spreads_data", "" + result3);
JSONObject jobject = new JSONObject(result3);
Log.v("msg", "" + jobject.getString("msg"));
mssg = "" + jobject.getString("msg");
} else {
Log.d("spreadsrJSON", "StatusCode " + rp.getStatusLine().getStatusCode());
}
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i("Exception", e.toString());
}
return null;
}
@Override
protected void onPreExecute() {
Log.i("Async-Example", "onPreExecute Called");
//simpleWaitDialog = ProgressDialog.show(context,"Wait..", "Sending data");
}
@Override
protected void onPostExecute(String result) {
Log.i("Async-Example", "onPostExecute Called");
Toast.makeText(context, mssg, Toast.LENGTH_SHORT).show();
//Toast.makeText(context,resultt,Toast.LENGTH_SHORT).show();
//RowItem3 item3 = new RowItem3(/*vector.elementAt(i),*/vector);
//simpleWaitDialog.dismiss();
}
}
private class SendlikesValue extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... param) {
// TODO Auto-generated method stub
Log.v("param2", "= " + param[0]);
Log.v("param2", "= " + param[1]);
String approvevalue = "" + param[0];
String approvevalue2 = "" + param[1];
try {
Log.i("SendlikesValue", "SendlikesValue");
HttpClient hc = new DefaultHttpClient();
HttpPost postMethod = new HttpPost("http://192.168.21.74/mynetwork/formapi/likes");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("feed_id", approvevalue));
nameValuePairs.add(new BasicNameValuePair("user_id", approvevalue2));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse rp = hc.execute(postMethod);
if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result3 = EntityUtils.toString(rp.getEntity());
Log.v("likes_data", "" + result3);
JSONObject jobject = new JSONObject(result3);
Log.v("msg", "" + jobject.getString("msg"));
likemssg = "" + jobject.getString("msg");
} else {
Log.d("likesJSON", "StatusCode " + rp.getStatusLine().getStatusCode());
}
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i("likesException", e.toString());
}
return null;
}
@Override
protected void onPreExecute() {
Log.i("Async-Example", "onPreExecute Called");
//simpleWaitDialog = ProgressDialog.show(context,"Wait..", "Sending data");
}
@Override
protected void onPostExecute(String result) {
Log.i("Async-Example", "onPostExecute Called");
Toast.makeText(context, likemssg, Toast.LENGTH_SHORT).show();
//RowItem3 item3 = new RowItem3(/*vector.elementAt(i),*/vector);
//simpleWaitDialog.dismiss();
}
}
}
答案 0 :(得分:17)
假设您要将Activity ActivityABC
的上下文传递给Adapter
。然后在适配器中创建以下变量
Activity mActivity = null;
并在我们的适配器中将此ActivityABC.this
之类的上下文传递给变量mActivity
,然后使用以下方式显示对话框:
Dialog dialog = new Dialog(mActivity);
希望这个技巧适合你