Android nullpointer异常并且无法在未调用Looper.prepare()的线程内创建处理程序(Android nullpointer exception and Can't

编程入门 行业动态 更新时间:2024-10-24 20:13:20
Android nullpointer异常并且无法在未调用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(); } } }

}

这是我的logcat

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)

Whats wrong with this class, I am getting error as "Can't create handler inside thread that has not called Looper.prepare()" and error is pointing at the pose Execution of 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(); } } }

}

Here is my logcat

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)上使用的toast上使用了ComposeActivity.this而不是getApplicationContext() protected void onPostExecute(JSONObject json)

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

After all . I did some small change in my code and it worked. I did following changes.

Declare the pDialog on top class i.e

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

I used ComposeActivity.this instead of getApplicationContext() on the toast I used on protected void onPostExecute(JSONObject json)

And the problem is solved. But I still don't get why previous implementation does not work as it was working some other classes .

更多推荐

本文发布于:2023-08-07 06:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   异常   程序   prepare   Looper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!