0

「コンストラクター AlertDialog.Builder(XMLParser) は未定義です」を定義するにはどうすればよいですか。コードにエラーがあります。それを機能させる方法を教えてください。アプリで例外がキャッチされたときにアラート ダイアログ ボックスを表示しようとしています。

public class XMLParser {
private Context context;
// constructor


/**
 * Getting XML from URL making HTTP request
 * @param url string
 * */
public String getXmlFromUrl(String url) {
    String xml = null;

    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}

/**
 * Getting XML DOM element
 * @param XML string
 * */
public Document getDomElement(String xml){
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is); 

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e){
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage( "Host not found" )
                    .setCancelable(false)
                    .setPositiveButton("Exit",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    System.exit(0);
                                }

                            });
            AlertDialog alert = builder.create();
            alert.show();



            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }

        return doc;
}

/** Getting node value
  * @param elem element
  */
 public final String getElementValue( Node elem ) {
     Node child;
     if( elem != null){
         if (elem.hasChildNodes()){
             for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                 if( child.getNodeType() == Node.TEXT_NODE  ){
                     return child.getNodeValue();
                 }
             }
         }
     }
     return "";
 }

 /**
  * Getting node value
  * @param Element node
  * @param key string
  * */
 public String getValue(Element item, String str) {     
        NodeList n = item.getElementsByTagName(str);        
        return this.getElementValue(n.item(0));
    }

}

ログキャット:

08-26 22:37:34.173: E/AndroidRuntime(2221): FATAL EXCEPTION: AsyncTask #1
08-26 22:37:34.173: E/AndroidRuntime(2221): java.lang.RuntimeException: An error occured while executing doInBackground()
08-26 22:37:34.173: E/AndroidRuntime(2221):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at java.lang.Thread.run(Thread.java:856)
08-26 22:37:34.173: E/AndroidRuntime(2221): Caused by: java.lang.NullPointerException
08-26 22:37:34.173: E/AndroidRuntime(2221):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at com.example.androidhive.XMLParser.getDomElement(XMLParser.java:80)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at com.example.androidhive.CategoryList$loadlistView.doInBackground(CategoryList.java:112)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at com.example.androidhive.CategoryList$loadlistView.doInBackground(CategoryList.java:1)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-26 22:37:34.173: E/AndroidRuntime(2221):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-26 22:37:34.173: E/AndroidRuntime(2221):     ... 4 more
08-26 22:37:40.399: E/WindowManager(2221): Activity com.example.androidhive.CategoryList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41649130 that was originally added here
08-26 22:37:40.399: E/WindowManager(2221): android.view.WindowLeaked: Activity com.example.androidhive.CategoryList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41649130 that was originally added here
08-26 22:37:40.399: E/WindowManager(2221):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:428)
08-26 22:37:40.399: E/WindowManager(2221):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
08-26 22:37:40.399: E/WindowManager(2221):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
08-26 22:37:40.399: E/WindowManager(2221):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
08-26 22:37:40.399: E/WindowManager(2221):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
08-26 22:37:40.399: E/WindowManager(2221):  at android.app.Dialog.show(Dialog.java:278)
08-26 22:37:40.399: E/WindowManager(2221):  at com.example.androidhive.CategoryList$loadlistView.onPreExecute(CategoryList.java:102)
08-26 22:37:40.399: E/WindowManager(2221):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
08-26 22:37:40.399: E/WindowManager(2221):  at android.os.AsyncTask.execute(AsyncTask.java:511)
08-26 22:37:40.399: E/WindowManager(2221):  at com.example.androidhive.CategoryList.checkInternetConnection(CategoryList.java:158)
08-26 22:37:40.399: E/WindowManager(2221):  at com.example.androidhive.CategoryList.onCreate(CategoryList.java:47)
08-26 22:37:40.399: E/WindowManager(2221):  at android.app.Activity.performCreate(Activity.java:4465)
08-26 22:37:40.399: E/WindowManager(2221):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
08-26 22:37:40.399: E/WindowManager(2221):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
08-26 22:37:40.399: E/WindowManager(2221):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
08-26 22:37:40.399: E/WindowManager(2221):  at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-26 22:37:40.399: E/WindowManager(2221):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
08-26 22:37:40.399: E/WindowManager(2221):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 22:37:40.399: E/WindowManager(2221):  at android.os.Looper.loop(Looper.java:137)
08-26 22:37:40.399: E/WindowManager(2221):  at android.app.ActivityThread.main(ActivityThread.java:4512)
08-26 22:37:40.399: E/WindowManager(2221):  at java.lang.reflect.Method.invokeNative(Native Method)
08-26 22:37:40.399: E/WindowManager(2221):  at java.lang.reflect.Method.invoke(Method.java:511)
08-26 22:37:40.399: E/WindowManager(2221):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
08-26 22:37:40.399: E/WindowManager(2221):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
08-26 22:37:40.399: E/WindowManager(2221):  at dalvik.system.NativeStart.main(Native Method)
4

2 に答える 2

0

実際、AlertDialog.Builder のコンストラクタは

AlertDialog.Builder(Context context)

このビルダーのコンテキストとそれが作成する AlertDialog を使用するコンストラクター。

コードでは、コンテキストではなくXMLParser クラスthisを参照します

AlertDialog.Builder builder = new AlertDialog.Builder(this);

.this渡す代わりに、呼び出し元のアクティビティまたはアプリケーションのコンテキストを渡します。

何かのようなもの、

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
于 2012-08-26T13:43:10.070 に答える
0

AlertDialog.Builder オブジェクトがある場合は、それに対して show を呼び出すだけです。最も簡単な例:

new AlertDialog.Builder(someContext).setMessage("Some text").show();

ボタンを設定した後などに、これを行うこともできます...

于 2012-08-26T13:45:15.757 に答える