1

我想访问 ListItem 的异步任务 onClick 中的警报对话框。以下是我传递给活动“ getbaseContext()”、“ ClassName.this”、“ this”的引用;但没有任何效果,它会崩溃。任何想法 ?

异步.java

公共类异步扩展活动{

/** 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)

4

7 回答 7

1

在调用异步类之前初始化上下文。

于 2013-07-31T10:30:32.540 回答
0

尝试不是从onCreate您的活动方法运行您的 AsyncTask,而是从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);
}

有了这个,您将确定活动确实处于活动状态并完成了它的初始化过程。

于 2013-07-31T11:23:09.077 回答
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();
于 2013-07-31T10:31:21.233 回答
0

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;


@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);



        }


    }

}

} 

put your onItemClickListner in onCreate() Method..

于 2013-07-31T10:32:24.943 回答
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) 
        {

        }
    }

在此之后,您可以将此异步任务称为

new asyncTask(Your_Context).execute(); 
于 2013-07-31T11:27:06.170 回答
0

而不是使用这个

getBaseContext()

利用

getAppplicationContext()

并且还在执行异步任务之前初始化上下文,例如

this.context = getAppplicationContext();

然后执行

RssFeedTask rssTask = new RssFeedTask();
    rssTask.execute();
于 2013-07-31T10:29:36.207 回答
0

这将起作用:

像这样创建弹出窗口

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" />

于 2013-08-01T09:38:34.363 回答