致命异常Asynctask#1执行doInBackground()时发生java.lang.RuntimeException错误

时间:2013-12-31 20:23:48

标签: android android-asynctask

我试图从Android应用程序调用Web服务方法,但我收到错误执行doInBackground()时发生java.lang.RuntimeException错误 和应用程序停止我已经看到很多关于这个问题,但他们解决了通过添加到清单文件的互联网权限,但它没有工作

这是我的代码

     package tony.ds_hw;



import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class get_info extends Activity implements OnClickListener {


Button infobutton;
   EditText nametext;

        private static String URL="http://192.168.1.102:8080/DS_HW-war/webresources/Getinfo";
        private static final  String GET_REQUEST="GET";
        private static final String POST_REQUEST="POST";




    JSONParser jsonParser=new JSONParser();
    ProgressDialog pDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        nametext=(EditText) findViewById(R.id.editText1);
        setContentView(R.layout.get_info);
        infobutton=(Button) findViewById(R.id.button1);
        infobutton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.button1)
        {
             new Find().execute(URL);
        }
    }

    class Find extends AsyncTask<String,Void,String>
    {


        String json=null;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(get_info.this);
            pDialog.setMessage("Loading Please wait!");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            Toast.makeText(get_info.this,"finsih pre",Toast.LENGTH_LONG).show();
        }

          @Override
            protected String doInBackground(String... params) {
                String name =nametext.getText().toString();
                List<NameValuePair> param=new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("name", name));
                JSONObject jsonObject=jsonParser.makeHttpRequest(params[0], GET_REQUEST, param);
                json=jsonObject.toString();
                if(json != null)
                {
                Log.d("msg","Success");
                }
                else 
                {
                    Log.d("msg","Failed" );
                }

                return json;
            }

            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                pDialog.dismiss();
                if(result!=null) {
                Toast.makeText(get_info.this,"Data Loaded successfully", Toast.LENGTH_LONG).show();

                Intent i=new Intent(get_info.this,Result.class ).putExtra("jsonstring",result);
                finish();
                startActivity(i);

                } else 
                    Toast.makeText(get_info.this,"Failed To Load Data", Toast.LENGTH_LONG).show();
            }



    }



}

和logcat

12-31 23:35:58.593: E/AndroidRuntime(1124): FATAL EXCEPTION: AsyncTask #1
12-31 23:35:58.593: E/AndroidRuntime(1124): java.lang.RuntimeException: An error occured while executing doInBackground()
12-31 23:35:58.593: E/AndroidRuntime(1124):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
12-31 23:35:58.593: E/AndroidRuntime(1124):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-31 23:35:58.593: E/AndroidRuntime(1124):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-31 23:35:58.593: E/AndroidRuntime(1124):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-31 23:35:58.593: E/AndroidRuntime(1124):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-31 23:35:58.593: E/AndroidRuntime(1124):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
12-31 23:35:58.593: E/AndroidRuntime(1124):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-31 23:35:58.593: E/AndroidRuntime(1124):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-31 23:35:58.593: E/AndroidRuntime(1124):     at java.lang.Thread.run(Thread.java:856)
12-31 23:35:58.593: E/AndroidRuntime(1124): Caused by: java.lang.NullPointerException
12-31 23:35:58.593: E/AndroidRuntime(1124):     at tony.ds_hw.get_info$Find.doInBackground(get_info.java:73)
12-31 23:35:58.593: E/AndroidRuntime(1124):     at tony.ds_hw.get_info$Find.doInBackground(get_info.java:1)
12-31 23:35:58.593: E/AndroidRuntime(1124):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
12-31 23:35:58.593: E/AndroidRuntime(1124):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-31 23:35:58.593: E/AndroidRuntime(1124):     ... 5 more
12-31 23:35:58.943: E/WindowManager(1124): Activity tony.ds_hw.get_info has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2be0f4a0 that was originally added here
12-31 23:35:58.943: E/WindowManager(1124): android.view.WindowLeaked: Activity tony.ds_hw.get_info has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2be0f4a0 that was originally added here
12-31 23:35:58.943: E/WindowManager(1124):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
12-31 23:35:58.943: E/WindowManager(1124):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
12-31 23:35:58.943: E/WindowManager(1124):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
12-31 23:35:58.943: E/WindowManager(1124):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
12-31 23:35:58.943: E/WindowManager(1124):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
12-31 23:35:58.943: E/WindowManager(1124):  at android.app.Dialog.show(Dialog.java:278)
12-31 23:35:58.943: E/WindowManager(1124):  at tony.ds_hw.get_info$Find.onPreExecute(get_info.java:67)
12-31 23:35:58.943: E/WindowManager(1124):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
12-31 23:35:58.943: E/WindowManager(1124):  at android.os.AsyncTask.execute(AsyncTask.java:511)
12-31 23:35:58.943: E/WindowManager(1124):  at tony.ds_hw.get_info.onClick(get_info.java:51)
12-31 23:35:58.943: E/WindowManager(1124):  at android.view.View.performClick(View.java:3534)
12-31 23:35:58.943: E/WindowManager(1124):  at android.view.View$PerformClick.run(View.java:14263)
12-31 23:35:58.943: E/WindowManager(1124):  at android.os.Handler.handleCallback(Handler.java:605)
12-31 23:35:58.943: E/WindowManager(1124):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-31 23:35:58.943: E/WindowManager(1124):  at android.os.Looper.loop(Looper.java:137)
12-31 23:35:58.943: E/WindowManager(1124):  at android.app.ActivityThread.main(ActivityThread.java:4441)
12-31 23:35:58.943: E/WindowManager(1124):  at java.lang.reflect.Method.invokeNative(Native Method)
12-31 23:35:58.943: E/WindowManager(1124):  at java.lang.reflect.Method.invoke(Method.java:511)
12-31 23:35:58.943: E/WindowManager(1124):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-31 23:35:58.943: E/WindowManager(1124):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-31 23:35:58.943: E/WindowManager(1124):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

您的问题出在EditText上。您在设置导致windowLeakedNPE的活动内容之前已经识别出来。

所以要解决这个改变:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    nametext=(EditText) findViewById(R.id.editText1); //move this!
    setContentView(R.layout.get_info);
    infobutton=(Button) findViewById(R.id.button1);

为:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_info);
    nametext=(EditText) findViewById(R.id.editText1); //to here
    infobutton=(Button) findViewById(R.id.button1);

祝你的应用程序运气好,并且编程愉快。