安卓:我执行的AsyncTask立即崩溃的应用程序 [英] Android: executing my asynctask crashes app immediately

查看:133
本文介绍了安卓:我执行的AsyncTask立即崩溃的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的TabActivity异步任务,虽然我以前曾使用它了,这一切突然停止。我必须有改变的东西。我称之为新CallWebServiceAd()执行(); 在活动的构造

I have the async task in my TabActivity, and although I have been using it before, it all of a sudden stopped. I must have changed something. I call new CallWebServiceAd().execute(); in the constructor of the activity.

我可以注释掉所有的异步任务中的code,但仅仅出现在调用的AsyncTask崩溃的应用程序。

I can comment out all of the code within the async task, but appears merely calling the asynctask crashes the app.

我得到:

01-26 16:27:04.196: E/AndroidRuntime(818): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tca.app/com.tca.app.AndroidTabLayoutActivity}: java.lang.NullPointerException
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.os.Looper.loop(Looper.java:137)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.ActivityThread.main(ActivityThread.java:4340)
01-26 16:27:04.196: E/AndroidRuntime(818):  at java.lang.reflect.Method.invokeNative(Native Method)
01-26 16:27:04.196: E/AndroidRuntime(818):  at java.lang.reflect.Method.invoke(Method.java:511)
01-26 16:27:04.196: E/AndroidRuntime(818):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-26 16:27:04.196: E/AndroidRuntime(818):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-26 16:27:04.196: E/AndroidRuntime(818):  at dalvik.system.NativeStart.main(Native Method)
01-26 16:27:04.196: E/AndroidRuntime(818): Caused by: java.lang.NullPointerException
01-26 16:27:04.196: E/AndroidRuntime(818):  at com.tca.app.AndroidTabLayoutActivity.onCreate(AndroidTabLayoutActivity.java:51)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.Activity.performCreate(Activity.java:4465)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-26 16:27:04.196: E/AndroidRuntime(818):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-26 16:27:04.196: E/AndroidRuntime(818):  ... 11 more

下面是异步任务部分:

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




        this.setTabs();
        new CallWebServiceAd().execute();

        System.out.println("Height"+tabHost.getHeight());

    }

     public String getAd(){

              RestClient client = new RestClient("http://example.com");

            try {
                client.Execute(RequestMethod.GET);
            } catch (Exception e) {
                e.printStackTrace();
            }
           String theResponse = client.getResponse();


           return theResponse;  

        }

        public void parseJSONResponse(String jsonResponse) {

            //using gson, place all the json into the SingleEvent object and then into a List
            Type listType = new TypeToken<List<AdEvent>>(){}.getType();  
            List<AdEvent> ad = new Gson().fromJson(jsonResponse, listType);

            this.displayAd(ad);



        }


        private class CallWebServiceAd extends AsyncTask<String,String,String>{

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



                return getAd();
            }


            @Override
            protected void onPostExecute(String result) {

                parseJSONResponse(result);

            }


        }

我已经添加了的onCreate。但是就像我说的,这是新的CallWebServiceAd()执行();我不知道为什么它之前的工作。

I've added the onCreate. But like I said, it is the new CallWebServiceAd().execute(); I don't know why it was working before.

推荐答案

首先,检查你的应用程序中确实下降的这个的应用程序。今天我浪费了一个小时才找到的原因是真的是在其他活动。

Firstly, check, that your app is falling really in this app. Today I wasted an hour to find that the reason was really in the other activity.

其次,如果是在这个程序,把断点的onCreate,在onStart和onResume。如果没有最后的两个,使他们。所以,你确切地知道,当它正在下降。

Secondly, if it is in this app, put breakpoints in onCreate, onStart and onResume. If you don't have the two last, make them. So, you'll know exactly, when it is falling.

第三,如果你是进入本次活动从另一方面,把同样的断点在该次活动到的onStop,和的onDestroy在onPause。

Thirdly, if you are coming into this activity from the other, put the same breakpoints in that second Activity into onStop, onDestroy and onPause.

很有可能,你会看到的解决方案。如果没有,回来,把更多的信息的问题,让我们想想。

Very probably, you'll see the solution. If not, come back, put more info in the question and let's think.

这篇关于安卓:我执行的AsyncTask立即崩溃的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆