线程中的java.lang.ExceptionInInitializerError

时间:2013-06-05 07:32:49

标签: android

在我的应用程序中,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

2 个答案:

答案 0 :(得分:0)

尝试:

Looper.prepare();
nvail = new APICALL(ctx).execute(Modules.serverCheckLink).get();
Looper.loop()

答案 1 :(得分:0)

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

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();
        }
    });
}