如何定义“构造函数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));
}
}
Logcat:
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)
答案 0 :(得分:0)
实际上,AlertDialog.Builder的构造函数是
AlertDialog.Builder(Context context)
构造函数使用此构建器的上下文及其创建的AlertDialog。
并且在您的代码this
中引用您的 XMLParser类而不是任何上下文
AlertDialog.Builder builder = new AlertDialog.Builder(this);
而不是传递.this
传递调用活动或应用程序的上下文。
之类的,
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
答案 1 :(得分:0)
如果您有AlertDialog.Builder对象,只需调用show即可。最简单的例子:
new AlertDialog.Builder(someContext).setMessage("Some text").show();
您也可以在设置按钮等之后执行此操作...