1

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)

4

7 に答える 7

1

非同期クラスを呼び出す前にコンテキストを初期化します。

于 2013-07-31T10:30:32.540 に答える
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 に答える
0

これを使う代わりに

getBaseContext()

使用する

getAppplicationContext()

また、非同期タスクを実行する前にコンテキストを初期化します

this.context = getAppplicationContext();

そして、実行します

RssFeedTask rssTask = new RssFeedTask();
    rssTask.execute();
于 2013-07-31T10:29:36.207 に答える
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(新しい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() メソッドに入れます。

于 2013-07-31T10:32:24.943 に答える