0

在我的应用程序中,有时会出现 java.lang.ExceptionInInitializerError 是否有任何解决方案我无法解决这个问题。

代码:

        int nvail = 0;
        try {
            nvail = new APICALL(ctx).execute(Modules.serverCheckLink).get();
        } catch (Exception e) {
            e.printStackTrace();
        }

这是我的 APICALL 课程:(已编辑)

public class APICALL extends AsyncTask<String, Long, Integer> {
private ProgressDialog loading;
Context context;

public APICALL(Context ctx) {
    context = ctx;
}

protected void onPreExecute() {
    loading = ProgressDialog.show(context, null, "Please wait..");
    super.onPreExecute();
}

protected void onPostExecute(Integer result) {
    if (loading != null && loading.isShowing()) {
        loading.dismiss();
    }
    super.onPostExecute(result);
}

@Override
protected Integer doInBackground(String... params) {
    Activity act = (Activity) context;
    NetworkInfo info = ((ConnectivityManager) act
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();

    if (info == null || !info.isConnected())
        return 0;
    else {
        try {

            HttpPost httpPost = new HttpPost(params[0].replaceAll(" ",
                    "%20"));

            HttpParams httpParameters = new BasicHttpParams();
            int timeoutConnection = 5000;
            HttpConnectionParams.setConnectionTimeout(httpParameters,
                    timeoutConnection);
            int timeoutSocket = 5000;
            HttpConnectionParams
                    .setSoTimeout(httpParameters, timeoutSocket);

            DefaultHttpClient httpClient = new DefaultHttpClient(
                    httpParameters);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            String Response = EntityUtils.toString(httpEntity);

            if (Response.equalsIgnoreCase("yes"))
                return 1;
            else
                return 0;

        } catch (Exception e) {
            e.printStackTrace();

            return 0;
        }
    }
}

}

日志:

 06-05 12:47:20.986: W/dalvikvm(2055): Exception Ljava/lang/RuntimeException; thrown while initializing Landroid/os/AsyncTask;
06-05 12:47:20.986: W/dalvikvm(2055): threadid=9: thread exiting with uncaught exception (group=0x401ca760)
06-05 12:47:20.986: W/System.err(2055): java.lang.ExceptionInInitializerError
06-05 12:47:20.986: W/System.err(2055):     at palmagent.NorthShoreAgent.Two.Modules.checkSavedNetsheet(Modules.java:2338)
06-05 12:47:20.986: W/System.err(2055):     at palmagent.NorthShoreAgent.Two.Modules.getData(Modules.java:2174)
06-05 12:47:20.986: W/System.err(2055):     at palmagent.NorthShoreAgent.Two.Modules.CheckingExpiry(Modules.java:2802)
06-05 12:47:20.986: W/System.err(2055):     at palmagent.NorthShoreAgent.Two.Pass.DoWork(Pass.java:434)
06-05 12:47:20.986: W/System.err(2055):     at palmagent.NorthShoreAgent.Two.Pass.access$1(Pass.java:391)
06-05 12:47:20.986: W/System.err(2055):     at palmagent.NorthShoreAgent.Two.Pass$DoinTask.run(Pass.java:372)
06-05 12:47:20.986: W/System.err(2055): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-05 12:47:20.986: W/System.err(2055):     at android.os.Handler.<init>(Handler.java:121)
06-05 12:47:20.986: W/System.err(2055):     at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:595)
06-05 12:47:20.986: W/System.err(2055):     at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:595)
06-05 12:47:20.986: W/System.err(2055):     at android.os.AsyncTask.<clinit>(AsyncTask.java:184)
06-05 12:47:20.986: W/System.err(2055):     ... 6 more
4

2 回答 2

0

尝试:

Looper.prepare();
nvail = new APICALL(ctx).execute(Modules.serverCheckLink).get();
Looper.loop()
于 2013-06-05T07:35:02.943 回答
0

问题可能出在您调用执行的第一个代码片段中,即

new APICALL(ctx).execute(...

该代码片段必须在主线程而不是后台线程上执行。必要时使用处理程序

private Handler handler = new Handler();

void myFunc() {
    handler.post(new Runnable() {
        public void run() {
            nvail = new APICALL(ctx).execute(Modules.serverCheckLink).get();
        }
    });
}
于 2013-08-28T23:25:31.127 回答