繁体   English   中英

Android空指针异常,并且无法在尚未调用Looper.prepare()的线程内创建处理程序

[英]Android nullpointer exception and Can't create handler inside thread that has not called 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; .................... .................

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

问题就解决了。 但是我仍然不理解为什么以前的实现不能和其他的类一起工作的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM