Android 空指针异常和无法在未调用 Looper.prepare() 的线程中创建处理程序



这个类有什么问题,我收到错误,因为"无法在未调用 Looper.prepare() 的线程中创建处理程序",并且错误指向姿势 MessageSend 的执行。

public class ComposeActivity extends Activity {
    String receiver,subject,message;
    TextView compose_to;
    EditText compose_subject;
    TextView compose_msg_label;
    EditText compose_message;
    TextView compose_error;
    Button send_pm;
    ConnectionDetector cd ;
    AlertDialogManager alert;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_compose);
        getActionBar().setDisplayHomeAsUpEnabled(true); // Put the back button in title
        Bundle bundle = getIntent().getExtras();
        /*
         * Instantiate objects
         */
        cd = new ConnectionDetector(getApplicationContext()); // Connection Detector
        alert = new AlertDialogManager(); // Alert Dialog
        /* 
         * Assign view to variable
         */
        compose_to = (TextView)findViewById(R.id.compose_to);
        compose_subject = (EditText)findViewById(R.id.compose_subject);
        compose_msg_label = (TextView)findViewById(R.id.compose_message_label);
        compose_message = (EditText)findViewById(R.id.compose_message);
        compose_error = (TextView)findViewById(R.id.compose_error);
        send_pm = (Button)findViewById(R.id.send_pm);
    /*
     * Check for the passed receiver
     */
    if(bundle != null){
        receiver = bundle.getString("to");
        compose_to.setText(receiver);
    }
    /*
     * send_pm button click
     */
    send_pm.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View arg0) {
            subject = compose_subject.getText().toString();
            message = compose_message.getText().toString();
            /*
             * check emptyness
             */
            if(!subject.equals("") && !message.equals("")){
                if(cd.isConnectingToInternet()){
                    //Toast.makeText(getApplicationContext(),"Correct", Toast.LENGTH_SHORT).show();
                    new MessageSend().execute();
                }else{
                    alert.showAlertDialog(ComposeActivity.this, getResources().getString(R.string.connection_error_head), getResources().getString(R.string.connection_error), null);
                }
            }else if(subject.equals("")){
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.empty_subject), Toast.LENGTH_SHORT).show();
            }else if(message.equals("")){
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.empty_message), Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.toast_blank_input), Toast.LENGTH_SHORT).show();
            }
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.compose, menu);
    return true;
}
/*
 * 
 */
private class MessageSend extends AsyncTask<String, String, JSONObject> {
     private ProgressDialog pDialog;
     String loc;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
             Bundle bundle = getIntent().getExtras();
             //id = bundle.getString("siteid");
             pDialog = new ProgressDialog(ComposeActivity.this);
             pDialog.setMessage("Sending Message ...");
             pDialog.setIndeterminate(false);
             pDialog.setCancelable(true);
             pDialog.show();
        }
        @Override
        protected JSONObject doInBackground(String... args) {
            //user = db.getUserDetails();
            UserFunctions userFunction = new UserFunctions();
            //JSONObject json = userFunction.deleteSite(id);
            return null;
        }
        @Override
        protected void onPostExecute(JSONObject json) {
                try {
                    if(json.has("success_msg")){
                        pDialog.dismiss();
                        Intent intent = new Intent(ComposeActivity.this,SitePage.class);
                        startActivity(intent);  
                        Toast.makeText(getApplicationContext(), json.getString("success_msg"), Toast.LENGTH_SHORT).show();
                    }else{
                        pDialog.dismiss();
                        String err = json.getString("error_msg");
                        Toast.makeText(getApplicationContext(), err, Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        }
}   

}

这是我的日志猫

02-21 22:32:10.655: E/AndroidRuntime(2403): FATAL EXCEPTION: main
02-21 22:32:10.655: E/AndroidRuntime(2403): java.lang.NullPointerException
02-21 22:32:10.655: E/AndroidRuntime(2403):     at com.Creators.biotrack.ComposeActivity$MessageSend.onPostExecute(ComposeActivity.java:131)
02-21 22:32:10.655: E/AndroidRuntime(2403):     at com.Creators.biotrack.ComposeActivity$MessageSend.onPostExecute(ComposeActivity.java:1)
02-21 22:32:10.655: E/AndroidRuntime(2403):     at android.os.AsyncTask.finish(AsyncTask.java:631)
02-21 22:32:10.655: E/AndroidRuntime(2403):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-21 22:32:10.655: E/AndroidRuntime(2403):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-21 22:32:10.655: E/AndroidRuntime(2403):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 22:32:10.655: E/AndroidRuntime(2403):     at android.os.Looper.loop(Looper.java:137)
02-21 22:32:10.655: E/AndroidRuntime(2403):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-21 22:32:10.655: E/AndroidRuntime(2403):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 22:32:10.655: E/AndroidRuntime(2403):     at java.lang.reflect.Method.invoke(Method.java:525)
02-21 22:32:10.655: E/AndroidRuntime(2403):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-21 22:32:10.655: E/AndroidRuntime(2403):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-21 22:32:10.655: E/AndroidRuntime(2403):     at dalvik.system.NativeStart.main(Native Method)

毕竟.我在代码中做了一些小的更改,它奏效了。我做了以下更改。

  • 在顶级类上声明 pDialog,即

    public class ComposeActivity extends Activity { public ProgressDialog pDialog; String receiver,subject,message; TextView compose_to; .................... .................

  • 我在吐司上用了ComposeActivity.this而不是getApplicationContext() protected void onPostExecute(JSONObject json)

问题解决了。但是我仍然不明白为什么以前的实现不起作用,因为它正在处理其他一些类。

最新更新