ListItem の async Task onClick 内でアラート ダイアログにアクセスしたい。getbaseContext()
以下は、アクティビティ " "、" ClassName.this
"、" "に渡した参照this
です。しかし、何も機能せず、クラッシュしました。何か案が ?
Async.java
public class Async extends 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 & 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)