0

我是安卓新手。我创建了“重复调用 JSON Web 服务”应用程序。但它不能工作。它意外停止。在这里我使用计时器。在这里我也给出了代码。我如何清除这个意外错误。请帮助我。这个应用程序意外停止

这里也给出了错误。请更正错误

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: 

代码:

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

    }
4

1 回答 1

1

您正在尝试在非 UI 线程中执行 UI 操作。从您的代码来看,发生这种情况是因为您在以下代码行中访问了ContextActivity TimerTask

t.schedule(new TimerTask(){

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

        }, 1000 * 15);

编辑我会使用 aHandler而不是,Timer因为您希望操作在后台定期运行。在 Handlers 的 runnable 中调用postDelayed方法,使进程在经过指定的时间后再次运行,并使用 UI 处理程序更新 UI。有关处理程序的信息,请参见此处

于 2013-07-16T06:47:06.837 回答