flurry是否与asynctask互动?

时间:2013-07-30 08:54:25

标签: android

我添加了

@Override
    protected void onStart() {
        super.onStart();
        FlurryAgent.onStartSession(this, "my id");
    }

    @Override
    protected void onStop() {
        super.onStop();
        FlurryAgent.onEndSession(this);
    }

我的应用程序因以下错误而崩溃:

07-30 11:49:40.049: E/AndroidRuntime(20178): FATAL EXCEPTION: FlurryAgent
07-30 11:49:40.049: E/AndroidRuntime(20178): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.view.ViewRoot.checkThread(ViewRoot.java:3165)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.view.ViewRoot.invalidateChild(ViewRoot.java:690)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:716)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.view.ViewGroup.invalidateChild(ViewGroup.java:2628)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.view.View.invalidate(View.java:5342)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.widget.TextView.checkForRelayout(TextView.java:5832)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.widget.TextView.setText(TextView.java:2871)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.widget.TextView.setText(TextView.java:2739)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.widget.TextView.setText(TextView.java:2714)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at ua.mirkvartir.android.frontend.ResultViewActivity$AsyncLoad.onPostExecute(ResultViewActivity.java:435)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at ua.mirkvartir.android.frontend.ResultViewActivity$AsyncLoad.onPostExecute(ResultViewActivity.java:1)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.os.AsyncTask.finish(AsyncTask.java:417)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.os.Looper.loop(Looper.java:150)
07-30 11:49:40.049: E/AndroidRuntime(20178):    at android.os.HandlerThread.run(HandlerThread.java:60)

onPostExecute方法中的第435行prodavetc_label_tv.setText(Html.fromHtml(prodavetc_label));

通过将所有视图操作放入asynctask到RunOnUIThread来解决这个问题很简单。我想知道flury如何与线程交互,以及为什么onPostexecute不被视为ui线程。

class AsyncLoad extends AsyncTask<String, Void, JSONObject> {
        ProgressDialog pd;

        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(app);
            pd.setOwnerActivity(app);
            pd.setTitle("Идет загрузка..");
            pd.setCancelable(true);
            pd.show();
        }

        @Override
        protected JSONObject doInBackground(String... arg0) {
            JSONObject json = JSONParser.getJSONfromURL(RequestStringCreater
                    .idSearchString());
            saveJSON = json;
            return json;
        }

        @Override
        protected void onPostExecute(JSONObject result) {
            pd.dismiss();
            saveJSON = result;
            if (result == null) {

                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage(
                        "Потеряна связь с сервером. Попробуйте позже.")
                        .setCancelable(false)
                        .setPositiveButton("закрыть",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        // do things
                                        Intent home = new Intent(context,
                                                ChooserActivity.class);
                                        home.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                        home.putExtra("user_id", "0");
                                        startActivity(home);
                                        finish();
                                    }
                                });
                AlertDialog alert = builder.create();
                alert.show();
            } else {
                try {

                    JSONArray results = result.getJSONArray("results");
                    JSONObject e = results.getJSONObject(0);
                    description = e.getString("description");
                    prodavetc_label = e.getString("user_contact");
                    price_title = e.getString("price") + " "
                            + e.getString("currency_title") + " "
                            + e.getString("pricefor_title");
                    item_title = e.getString("title");
                    ids = e.getString("id");
                    href = e.getString("href");
                    chg_data = e.getString("upddate");
                    user_firm = e.getString("user_firm");
                    phone1 = e.getString("phone1");
                    phone2 = e.getString("phone2");
                    country_title = e.getString("country_title");
                    region_title = e.getString("region_title");
                    district_title = e.getString("district_title");
                    settle_title = e.getString("settle_title");
                    street = e.getString("street");
                    rooms = e.getString("rooms");
                    floor = e.getString("floor");
                    floors = e.getString("floors");
                    sqTotal = e.getString("sqTotal");
                    sqLiving = e.getString("sqLiving");
                    sqKitchen = e.getString("sqKitchen");
                    sqLand = e.getString("sqLand");
                    not_photo = e.getString("not_photo");
                    email = e.getString("email");
                    UILApplication.coords = e.getString("coords");
                } catch (JSONException e) {
                    Log.e("log_tag", "Error parsing data" + e.toString());
                }
                UILApplication.photo_buffer = delimitary_parse(); // saving
                                                                        // data
                // for
                // photoshowintent
                TextView prodavetc_label1_tv = (TextView) findViewById(R.id.prodavetc_label1);
                TextView prodavetc_label_tv = (TextView) findViewById(R.id.prodavetc_label);
                if (prodavetc_label == null
                        || prodavetc_label.trim().length() <= 0
                        || prodavetc_label.equalsIgnoreCase("null")) {
                    prodavetc_label1_tv.setVisibility(View.GONE);
                    prodavetc_label_tv.setVisibility(View.GONE);
                } else {
                    prodavetc_label_tv.setText(Html.fromHtml(prodavetc_label));
                }
                TextView user_firm1_tv = (TextView) findViewById(R.id.user_firm1);
                TextView user_firm_tv = (TextView) findViewById(R.id.user_firm);
                if (user_firm == null || user_firm.trim().length() <= 0
                        || user_firm.equalsIgnoreCase("null")) {
                    user_firm1_tv.setVisibility(View.GONE);
                    user_firm_tv.setVisibility(View.GONE);
                } else {
                    user_firm_tv.setText(Html.fromHtml(user_firm));
                }

                Button mail_btn = (Button) findViewById(R.id.mail_btn);
                if (email == null || email.trim().length() <= 0
                        || email.equalsIgnoreCase("null")) {
                    mail_btn.setVisibility(View.GONE);
                } else {
                    mail_btn.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            Intent emailIntent = new Intent(
                                    android.content.Intent.ACTION_SEND);
                            String[] recipients = new String[] { email, "" };
                            emailIntent.putExtra(
                                    android.content.Intent.EXTRA_EMAIL,
                                    recipients);

                            emailIntent.putExtra(
                                    android.content.Intent.EXTRA_SUBJECT,
                                    "Объявление :" + item_title);

                            emailIntent.putExtra(
                                    android.content.Intent.EXTRA_TEXT,
                                    "Здравствуйте, хотел бы связаться с Вами по поводу объявления #"
                                            + ids + ".");

                            emailIntent.setType("message/rfc822");
                            startActivity(Intent.createChooser(emailIntent,
                                    "Отправить e-mail..."));
                        }
                    });
                }

                TextView photo_count = (TextView) findViewById(R.id.photo_count);
                if (empty_image_flag) {
                    photo_count.setText("0");
                } else {
                    photo_count.setText(UILApplication.photo_buffer.length
                            + "");
                }
                TextView phones1_tv = (TextView) findViewById(R.id.phones1);
                TextView phones_tv = (TextView) findViewById(R.id.phones);
                ImageView call_phome1_iv = (ImageView) findViewById(R.id.img_call_phone1);
                TableRow table_row_phone1 = (TableRow) findViewById(R.id.table_row_phone1);
                if (phone1 == null || phone1.trim().length() <= 0
                        || phone1.equalsIgnoreCase("null")) {
                    phones1_tv.setVisibility(View.GONE);
                    phones_tv.setVisibility(View.GONE);
                    call_phome1_iv.setVisibility(View.GONE);
                } else {
                    phones_tv.setText(Html.fromHtml(phone1));
                    table_row_phone1.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            String number = "tel:" + phone1.toString().trim();
                            Intent callIntent = new Intent(Intent.ACTION_CALL,
                                    Uri.parse(number));
                            startActivity(callIntent);
                        }
                    });
                }
                TextView phone21_tv = (TextView) findViewById(R.id.phones21);
                TextView phone2_tv = (TextView) findViewById(R.id.phones2);
                ImageView call_phone2_iv = (ImageView) findViewById(R.id.img_call_phone2);
                TableRow table_row_phone2 = (TableRow) findViewById(R.id.table_row_phone2);
                if (phone2 == null || phone2.trim().length() <= 0
                        || phone2.equalsIgnoreCase("null")) {
                    phone21_tv.setVisibility(View.GONE);
                    phone2_tv.setVisibility(View.GONE);
                    call_phone2_iv.setVisibility(View.GONE);
                } else {
                    phone2_tv.setText(Html.fromHtml(phone2));
                    table_row_phone2.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            String number = "tel:" + phone2.toString().trim();
                            Intent callIntent = new Intent(Intent.ACTION_CALL,
                                    Uri.parse(number));
                            startActivity(callIntent);
                        }
                    });
                }

                TextView description_tv = (TextView) findViewById(R.id.result_description);
                if (description == null || description.trim().length() <= 0
                        || description.equalsIgnoreCase("null")) {
                    description_tv.setVisibility(View.GONE);
                } else {
                    description_tv.setText(Html.fromHtml(description));
                }

                TextView country_title1_tv = (TextView) findViewById(R.id.country_title1);
                TextView country_title_tv = (TextView) findViewById(R.id.country_title);
                if (country_title == null || country_title.trim().length() <= 0
                        || country_title.equalsIgnoreCase("null")) {
                    country_title1_tv.setVisibility(View.GONE);
                    country_title_tv.setVisibility(View.GONE);
                } else {
                    country_title_tv.setText(Html.fromHtml(country_title));
                }

                TextView region_title1_tv = (TextView) findViewById(R.id.region_title1);
                TextView region_title_tv = (TextView) findViewById(R.id.region_title);
                if (region_title == null || region_title.trim().length() <= 0
                        || region_title.equalsIgnoreCase("null")) {
                    region_title1_tv.setVisibility(View.GONE);
                    region_title_tv.setVisibility(View.GONE);
                } else {
                    region_title_tv.setText(Html.fromHtml(region_title));
                }

                TextView district_title1_tv = (TextView) findViewById(R.id.district_title1);
                TextView district_title_tv = (TextView) findViewById(R.id.district_title);
                if (district_title == null
                        || district_title.trim().length() <= 0
                        || district_title.equalsIgnoreCase("null")) {
                    district_title1_tv.setVisibility(View.GONE);
                    district_title_tv.setVisibility(View.GONE);
                } else {
                    district_title_tv.setText(Html.fromHtml(district_title));
                }

                TextView settle_title1_tv = (TextView) findViewById(R.id.settle_title1);
                TextView settle_title_tv = (TextView) findViewById(R.id.settle_title);
                if (settle_title == null || settle_title.trim().length() <= 0
                        || settle_title.equalsIgnoreCase("null")) {
                    settle_title1_tv.setVisibility(View.GONE);
                    settle_title_tv.setVisibility(View.GONE);
                } else {
                    settle_title_tv.setText(Html.fromHtml(settle_title));
                }

                TextView street1_tv = (TextView) findViewById(R.id.street1);
                TextView street_tv = (TextView) findViewById(R.id.street);
                if (street == null || street.trim().length() <= 0
                        || street.equalsIgnoreCase("null")) {
                    street1_tv.setVisibility(View.GONE);
                    street_tv.setVisibility(View.GONE);
                } else {
                    street_tv.setText(Html.fromHtml(street));
                }

                TextView rooms1_tv = (TextView) findViewById(R.id.rooms1);
                TextView rooms_tv = (TextView) findViewById(R.id.rooms);
                if (rooms == null || rooms.trim().length() <= 0
                        || rooms.equalsIgnoreCase("null")
                        || rooms.equalsIgnoreCase("0")) {
                    rooms1_tv.setVisibility(View.GONE);
                    rooms_tv.setVisibility(View.GONE);
                } else {
                    rooms_tv.setText(Html.fromHtml(rooms));
                }

                TextView floor1_tv = (TextView) findViewById(R.id.floor1);
                TextView floor_tv = (TextView) findViewById(R.id.floor);
                if (floor == null || floor.trim().length() <= 0
                        || floor.equalsIgnoreCase("null")
                        || rooms.equalsIgnoreCase("0")) {
                    floor1_tv.setVisibility(View.GONE);
                    floor_tv.setVisibility(View.GONE);
                } else {
                    floor_tv.setText(Html.fromHtml(floor));
                }
                TextView floors1_tv = (TextView) findViewById(R.id.floors1);
                TextView floors_tv = (TextView) findViewById(R.id.floors);
                if (floors == null || floors.trim().length() <= 0
                        || floors.equalsIgnoreCase("null")
                        || rooms.equalsIgnoreCase("0")) {
                    floors1_tv.setVisibility(View.GONE);
                    floors_tv.setVisibility(View.GONE);
                } else {
                    floors_tv.setText(Html.fromHtml(floors));
                }
                TextView sqTotal1_tv = (TextView) findViewById(R.id.sqTotal1);
                TextView sqTotal_tv = (TextView) findViewById(R.id.sqTotal);
                if (sqTotal == null || sqTotal.trim().length() <= 0
                        || sqTotal.equalsIgnoreCase("null")
                        || sqTotal.equalsIgnoreCase("0")) {
                    sqTotal1_tv.setVisibility(View.GONE);
                    sqTotal_tv.setVisibility(View.GONE);
                } else {
                    sqTotal_tv.setText(Html.fromHtml(sqTotal) + " кв.м");
                }

                TextView sqLiving1_tv = (TextView) findViewById(R.id.sqLiving1);
                TextView sqLiving_tv = (TextView) findViewById(R.id.sqLiving);
                if (sqLiving == null || sqLiving.trim().length() <= 0
                        || sqLiving.equalsIgnoreCase("null")
                        || sqLiving.equalsIgnoreCase("0")) {
                    sqLiving1_tv.setVisibility(View.GONE);
                    sqLiving_tv.setVisibility(View.GONE);
                } else {
                    sqLiving_tv.setText(Html.fromHtml(sqLiving) + " кв.м");
                }

                TextView sqKitchen1_tv = (TextView) findViewById(R.id.sqKitchen1);
                TextView sqKitchen_tv = (TextView) findViewById(R.id.sqKitchen);
                if (sqKitchen == null || sqKitchen.trim().length() <= 0
                        || sqKitchen.equalsIgnoreCase("null")
                        || sqKitchen.equalsIgnoreCase("0")) {
                    sqKitchen1_tv.setVisibility(View.GONE);
                    sqKitchen_tv.setVisibility(View.GONE);
                } else {
                    sqKitchen_tv.setText(Html.fromHtml(sqKitchen) + " кв.м");
                }

                TextView sqLand1_tv = (TextView) findViewById(R.id.sqLand1);
                TextView sqLand_tv = (TextView) findViewById(R.id.sqLand);
                if (sqLand == null || sqLand.trim().length() <= 0
                        || sqLand.equalsIgnoreCase("null")
                        || sqLand.equalsIgnoreCase("0")) {
                    sqLand1_tv.setVisibility(View.GONE);
                    sqLand_tv.setVisibility(View.GONE);
                } else {
                    sqLand_tv.setText(Html.fromHtml(sqLand) + " кв.м");
                }
                Bundle ex = getIntent().getExtras();
                byte[] imm = ex.getByteArray("imMap");
                imSmall = (ImageView) findViewById(R.id.loaderImageView);
                Bitmap bitmap = BitmapFactory.decodeByteArray(imm, 0,
                        imm.length);
                imSmall.setImageBitmap(bitmap);

                imSmall.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        UILApplication.photo_iterator = 0;
                        bigImageCreator();
                        String[] buf_image_url = new String[UILApplication.photo_buffer_big
                                .size()];
                        buf_image_url = UILApplication.photo_buffer_big
                                .toArray(buf_image_url);
                        Intent intent = new Intent(
                                "ua.mirkvartir.android.frontend.ImageShowActivity");
                        intent.putExtra(Extra.IMAGES, buf_image_url);
                        startActivity(intent);
                    }
                });
                TextView price_tittle_tv = (TextView) findViewById(R.id.price_tittle);
                price_tittle_tv.setText(Html.fromHtml(price_title));
                TextView item_title_tv = (TextView) findViewById(R.id.item_title);
                if (item_title == null || item_title.trim().length() <= 0
                        || item_title.equalsIgnoreCase("null")) {
                    item_title_tv.setText("Нет названия");
                } else {
                    item_title_tv.setText(Html.fromHtml(item_title));
                }

                TextView ids_tv = (TextView) findViewById(R.id.ids);
                ids_tv.setText(Html.fromHtml(ids));
                TextView chg_data_tv = (TextView) findViewById(R.id.chg_data);
                chg_data_tv.setText(Html.fromHtml(chg_data));
                Button share_btn = (Button) findViewById(R.id.share_btn);
                // social sharing
                share_btn.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        final Intent intent = new Intent(Intent.ACTION_SEND);
                        intent.setType("text/plain");
                        intent.putExtra(Intent.EXTRA_SUBJECT,
                                "Отличный вариант недвижимости:");
                        intent.putExtra(Intent.EXTRA_TEXT, "Хороший вариант "
                                + href);
                        startActivity(Intent.createChooser(intent,
                                getString(R.string.app_name)));
                    }
                });
                ImageView map_button = (ImageView) findViewById(R.id.imageView5);
                if (street.length()>0 || (UILApplication.coords != null
                        && UILApplication.coords.trim().length() > 0
                        && !UILApplication.coords.equalsIgnoreCase("null")
                        && !UILApplication.coords.equalsIgnoreCase("0"))) {
                    map_button.setVisibility(View.VISIBLE);
                }
                map_button.setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {
                        String geoUriString = "geo:0,0?q="+street;
                        if (UILApplication.coords != null
                                && UILApplication.coords.trim().length() > 0
                                && !UILApplication.coords.equalsIgnoreCase("null")
                                && !UILApplication.coords.equalsIgnoreCase("0")){
                            geoUriString = "geo:"+UILApplication.coords+"?z=15";
                        }else{
                            geoUriString = "geo:0,0?q="+street;
                        }

                        Uri geoUri = Uri.parse(geoUriString);
                        Intent map = new Intent(Intent.ACTION_VIEW, geoUri);
                        startActivity(map);
//                      startActivity(new Intent(
//                              "ua.mirkvartir.android.frontend.MyMapActivity"));
                    }
                });
                Button btn_look_on_site = (Button) findViewById(R.id.btn_look_on_site);
                btn_look_on_site.setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {
                        openWebURL(href);
                    }
                });
                Button save_btn = (Button) findViewById(R.id.save_btn);
                if (UILApplication.addType == 0) {
                    save_btn.setText("В избранное");
                } else {
                    save_btn.setText("Отписатся");
                }
                save_btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        if (UILApplication.addType == 0) {
                            writeJSONtoFile(saveJSON.toString());
                        } else {
                            deleteRecord(saveJSON.toString());
                        }
                    }
                });
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

知道包,有答案http://support.flurry.com/index.php?title=Guides/s/Publishers/Tips/Android

有必要稍微扩展onStart()

public void onStart()
   {
       super.onStart();
      // Add the following code to your main Activity before calling FlurryAgent.onStartSession():
      // workaround for AsyncTask issue
       try {

      Class.forName("android.os.AsyncTask");
      }  catch (ClassNotFoundException e) {

         e.printStackTrace();
      }

      FlurryAgent.onStartSession(this, YOUR_API_KEY);
      //your code
  }