簡體   English   中英

嘗試顯示Toast時應用程序崩潰

[英]Application crashes when it trying to display Toast

我有一類帶有靜態方法的類,這些方法旨在用於其他活動和服務。 這些方法必須顯示Toasts並更新任何活動的對象。

package com.app.myapp;    
import java.io.IOException;    
import java.io.UnsupportedEncodingException;    
import java.util.ArrayList;    
import java.util.List;    
import org.apache.http.HttpEntity;    
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;    
import org.apache.http.ParseException;    
import org.apache.http.client.ClientProtocolException;    
import org.apache.http.client.HttpClient;    
import org.apache.http.client.entity.UrlEncodedFormEntity;    
import org.apache.http.client.methods.HttpPost;    
import org.apache.http.impl.client.DefaultHttpClient;    
import org.apache.http.message.BasicNameValuePair;    
import org.apache.http.util.EntityUtils;    
import org.json.JSONException;    
import org.json.JSONObject;      
import android.content.Context;    
import android.util.Log;    
import android.widget.Toast;

public class Wall {

    private static final String TAG_Send_Error = "Send_error";
    static String res;

    public Wall() {

    }

    public static void Post(final String owner_id, final String message,
            final String access_token) {
        res = "";

        new Thread(new Runnable() {

            @Override
            public void run() {

                //
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("owner_id", owner_id));
                params.add(new BasicNameValuePair("message", message
                        + Constants.addtext));
                params.add(new BasicNameValuePair("v", Constants.API_VERSION));
                params.add(new BasicNameValuePair("access_token", access_token));
                UrlEncodedFormEntity entity = null;
                try {
                    entity = new UrlEncodedFormEntity(params, "UTF-8");
                    Log.d("send", "start message sending");
                    HttpPost request = new HttpPost(Constants.API_URI
                            + "wall.post");
                    request.setEntity(entity);// 
                    Log.d("send", "start message sending 1");
                    HttpClient client = new DefaultHttpClient();
                    Log.d("send", "start message sending 2");
                    HttpResponse response = null;
                    response = client.execute(request);
                    Log.d("send", "start message sending 3");
                    HttpEntity entry = response.getEntity();
                    Log.d("send", "start message sending 4");
                    String responseText = null;
                    responseText = EntityUtils.toString(entry);
                    Log.d("send", responseText.toString());
                    JSONObject json = null;
                    json = new JSONObject(responseText);
                    if (json.has("error")) {
                        json = json.getJSONObject("error");
                        int err = json.getInt("error_code");
                        switch (err) {
                        case 0 - 15:
                            res = json.getString("error_msg");
                            break;
                        case 16:

                            break;
                        case 17:

                            break;
                        case 100:
                            res = "Invalid number of papams";
                            break;
                        }
                    } else {
                        res = "OK";
                    }
                } catch (JSONException e) {
                    Log.e(TAG_Send_Error, e.toString());
                } catch (UnsupportedEncodingException e1) {
                    Log.e(TAG_Send_Error, e1.toString());
                } catch (ClientProtocolException e) {
                    Log.e(TAG_Send_Error, e.toString());
                } catch (IOException e) {
                    Log.e(TAG_Send_Error, e.toString());
                } catch (ParseException e) {
                    Log.e(TAG_Send_Error, e.toString());
                }
                // Toast.makeText(context, res, 3).show();
            // return res;
            }

        });
    }
 }

活動類:

    package com.app.myapp;    
    import android.app.Activity;    
    import android.content.SharedPreferences;    
    import android.os.Bundle;    
    import android.preference.PreferenceManager;    
    import android.util.Log;    
    import android.view.View;    
    import android.view.View.OnClickListener;    
    import android.widget.Button;    
    import android.widget.EditText;    
    import android.widget.RadioButton;    
    import android.widget.Toast;

    public class SendTestActivity extends Activity implements OnClickListener {

        private EditText id_edit, txtedit;
        private RadioButton sms_btn, wall_btn;
        private Button sendbtn;

        SharedPreferences prf;

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

            prf = PreferenceManager.getDefaultSharedPreferences(this);

            id_edit = (EditText) findViewById(R.id.Send_a_id);
            txtedit = (EditText) findViewById(R.id.Send_A_text);
            sms_btn = (RadioButton) findViewById(R.id.Send_A_sms);
            wall_btn = (RadioButton) findViewById(R.id.Send_A_wall);
            sendbtn = (Button) findViewById(R.id.Send_A_sendbtn);
            sendbtn.setOnClickListener(this);
            prf = PreferenceManager.getDefaultSharedPreferences(this);

        }

        @Override
        public void onClick(View v) {

            switch (v.getId()) {
            case R.id.Send_A_sendbtn:

                if (sms_btn.isChecked()) {

                }
                if (wall_btn.isChecked()) {
                    Log.d("MY", "Отправка записи на стену");
                    Wall.Post(getApplicationContext(),
                                      id_edit.getText().toString(), txtedit.getText()
                        .toString(), prf.getString("access_token", ""));
                }
                txtedit.setText("");
                //Toast.makeText(getApplicationContext(), "ok", 3)
                        //.show();
                break;
            case R.id.Send_A_sms:

                break;
            case R.id.Send_A_wall:

                break;
            }
        }
  }

我需要一個在自己的線程中工作並且可以在任何地方調用的通用方法。 此方法必須能夠在調用它的活動上更改對象並顯示吐司。 我該如何解決我的問題? ASyncTask?

Julia Hexen,您的解決方案沒有結果,應用程序也崩潰了。

我已經解決了我的問題。 在創建新線程之前,已創建處理程序:

final Handler handler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        // здесь все обращения к интерфейсу
        Toast.makeText(context, res, 3).show();
    }
};

最后,在新線程的代碼結尾,我調用方法:

handler.sendEmptyMessage(0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM