我想在ListItem的异步任务onClick内访问警报对话框。以下是我传给活动“getbaseContext()
”,“ClassName.this
”,“this
”的参考资料;但没有任何效果,它会崩溃。有什么想法吗?
Async.java
公共类Async扩展了Activity {
/** Called when the activity is first created. */
ListView _rssFeedListView;
List<JSONObject> jobs;
List<RssFeedStructure> rssStr;
private BlogAdapter _adapter;
TextView textview;
Context context;
AlertDialog.Builder builder
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
//context = getApplicationContext();
_rssFeedListView = (ListView) findViewById(R.id.rssfeed_listview);
builder = new AlertDialog.Builder(getApplicationContext());
textview = (TextView) findViewById(R.id.loading);
RssFeedTask rssTask = new RssFeedTask();
rssTask.execute();
_rssFeedListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
// builder.setCancelable(true);
builder.setTitle("Invitation sent unsuccesful.Please try again !");
// builder.setInverseBackgroundForced(true);
builder.setNegativeButton(
"Ok",null
);
builder.show();
}
});
}
private class RssFeedTask extends AsyncTask<String, Void, String> {
// private String Content;
// private ProgressDialog Dialog;
String response = "";
AlertDialog.Builder alert;
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... urls) {
try {
String feed = "http://url";
XmlHandler rh = new XmlHandler();
rssStr = rh.getLatestArticles(feed);
} catch (Exception e) {
}
return response;
}
@Override
protected void onPostExecute(String result) {
if (rssStr != null) {
_adapter = new BlogAdapter(Async.this, rssStr);
_rssFeedListView.setAdapter(_adapter);
textview.setVisibility(View.INVISIBLE);
}
}
}
}
错误:
07-31 16:45:56.615: E/AndroidRuntime(28575): FATAL EXCEPTION: main
07-31 16:45:56.615: E/AndroidRuntime(28575): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.view.ViewRootImpl.setView(ViewRootImpl.java:712)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:346)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.app.Dialog.show(Dialog.java:277)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
07-31 16:45:56.615: E/AndroidRuntime(28575): at com.itcuties.multicategoryrssreader.BlogFeeds$1.onItemClick(BlogFeeds.java:86)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.widget.AbsListView.performItemClick(AbsListView.java:1283)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3074)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.widget.AbsListView$1.run(AbsListView.java:4147)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.os.Handler.handleCallback(Handler.java:615)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.os.Looper.loop(Looper.java:137)
07-31 16:45:56.615: E/AndroidRuntime(28575): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-31 16:45:56.615: E/AndroidRuntime(28575): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 16:45:56.615: E/AndroidRuntime(28575): at java.lang.reflect.Method.invoke(Method.java:511)
07-31 16:45:56.615: E/AndroidRuntime(28575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-31 16:45:56.615: E/AndroidRuntime(28575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-31 16:45:56.615: E/AndroidRuntime(28575): at dalvik.system.NativeStart.main(Native Method)
Manish&amp; Raji的建议:
07-31 17:53:35.210: E/AndroidRuntime(8442): FATAL EXCEPTION: main
07-31 17:53:35.210: E/AndroidRuntime(8442): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@41bdb010 is not valid; is your activity running?
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.view.ViewRootImpl.setView(ViewRootImpl.java:708)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:346)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.view.Window$LocalWindowManager.addView(Window.java:554)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.app.Dialog.show(Dialog.java:277)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
07-31 17:53:35.210: E/AndroidRuntime(8442): at com.itcuties.multicategoryrssreader.BlogFeeds$1.onItemClick(BlogFeeds.java:78)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.widget.AbsListView.performItemClick(AbsListView.java:1283)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3074)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.widget.AbsListView$1.run(AbsListView.java:4147)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.os.Handler.handleCallback(Handler.java:615)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.os.Looper.loop(Looper.java:137)
07-31 17:53:35.210: E/AndroidRuntime(8442): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-31 17:53:35.210: E/AndroidRuntime(8442): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 17:53:35.210: E/AndroidRuntime(8442): at java.lang.reflect.Method.invoke(Method.java:511)
07-31 17:53:35.210: E/AndroidRuntime(8442): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-31 17:53:35.210: E/AndroidRuntime(8442): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-31 17:53:35.210: E/AndroidRuntime(8442): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
在调用异步类之前初始化上下文。
答案 1 :(得分:0)
而不是使用此
getBaseContext()
使用
getAppplicationContext()
并且在执行Async Task之前初始化上下文,如
this.context = getAppplicationContext();
然后执行
RssFeedTask rssTask = new RssFeedTask();
rssTask.execute();
答案 2 :(得分:0)
我在AsyncTask
onPostExecute()
中使用了以下代码并且工作正常,
AlertDialog.Builder builder = new AlertDialog.Builder(
FacebookFriends.this);
builder.setCancelable(true);
builder.setTitle("Invitation sent unsuccesful.Please try again !");
builder.setInverseBackgroundForced(true);
builder.setNegativeButton(
"Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
答案 3 :(得分:0)
公共类Async扩展了Activity {
/** Called when the activity is first created. */
ListView _rssFeedListView;
List<JSONObject> jobs;
List<RssFeedStructure> rssStr;
private BlogAdapter _adapter;
TextView textview;
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
_rssFeedListView = (ListView) findViewById(R.id.rssfeed_listview);
textview = (TextView) findViewById(R.id.loading);
RssFeedTask rssTask = new RssFeedTask();
rssTask.execute();
this.context = getBaseContext();
_rssFeedListView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
alert = new AlertDialog.Builder(getBaseContext());
alert.setTitle("Title here");
WebView wv = new WebView(getBaseContext());
wv.loadUrl("http://www.google.com");
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(
WebView view, String url) {
view.loadUrl(url);
return true;
}
});
alert.setView(wv);
alert.setNegativeButton("Close",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
alert.show();
}
});
}
private class RssFeedTask extends AsyncTask<String, Void, String> {
// private String Content;
// private ProgressDialog Dialog;
String response = "";
AlertDialog.Builder alert;
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... urls) {
try {
// String feed =
// "https://www.facebook.com/feeds/page.php?format=atom10&id=330938752535";
String feed = "http://srisriravishankar.org/feed/";
XmlHandler rh = new XmlHandler();
rssStr = rh.getLatestArticles(feed);
} catch (Exception e) {
}
return response;
}
@Override
protected void onPostExecute(String result) {
if (rssStr != null) {
_adapter = new BlogAdapter(Async.this, rssStr);
_rssFeedListView.setAdapter(_adapter);
textview.setVisibility(View.INVISIBLE);
}
}
}
}
将onItemClickListner放在onCreate()方法中..
答案 4 :(得分:0)
尝试运行您的AsyncTask,而不是来自onCreate
活动的方法,而是来自onResume
。或者甚至在onResume
之后,使用例如处理程序。像这样:
Handler handler = new Handler();
Runnable r = new Runnable(){
public void run() {
/*your asyncTask execution code here*/
}
};
@Override
public void onResume() {
super.onResume;
handler.post(r);
}
有了这个,你将确保活动真正活跃并完成初始化过程。
答案 5 :(得分:0)
创建异步任务的构造函数并在异步任务中传递上下文。
请参阅以下示例
private class asyncTask extends AsyncTask<Void, Void, Cursor>
{
Context mContext;
asyncTask(Context context)
{
mContext = context
}
protected void onPreExecute()
{
}
protected void onPostExecute(Cursor cursor)
{
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setCancelable(true);
builder.setTitle("Invitation sent unsuccesful.Please try again !");
builder.setInverseBackgroundForced(true);
builder.setNegativeButton(
"Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
@Override
protected Cursor doInBackground(Void... params)
{
}
}
在此之后,您可以将此asynctask称为
new asyncTask(Your_Context).execute();
答案 6 :(得分:0)
这将有效:
像这样创建Popupwindow
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.new_popup_layout, null);
final PopupWindow popupWindow = new PopupWindow(
layout,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
WebView web = (WebView)layout.findViewbyId(R.id.webView1);
popupWindow.showAtLocation(view,Gravity.CENTER,0,0);
并使用webview创建new_popup_layout布局文件,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="461dp" />