2013-07-16 2 views
0

Je suis nouveau dans android. Je crée l'application "Service Web JSON avec appels répétés". Mais ça ne peut pas marcher. il est arrêté de manière inattendue. Ici, j'utilise Timer.Ici, je donne aussi le code.Comment je effacer cette erreur inattendue.please m'aider.Cette application est arrêtée non attendueJSON WebService appelant

L'erreur est également donnée ici. S'il vous plaît corriger l'erreur

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

    } 
+0

cochez cette case [SO] (http://stackoverflow.com/questions/5009816/android-cant-create-handler-inside-thread-that-has-not-called-looper-prepare) –

+1

Utiliser le gestionnaire plutôt que minuteur. vous n'êtes pas autorisé (comme votre exception l'indique) à créer une asynctask sur un thread d'arrière-plan. – njzk2

Répondre

1

Vous essayez de faire opperations l'interface utilisateur dans un thread non de l'interface utilisateur. En regardant de votre code, cela se produit parce que vous accédez Context de l'activité à l'intérieur de votre TimerTask dans ces lignes de code:

t.schedule(new TimerTask(){ 

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

     }, 1000 * 15); 

EDIT je l'aurais utilisé un Handler au lieu de Timer puisque vous souhaitez que l'opération à exécuter périodiquement en arrière-plan. Appelez la méthode postDelayed dans votre Handlers' runnable pour rendre le processus courir à nouveau après un certain laps de temps est passé et utiliser le gestionnaire d'interface utilisateur pour mettre à jour l'interface utilisateur. Voir here pour plus d'informations sur les gestionnaires.

+0

Alors, que puis-je faire pour résoudre ce problème – ajay

+0

@ajay J'ai mis à jour ma réponse. – Angelo

+0

@ajay D'ailleurs, ce n'est pas une bonne expérience utilisateur d'ouvrir une boîte de dialogue chaque fois que votre processus est appelé. – Angelo

Questions connexes