0

I m stuck in the problem from a time. this is my code

public class PilgrimFriendActivity extends Activity {

    private String DOMAIN = "http://";

    private String LAWS_URL = "";
    private String LOCATIONS_URL = "";

    private ListView list;
    private TextView content;
    private Law law  ;
    private Location location;
    private ProgressDialog dialog;
    private SharedPreferences preferences;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        preferences = getSharedPreferences("com.hackontech.pilgrim_preferences", MODE_PRIVATE);
        this.DOMAIN += preferences.getString("DOMAIN","192.168.0.104");
        this.LAWS_URL = DOMAIN + "/pilgrimfriend/main/getAllLaws";
        this.LOCATIONS_URL = DOMAIN + "/pilgrimfriend/main/getAllLocations";

        dialog = new ProgressDialog(this);
        content = (TextView) findViewById(R.id.content);
        list = (ListView) findViewById(R.id.laws);
        this.lLaws();

    }


    public void lLaws() {

        final class RequestTask extends AsyncTask<String, String, String>{

            private String URL = null;

            @Override
            protected String doInBackground(String... uri) {
                String result = null;
                URL = uri[0];
                try {

                    //Create default http client
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpGet httpget = new HttpGet(URL);
                    Log.v("URL",URL);
                    HttpResponse response = httpclient.execute(httpget);
                    //Retrieve the response body or "entity"
                    HttpEntity entity = response.getEntity();
                    //Return the entity as String
                    result = EntityUtils.toString(entity);
                    Log.v("result",result.toString());

                } catch (Exception e) {
                    return e.getMessage();
                }

                return result;
            }

            @Override
            protected void onPostExecute(String result) {
                //super.onPostExecute(result);
                dialog.dismiss();
                try{

                    JSONArray jArray = new JSONArray(result);

                    JSONObject json_data;
                    ArrayList<String> items = new ArrayList<String>();
                    for(int i=0; i < jArray.length() ; i++) {
                        json_data = jArray.getJSONObject(i);
                        int id=json_data.getInt("id");
                        String title=json_data.getString("title");
                        String scholor =json_data.getString("scholor");
                        items.add(title+" ("+scholor+")");
                    }

                    content.setText(result);
                    ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(getApplicationContext(),  
                            android.R.layout.simple_list_item_1, items);
                    list.setAdapter(mArrayAdapter);

                    Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();

                }
                catch(JSONException e) {
                    //content.setText(e.getMessage());
                }
                //Do anything with response..
            }

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();

                dialog.setTitle("Downloading Laws Data");
                dialog.show();
            }

        }

        new RequestTask().execute(LAWS_URL);
    }

I every single method is working but in onPostExecute when i change the view elements like i did content.setText(result), the application stops immediately, what's the problem.

here is the LogCat:

12-07 22:44:33.302: E/AndroidRuntime(778): FATAL EXCEPTION: main
12-07 22:44:33.302: E/AndroidRuntime(778): java.lang.NullPointerException
12-07 22:44:33.302: E/AndroidRuntime(778):  at com.hackontech.pilgrim.PilgrimFriendActivity$1RequestTask.onPostExecute(PilgrimFriendActivity.java:120)
12-07 22:44:33.302: E/AndroidRuntime(778):  at com.hackontech.pilgrim.PilgrimFriendActivity$1RequestTask.onPostExecute(PilgrimFriendActivity.java:1)
12-07 22:44:33.302: E/AndroidRuntime(778):  at android.os.AsyncTask.finish(AsyncTask.java:417)
12-07 22:44:33.302: E/AndroidRuntime(778):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
12-07 22:44:33.302: E/AndroidRuntime(778):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
12-07 22:44:33.302: E/AndroidRuntime(778):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 22:44:33.302: E/AndroidRuntime(778):  at android.os.Looper.loop(Looper.java:123)
12-07 22:44:33.302: E/AndroidRuntime(778):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-07 22:44:33.302: E/AndroidRuntime(778):  at java.lang.reflect.Method.invokeNative(Native Method)
12-07 22:44:33.302: E/AndroidRuntime(778):  at java.lang.reflect.Method.invoke(Method.java:507)
12-07 22:44:33.302: E/AndroidRuntime(778):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-07 22:44:33.302: E/AndroidRuntime(778):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-07 22:44:33.302: E/AndroidRuntime(778):  at dalvik.system.NativeStart.main(Native Method)
11
  • 3
    I guess content is null. did you initialize it? Dec 7, 2012 at 17:25
  • Yeah i did, i can update it anywhere else, not only the content, the listview is also not working, in short it's not updating the UI, but the strange part is that the process dialog is showing, the Toast does display
    – user868900
    Dec 7, 2012 at 17:28
  • add Crash logs and the line number. dialog is showing or dismiss? Dec 7, 2012 at 17:31
  • dialog is dismissed, let me post complete code
    – user868900
    Dec 7, 2012 at 17:32
  • @user868900 Exception backtrace in the log tells you exact line number where the exception was thrown, why not simply check what's going on there? How about placing a breakpoint there and running your application under debugger control? It's all basic stuff. Dec 7, 2012 at 17:51

1 Answer 1

0

For some reason, onPostExecute method does all the UI updates at once, when the method has finished.

For example:

@Override
protected Void doInBackground() {
    someTextView.setText("Do In Background Finished!");
}

@Override
protected void onPostExecute () {
    someTextView.setText("Post Execute Started");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // ..
    }
    someTextView.setText("Post Execute Finished");
}

will result in showing "Do In Background Finished!" and after 5 seconds (when onPostExecute ends) will update the text with "Post Execute Started" and immediately updates the text again with "Post Execute Finished".

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.