1

i m trying to call web services method from android app but i get in error java.lang.RuntimeException error occured while executing doInBackground() and the app stopped i have saw alot of question about this but they solved by adding internet permission to the manifest file but its Didn't work to

here is my code

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



    }



}

and the 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 Answer 1

3

your problem was with your EditText. you have identified it before you set the content of the activity which leads to windowLeaked and NPE.

so to fix this change:

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

to:

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

Good luck with your app and happy programming.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.