0

I am new in android . I create "JSON web service repetedly calling" application. But it can not work. it is stopped unexpectedly. Here i use Timer.Here i also give the code.How i clear this unexpected error.please help me.THis application is stopped un expectedly

The error is also given here. please correct the error

07-16 11:31:17.404: I/Process(275): Sending signal. PID: 275 SIG: 9
07-16 11:36:36.624: W/dalvikvm(285): Exception Ljava/lang/RuntimeException; thrown during Landroid/os/AsyncTask;.<clinit>
07-16 11:36:36.634: W/dalvikvm(285): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
07-16 11:36:36.664: E/AndroidRuntime(285): FATAL EXCEPTION: Timer-0
07-16 11:36:36.664: E/AndroidRuntime(285): java.lang.ExceptionInInitializerError
07-16 11:36:36.664: E/AndroidRuntime(285):  at com.example.repeat.MainActivity$1.run(MainActivity.java:51)
07-16 11:36:36.664: E/AndroidRuntime(285):  at java.util.Timer$TimerImpl.run(Timer.java:289)
07-16 11:36:36.664: E/AndroidRuntime(285): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
07-16 11:36:36.664: E/AndroidRuntime(285):  at android.os.Handler.<init>(Handler.java:121)
07-16 11:36:36.664: E/AndroidRuntime(285):  at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
07-16 11:36:36.664: E/AndroidRuntime(285):  at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
07-16 11:36:36.664: E/AndroidRuntime(285):  at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
07-16 11:36:36.664: E/AndroidRuntime(285):  ... 2 more
07-16 11:41:36.887: I/Process(285): Sending signal. PID: 285 SIG: 

Code:

    Timer t;
        private Context context;
         private static String url = "http://docs.blackberry.com/sampledata.json";
         private static final String TAG_VTYPE = "vehicleType";
         private static final String TAG_VCOLOR = "vehicleColor";
         private static final String TAG_FUEL = "fuel";
         private static final String TAG_TREAD = "treadType";
         private static final String TAG_OPERATOR = "approvedOperators";
         private static final String TAG_NAME = "name";
         private static final String TAG_POINTS = "experiencePoints";



        ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
        ListView lv ;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            t=new Timer();
            t.schedule(new TimerTask(){

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    new progress(MainActivity.this).execute();
                }

            }, 1000 * 15);

        }

        private class progress extends AsyncTask<String, Void, Boolean>{

            private ProgressDialog dialog;
            public progress(ListActivity activity){

                   Log.i("1", "Called");
                   context = activity;
                   dialog = new ProgressDialog(context);

            }

             protected void onPreExecute() {
                   this.dialog.setMessage("Progress start");
                   this.dialog.show();
                  }

             protected void onPostExecute(final Boolean success) {
                   if (dialog.isShowing()) {
                    dialog.dismiss();
                   }
                   ListAdapter Adapter=new SimpleAdapter(context,jsonlist,
                           R.layout.list_item,new String[]{TAG_VTYPE, TAG_VCOLOR,
                           TAG_FUEL, TAG_TREAD },new int[] {
                           R.id.vehicleType,R.id.vehicleColor,R.id.fuel,
                           R.id.treadType});

                   setListAdapter(Adapter);

                    // selecting single ListView item
                    lv = getListView();

             }

            @Override
            protected Boolean doInBackground(String... params) {
                // TODO Auto-generated method stub

                JSONParser parser=new JSONParser();
                JSONArray array=parser.getJSONFromUrl(url);
                for (int i = 0; i < array.length(); i++){
                    try{

                        JSONObject c= array.getJSONObject(i);
                         String vtype = c.getString(TAG_VTYPE);

                          String vcolor = c.getString(TAG_VCOLOR);
                         String vfuel = c.getString(TAG_FUEL);
                         String vtread = c.getString(TAG_TREAD);

                          HashMap<String, String> map = new HashMap<String, String>();

                          // adding each child node to HashMap key => value
                         map.put(TAG_VTYPE, vtype);
                         map.put(TAG_VCOLOR, vcolor);
                         map.put(TAG_FUEL, vfuel);
                         map.put(TAG_TREAD, vtread);
                         jsonlist.add(map);

                    }catch(JSONException e){

                        e.printStackTrace();
                    }
                }


                return null;
            }

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }
njzk2
  • 38,969
  • 7
  • 69
  • 107
ajay
  • 11
  • 1
  • 4
  • check this [SO](http://stackoverflow.com/questions/5009816/android-cant-create-handler-inside-thread-that-has-not-called-looper-prepare) – Srikanth Roopa Jul 16 '13 at 06:41
  • 1
    use handler rather than timer. you are not allowed (as your exception indicates) to create an asynctask on a background thread. – njzk2 Jul 16 '13 at 08:22

1 Answers1

1

You are trying to do UI opperations in a non UI thread. Looking from your code, this happens because you are accessing the Activity's Context inside your TimerTask in this lines of code:

t.schedule(new TimerTask(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
                new progress(MainActivity.this).execute();
            }

        }, 1000 * 15);

EDIT I would have used a Handler instead of Timer since you want the operation to run periodically in background. Call the method postDelayed inside your Handlers' runnable to make the process run again after a specified amount of time has passed and use the UI handler to update the UI. See here for information about handlers.

njzk2
  • 38,969
  • 7
  • 69
  • 107
AggelosK
  • 4,313
  • 2
  • 32
  • 37