在列表视图上填充活动

时间:2013-08-24 12:05:09

标签: android

我正在尝试从列表视图

启动项目活动
  • 我成功将数据解析为listview
  • 但无法填充为单个活动解析的数据
  • 我已在清单
  • 中声明了新活动

MainActivity.java

public class MainActivity extends Activity {

    // url to make request
    private static String url = "http://54.218.73.244:7002/";

    private HashMap<Integer, String> TimeMap = new HashMap<Integer, String>();

    List<Item> yourData = new ArrayList<Item>();

    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Instantiating ProgressDialog with onCreate method
        progressDialog=new ProgressDialog(MainActivity.this);
        new ParsingAsync().execute();

    }

    private class ParsingAsync extends AsyncTask<Void, Void, Void>
    {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog=ProgressDialog.show(MainActivity.this, "", "Please Wait", true, false);


        }

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


            String _response = null;
            try
            {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            HttpGet request = new HttpGet(url);
            HttpResponse response = httpclient.execute(request);
            HttpEntity resEntity = response.getEntity();
             _response =EntityUtils.toString(resEntity);
            JSONObject jsonObject = new JSONObject(_response);
            JSONArray first_array = jsonObject.getJSONArray("restaurants");
            JSONArray second_array = jsonObject.getJSONArray("RestaurantTimings");
             for(int i=0;i<first_array.length();i++)
             {
                    JSONObject c = second_array.getJSONObject(i);
                    Item item = new Item();

                    // Storing each json item in variable
                    int id = c.getInt("_id");
                    String TIME = c.getString("RestaurantTime");
                    item.setTime(TIME);

                    c = first_array.getJSONObject(i);

                    String NAME=c.getString("restaurantNAME");
                    item.setName(NAME);
                    yourData.add(item);
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progressDialog.dismiss();

            TextView timedisplay=(TextView) findViewById(R.id.RestaurantTimeID);


            ListView yourListView = (ListView) findViewById(R.id.listViewID);
            ListAdapter customAdapter = new ListAdapter(MainActivity.this, R.layout.itemlistrow, yourData);
            yourListView.setAdapter(customAdapter);
            yourListView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    Intent i = new Intent(MainActivity.this, RestaurantDesc.class);
                    i.putExtra("key", "http://54.218.73.244:7002/CopperChimney");
                    startActivity(i);

                }
            });
        }

    }

}

RestaurantDesc.java

public class RestaurantDesc extends Activity{
    // url to make request


    String url ;

    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.restaurant_desc);

        url = getIntent().getStringExtra("key");


        //Instantiating ProgressDialog with onCreate method
        progressDialog=new ProgressDialog(RestaurantDesc.this);
        new ParsingAsync().execute();

    }

    private class ParsingAsync extends AsyncTask<Void, Void, Void>
    {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog=ProgressDialog.show(RestaurantDesc.this, "", "Please Wait", true, false);


        }

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


            String _response = null;


            try
            {
                HttpClient httpclient = new DefaultHttpClient();
                httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
                HttpGet request = new HttpGet(url);
                HttpResponse response = httpclient.execute(request);
                HttpEntity resEntity = response.getEntity();
                _response =EntityUtils.toString(resEntity);
                JSONObject jsonObject = new JSONObject(_response);

                JSONArray first_array = jsonObject.getJSONArray("CopperChimney");
                JSONArray second_array = jsonObject.getJSONArray("RestaurantTimings");


                for(int i=0; i < first_array.length(); i++)
                {
                    JSONObject detail_obj = first_array.getJSONObject(i);

                    //FIRST  - - - - -- - - - ROW
                    TextView textView=(TextView) findViewById(R.id.textView1);
                    textView.setText(first_array.getJSONObject(1).getString("Starters"));


                }

                for(int i=0; i < second_array.length(); i++)
                {
                    JSONObject detail_obj = second_array.getJSONObject(i);
                //  textView1.setText(second_array.getJSONObject(0).getString("place"));
                }


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progressDialog.dismiss();

            //TextView timedisplay=(TextView) findViewById(R.id.RestaurantTimeID);



        }

    }
}   

LOG

08-24 17:12:37.076: D/dalvikvm(837): GC_EXTERNAL_ALLOC freed 166K, 50% free 2889K/5703K, external 2111K/2137K, paused 84ms
08-24 17:12:39.487: E/JSON Parser(837): Error parsing data org.json.JSONException: End of input at character 0 of 
08-24 17:12:39.487: W/dalvikvm(837): threadid=10: thread exiting with uncaught exception (group=0x40015560)
08-24 17:12:39.496: E/AndroidRuntime(837): FATAL EXCEPTION: AsyncTask #2
08-24 17:12:39.496: E/AndroidRuntime(837): java.lang.RuntimeException: An error occured while executing doInBackground()
08-24 17:12:39.496: E/AndroidRuntime(837):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-24 17:12:39.496: E/AndroidRuntime(837):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-24 17:12:39.496: E/AndroidRuntime(837):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-24 17:12:39.496: E/AndroidRuntime(837):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-24 17:12:39.496: E/AndroidRuntime(837):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-24 17:12:39.496: E/AndroidRuntime(837):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-24 17:12:39.496: E/AndroidRuntime(837):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-24 17:12:39.496: E/AndroidRuntime(837):  at java.lang.Thread.run(Thread.java:1019)
08-24 17:12:39.496: E/AndroidRuntime(837): Caused by: java.lang.NullPointerException
08-24 17:12:39.496: E/AndroidRuntime(837):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:84)
08-24 17:12:39.496: E/AndroidRuntime(837):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-24 17:12:39.496: E/AndroidRuntime(837):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-24 17:12:39.496: E/AndroidRuntime(837):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-24 17:12:39.496: E/AndroidRuntime(837):  ... 4 more
08-24 17:12:40.818: E/WindowManager(837): Activity com.project.findmybuffet.RestaurantDesc has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4058c960 that was originally added here
08-24 17:12:40.818: E/WindowManager(837): android.view.WindowLeaked: Activity com.project.findmybuffet.RestaurantDesc has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4058c960 that was originally added here
08-24 17:12:40.818: E/WindowManager(837):   at android.view.ViewRoot.<init>(ViewRoot.java:258)
08-24 17:12:40.818: E/WindowManager(837):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-24 17:12:40.818: E/WindowManager(837):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-24 17:12:40.818: E/WindowManager(837):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-24 17:12:40.818: E/WindowManager(837):   at android.app.Dialog.show(Dialog.java:241)
08-24 17:12:40.818: E/WindowManager(837):   at android.app.ProgressDialog.show(ProgressDialog.java:107)
08-24 17:12:40.818: E/WindowManager(837):   at android.app.ProgressDialog.show(ProgressDialog.java:95)
08-24 17:12:40.818: E/WindowManager(837):   at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPreExecute(RestaurantDesc.java:64)
08-24 17:12:40.818: E/WindowManager(837):   at android.os.AsyncTask.execute(AsyncTask.java:391)
08-24 17:12:40.818: E/WindowManager(837):   at com.project.findmybuffet.RestaurantDesc.onCreate(RestaurantDesc.java:54)
08-24 17:12:40.818: E/WindowManager(837):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-24 17:12:40.818: E/WindowManager(837):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-24 17:12:40.818: E/WindowManager(837):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-24 17:12:40.818: E/WindowManager(837):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-24 17:12:40.818: E/WindowManager(837):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-24 17:12:40.818: E/WindowManager(837):   at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 17:12:40.818: E/WindowManager(837):   at android.os.Looper.loop(Looper.java:123)
08-24 17:12:40.818: E/WindowManager(837):   at android.app.ActivityThread.main(ActivityThread.java:3683)
08-24 17:12:40.818: E/WindowManager(837):   at java.lang.reflect.Method.invokeNative(Native Method)
08-24 17:12:40.818: E/WindowManager(837):   at java.lang.reflect.Method.invoke(Method.java:507)
08-24 17:12:40.818: E/WindowManager(837):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-24 17:12:40.818: E/WindowManager(837):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-24 17:12:40.818: E/WindowManager(837):   at dalvik.system.NativeStart.main(Native Method)
08-24 17:12:43.516: I/Process(837): Sending signal. PID: 837 SIG: 9
08-24 17:16:19.807: D/dalvikvm(874): GC_EXTERNAL_ALLOC freed 167K, 50% free 2888K/5703K, external 2111K/2137K, paused 84ms
08-24 17:16:22.188: E/JSON Parser(874): Error parsing data org.json.JSONException: End of input at character 0 of 
08-24 17:16:22.188: W/dalvikvm(874): threadid=10: thread exiting with uncaught exception (group=0x40015560)
08-24 17:16:22.206: E/AndroidRuntime(874): FATAL EXCEPTION: AsyncTask #2
08-24 17:16:22.206: E/AndroidRuntime(874): java.lang.RuntimeException: An error occured while executing doInBackground()
08-24 17:16:22.206: E/AndroidRuntime(874):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-24 17:16:22.206: E/AndroidRuntime(874):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-24 17:16:22.206: E/AndroidRuntime(874):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-24 17:16:22.206: E/AndroidRuntime(874):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-24 17:16:22.206: E/AndroidRuntime(874):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-24 17:16:22.206: E/AndroidRuntime(874):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-24 17:16:22.206: E/AndroidRuntime(874):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-24 17:16:22.206: E/AndroidRuntime(874):  at java.lang.Thread.run(Thread.java:1019)
08-24 17:16:22.206: E/AndroidRuntime(874): Caused by: java.lang.NullPointerException
08-24 17:16:22.206: E/AndroidRuntime(874):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:90)
08-24 17:16:22.206: E/AndroidRuntime(874):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-24 17:16:22.206: E/AndroidRuntime(874):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-24 17:16:22.206: E/AndroidRuntime(874):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-24 17:16:22.206: E/AndroidRuntime(874):  ... 4 more
08-24 17:16:23.746: E/WindowManager(874): Activity com.project.findmybuffet.RestaurantDesc has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4058cd50 that was originally added here
08-24 17:16:23.746: E/WindowManager(874): android.view.WindowLeaked: Activity com.project.findmybuffet.RestaurantDesc has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4058cd50 that was originally added here
08-24 17:16:23.746: E/WindowManager(874):   at android.view.ViewRoot.<init>(ViewRoot.java:258)
08-24 17:16:23.746: E/WindowManager(874):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-24 17:16:23.746: E/WindowManager(874):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-24 17:16:23.746: E/WindowManager(874):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-24 17:16:23.746: E/WindowManager(874):   at android.app.Dialog.show(Dialog.java:241)
08-24 17:16:23.746: E/WindowManager(874):   at android.app.ProgressDialog.show(ProgressDialog.java:107)
08-24 17:16:23.746: E/WindowManager(874):   at android.app.ProgressDialog.show(ProgressDialog.java:95)
08-24 17:16:23.746: E/WindowManager(874):   at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPreExecute(RestaurantDesc.java:70)
08-24 17:16:23.746: E/WindowManager(874):   at android.os.AsyncTask.execute(AsyncTask.java:391)
08-24 17:16:23.746: E/WindowManager(874):   at com.project.findmybuffet.RestaurantDesc.onCreate(RestaurantDesc.java:60)
08-24 17:16:23.746: E/WindowManager(874):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-24 17:16:23.746: E/WindowManager(874):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-24 17:16:23.746: E/WindowManager(874):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-24 17:16:23.746: E/WindowManager(874):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-24 17:16:23.746: E/WindowManager(874):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-24 17:16:23.746: E/WindowManager(874):   at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 17:16:23.746: E/WindowManager(874):   at android.os.Looper.loop(Looper.java:123)
08-24 17:16:23.746: E/WindowManager(874):   at android.app.ActivityThread.main(ActivityThread.java:3683)
08-24 17:16:23.746: E/WindowManager(874):   at java.lang.reflect.Method.invokeNative(Native Method)
08-24 17:16:23.746: E/WindowManager(874):   at java.lang.reflect.Method.invoke(Method.java:507)
08-24 17:16:23.746: E/WindowManager(874):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-24 17:16:23.746: E/WindowManager(874):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-24 17:16:23.746: E/WindowManager(874):   at dalvik.system.NativeStart.main(Native Method)
08-24 17:17:16.536: W/dalvikvm(912): threadid=6: spin on suspend #1 threadid=9 (pcf=0)
08-24 17:17:16.536: D/dalvikvm(912): Temporarily moving tid 920 to fg (was 0)
08-24 17:17:16.536: D/dalvikvm(912): Temporarily raised priority on tid 920 (10 -> 0)
08-24 17:17:16.586: W/dalvikvm(912): threadid=6: spin on suspend resolved in 1048 msec
08-24 17:17:16.586: D/dalvikvm(912): Restored policy of 920 to 0
08-24 17:17:16.586: D/dalvikvm(912): Restored priority on 920 to 10
08-24 17:17:21.246: D/dalvikvm(912): GC_EXTERNAL_ALLOC freed 156K, 50% free 2888K/5703K, external 2111K/2137K, paused 84ms
08-24 17:17:23.526: E/JSON Parser(912): Error parsing data org.json.JSONException: End of input at character 0 of 
08-24 17:17:23.546: W/dalvikvm(912): threadid=10: thread exiting with uncaught exception (group=0x40015560)
08-24 17:17:23.566: E/AndroidRuntime(912): FATAL EXCEPTION: AsyncTask #2
08-24 17:17:23.566: E/AndroidRuntime(912): java.lang.RuntimeException: An error occured while executing doInBackground()
08-24 17:17:23.566: E/AndroidRuntime(912):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-24 17:17:23.566: E/AndroidRuntime(912):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-24 17:17:23.566: E/AndroidRuntime(912):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-24 17:17:23.566: E/AndroidRuntime(912):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-24 17:17:23.566: E/AndroidRuntime(912):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-24 17:17:23.566: E/AndroidRuntime(912):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-24 17:17:23.566: E/AndroidRuntime(912):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-24 17:17:23.566: E/AndroidRuntime(912):  at java.lang.Thread.run(Thread.java:1019)
08-24 17:17:23.566: E/AndroidRuntime(912): Caused by: java.lang.NullPointerException
08-24 17:17:23.566: E/AndroidRuntime(912):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:90)
08-24 17:17:23.566: E/AndroidRuntime(912):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-24 17:17:23.566: E/AndroidRuntime(912):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-24 17:17:23.566: E/AndroidRuntime(912):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-24 17:17:23.566: E/AndroidRuntime(912):  ... 4 more
08-24 17:17:24.786: E/WindowManager(912): Activity com.project.findmybuffet.RestaurantDesc has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4058a140 that was originally added here
08-24 17:17:24.786: E/WindowManager(912): android.view.WindowLeaked: Activity com.project.findmybuffet.RestaurantDesc has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4058a140 that was originally added here
08-24 17:17:24.786: E/WindowManager(912):   at android.view.ViewRoot.<init>(ViewRoot.java:258)
08-24 17:17:24.786: E/WindowManager(912):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-24 17:17:24.786: E/WindowManager(912):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-24 17:17:24.786: E/WindowManager(912):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-24 17:17:24.786: E/WindowManager(912):   at android.app.Dialog.show(Dialog.java:241)
08-24 17:17:24.786: E/WindowManager(912):   at android.app.ProgressDialog.show(ProgressDialog.java:107)
08-24 17:17:24.786: E/WindowManager(912):   at android.app.ProgressDialog.show(ProgressDialog.java:95)
08-24 17:17:24.786: E/WindowManager(912):   at com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPreExecute(RestaurantDesc.java:70)
08-24 17:17:24.786: E/WindowManager(912):   at android.os.AsyncTask.execute(AsyncTask.java:391)
08-24 17:17:24.786: E/WindowManager(912):   at com.project.findmybuffet.RestaurantDesc.onCreate(RestaurantDesc.java:60)
08-24 17:17:24.786: E/WindowManager(912):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-24 17:17:24.786: E/WindowManager(912):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-24 17:17:24.786: E/WindowManager(912):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-24 17:17:24.786: E/WindowManager(912):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-24 17:17:24.786: E/WindowManager(912):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-24 17:17:24.786: E/WindowManager(912):   at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 17:17:24.786: E/WindowManager(912):   at android.os.Looper.loop(Looper.java:123)
08-24 17:17:24.786: E/WindowManager(912):   at android.app.ActivityThread.main(ActivityThread.java:3683)
08-24 17:17:24.786: E/WindowManager(912):   at java.lang.reflect.Method.invokeNative(Native Method)
08-24 17:17:24.786: E/WindowManager(912):   at java.lang.reflect.Method.invoke(Method.java:507)
08-24 17:17:24.786: E/WindowManager(912):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-24 17:17:24.786: E/WindowManager(912):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-24 17:17:24.786: E/WindowManager(912):   at dalvik.system.NativeStart.main(Native Method)
08-24 17:22:23.667: I/Process(912): Sending signal. PID: 912 SIG: 9
08-24 17:25:35.066: D/dalvikvm(949): GC_EXTERNAL_ALLOC freed 157K, 50% free 2888K/5703K, external 2111K/2137K, paused 88ms
08-24 17:25:37.556: W/dalvikvm(949): threadid=10: thread exiting with uncaught exception (group=0x40015560)
08-24 17:25:37.576: E/AndroidRuntime(949): FATAL EXCEPTION: AsyncTask #2
08-24 17:25:37.576: E/AndroidRuntime(949): java.lang.RuntimeException: An error occured while executing doInBackground()
08-24 17:25:37.576: E/AndroidRuntime(949):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-24 17:25:37.576: E/AndroidRuntime(949):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-24 17:25:37.576: E/AndroidRuntime(949):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-24 17:25:37.576: E/AndroidRuntime(949):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-24 17:25:37.576: E/AndroidRuntime(949):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-24 17:25:37.576: E/AndroidRuntime(949):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-24 17:25:37.576: E/AndroidRuntime(949):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-24 17:25:37.576: E/AndroidRuntime(949):  at java.lang.Thread.run(Thread.java:1019)
08-24 17:25:37.576: E/AndroidRuntime(949): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-24 17:25:37.576: E/AndroidRuntime(949):  at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
08-24 17:25:37.576: E/AndroidRuntime(949):  at android.view.ViewRoot.requestLayout(ViewRoot.java:629)
08-24 17:25:37.576: E/AndroidRuntime(949):  at android.view.View.requestLayout(View.java:8267)
08-24 17:25:37.576: E/AndroidRuntime(949):  at android.widget.TextView.setText(TextView.java:2724)
08-24 17:25:37.576: E/AndroidRuntime(949):  at android.widget.TextView.setText(TextView.java:2592)
08-24 17:25:37.576: E/AndroidRuntime(949):  at android.widget.TextView.setText(TextView.java:2567)
08-24 17:25:37.576: E/AndroidRuntime(949):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:94)
08-24 17:25:37.576: E/AndroidRuntime(949):  at com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-24 17:25:37.576: E/AndroidRuntime(949):  at android.os.AsyncTask$2.call(AsyncTask.java:185)

08-24 17:25:38.956: E/WindowManager(949):   at dalvik.system.NativeStart.main(Native Method)

编辑后

RestaurantDesc.java

public class RestaurantDesc extends Activity{
    // url to make request


    String url ;
    ProgressDialog progressDialog;
    JSONObject jsonObject;
    JSONArray first_array ;
    JSONArray second_array;
    TextView textView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.restaurant_desc);
        url = getIntent().getStringExtra("key");
        progressDialog=new ProgressDialog(RestaurantDesc.this);
        new ParsingAsync().execute();
    }




    private class ParsingAsync extends AsyncTask<Void, Void, Void>
    {



        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog=ProgressDialog.show(RestaurantDesc.this, "", "Please Wait", true, false);
        }



        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            String _response = null;
            try
            {
                HttpClient httpclient = new DefaultHttpClient();
                httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
                HttpGet request = new HttpGet(url);
                HttpResponse response = httpclient.execute(request);
                HttpEntity resEntity = response.getEntity();
                _response =EntityUtils.toString(resEntity);

                jsonObject = new JSONObject(_response);
                first_array = jsonObject.getJSONArray("CopperChimney");


            } catch (JSONException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;

        }






        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progressDialog.dismiss();

            //TextView timedisplay=(TextView) findViewById(R.id.RestaurantTimeID);

            for(int i=0; i < first_array.length(); i++)
            {
                try {
                    JSONObject detail_obj = first_array.getJSONObject(i);

                    //FIRST  - - - - -- - - - ROW
                    TextView textView=(TextView) findViewById(R.id.textView1);
                    textView.setText(first_array.getJSONObject(1).getString("Starters"));


                    textView=(TextView) findViewById(R.id.textView2);
                    textView.setText(first_array.getJSONObject(2).getString("Starters"));

                    textView=(TextView) findViewById(R.id.textView3);
                    textView.setText(first_array.getJSONObject(3).getString("Starters"));



                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
            }
        }
    }
}           

任何想法,

感谢

4 个答案:

答案 0 :(得分:0)

您无法在doInBackground方法中触摸与UI相关的任何内容,因此这就是问题所在:

 TextView textView=(TextView) findViewById(R.id.textView1);
 textView.setText(first_array.getJSONObject(1).getString("Starters"));

将此项移至onPostExecute。

答案 1 :(得分:0)

你不能在doInbackground()中做与UI相关的东西。所以请从doInBackground()中删除以下行并将其放在onPostExecute()中。

TextView textView=(TextView) findViewById(R.id.textView1);
                    textView.setText(first_array.getJSONObject(1).getString("Starters"));

答案 2 :(得分:0)

首先你必须找到这个:

  TextView timedisplay=(TextView) findViewById(R.id.RestaurantTimeID);  
  ListView yourListView = (ListView) findViewById(R.id.listViewID);
onCreate()方法中的

,而不是在MainActivity.class中 onPostExecute()方法.....

并且在 RestaurantDesc.java 中找到 TextView onCreate()方法的ID ...

并更新此

 textView.setText(first_array.getJSONObject(1).getString("Starters"));

onPostExecute()方法中; ...

你的第一个数组以i = 0开始,所以改变所有

 textView.setText(first_array.getJSONObject(0).getString("Starters"));
 textView.setText(first_array.getJSONObject(1).getString("Starters"));
 textView.setText(first_array.getJSONObject(2).getString("Starters"));

答案 3 :(得分:0)

正如对方所说,你不能在doInBackground方法中触及与UI相关的任何内容。

您可以使用onProgressUpdate方法更新UI。

http://developer.android.com/reference/android/os/AsyncTask.html

通常,我把所有功能

protected void onProgressUpdate(Integer... progress) {
        switch (progress[0]) {
            case 1: {      
                TextView textView=(TextView) findViewById(R.id.textView1);             
                textView.setText(first_array.getJSONObject(1).getString("Starters"));
                .....
                break;
            }
            case 2: {
                ......
                break;
            }
            case 3: {

            }
            default: {
                ......
                break;
            }

        }

你可以从doInBackground方法中以这种方式调用它

            publishProgress(number of the case that you want);