当我点击添加朋友按钮时,我的json给我成功的响应和调试器进来如果条件和调试器来了如果阻止我的应用程序hAs不幸停止.....当条件错误时App运行正常
这是我的Logcate
06-13 15:27:30.700: E/AndroidRuntime(22157): FATAL EXCEPTION: AsyncTask #4
06-13 15:27:30.700: E/AndroidRuntime(22157): java.lang.RuntimeException: An error occured while executing doInBackground()
06-13 15:27:30.700: E/AndroidRuntime(22157): at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-13 15:27:30.700: E/AndroidRuntime(22157): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-13 15:27:30.700: E/AndroidRuntime(22157): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-13 15:27:30.700: E/AndroidRuntime(22157): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-13 15:27:30.700: E/AndroidRuntime(22157): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-13 15:27:30.700: E/AndroidRuntime(22157): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-13 15:27:30.700: E/AndroidRuntime(22157): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-13 15:27:30.700: E/AndroidRuntime(22157): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-13 15:27:30.700: E/AndroidRuntime(22157): at java.lang.Thread.run(Thread.java:856)
06-13 15:27:30.700: E/AndroidRuntime(22157): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-13 15:27:30.700: E/AndroidRuntime(22157): at android.os.Handler.<init>(Handler.java:121)
06-13 15:27:30.700: E/AndroidRuntime(22157): at android.app.Dialog.<init>(Dialog.java:107)
06-13 15:27:30.700: E/AndroidRuntime(22157): at android.app.AlertDialog.<init>(AlertDialog.java:114)
06-13 15:27:30.700: E/AndroidRuntime(22157): at android.app.AlertDialog$Builder.create(AlertDialog.java:913)
06-13 15:27:30.700: E/AndroidRuntime(22157): at com.example.phonebook.UserByIdProfile$addfrnd.doInBackground(UserByIdProfile.java:133)
06-13 15:27:30.700: E/AndroidRuntime(22157): at com.example.phonebook.UserByIdProfile$addfrnd.doInBackground(UserByIdProfile.java:1)
06-13 15:27:30.700: E/AndroidRuntime(22157): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-13 15:27:30.700: E/AndroidRuntime(22157): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-13 15:27:30.700: E/AndroidRuntime(22157): ... 5 more
06-13 15:27:31.440: E/WindowManager(22157): Activity com.example.phonebook.UserByIdProfile has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42237718 that was originally added here
06-13 15:27:31.440: E/WindowManager(22157): android.view.WindowLeaked: Activity com.example.phonebook.UserByIdProfile has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42237718 that was originally added here
06-13 15:27:31.440: E/WindowManager(22157): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
06-13 15:27:31.440: E/WindowManager(22157): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
06-13 15:27:31.440: E/WindowManager(22157): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
06-13 15:27:31.440: E/WindowManager(22157): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
06-13 15:27:31.440: E/WindowManager(22157): at android.view.Window$LocalWindowManager.addView(Window.java:547)
06-13 15:27:31.440: E/WindowManager(22157): at android.app.Dialog.show(Dialog.java:277)
06-13 15:27:31.440: E/WindowManager(22157): at com.example.phonebook.UserByIdProfile$addfrnd.onPreExecute(UserByIdProfile.java:103)
这是我的代码
protected String doInBackground(String... arg0) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
UserByIdModel tempuser = (UserByIdModel) getIntent().getSerializableExtra("UserById");
final UserModel user = (UserModel) getIntent().getSerializableExtra("User");
String SenderId=String.valueOf(user.getUser_Id());
//UserModel u=new UserModel();
//String SenderId=u.getUser_Id().toString();
String ReqesterId=String.valueOf(tempuser.getUser_Id());
params.add(new BasicNameValuePair("SenderUserID",SenderId));
params.add(new BasicNameValuePair("ReceiverUserID", ReqesterId));
JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);
Log.d("Create Response", json.toString());
try {
if(json!=null)
{
if(json.has("msg"))
{
String message=json.getString("msg");
if(message.equals(TAG_MGS))
{
AlertDialog alertDialog = new AlertDialog.Builder(
UserByIdProfile.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Your Friend Request has Been sent");
// Setting Icon to Dialog
//alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Write your code here to e
finish();
}
});
// Showing Alert Message
alertDialog.show();
//Toast.makeText(UserByIdProfile.this,"Your Friend Request Has Been Sent", Toast.LENGTH_LONG).show();
}
else
{
flag=1;
}
}
} // return null;
}
catch (JSONException e) {
e.printStackTrace();
}
//flag=1;
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
if(flag==1)
Toast.makeText(UserByIdProfile.this,"Your friend request has already sent!", Toast.LENGTH_SHORT).show();
}
}
}
我的提示框不起作用
答案 0 :(得分:2)
您正在使用doInbackground
中的alertdialog。在ui线程上调用doInbackground
。所以你不应该在后台线程上更新ui。使用onPostExecute
并根据doInbackground
中返回的结果显示alertdialog。在ui线程上调用onPostExecute
。
答案 1 :(得分:1)
所有用户交互部分应仅在UI线程上完成,因此请删除Alert Dialog
代码表单doInBackground
并在UI线程中调用它。喜欢这个
protected String doInBackground(String... arg0) {
.
.
.
try {
if(json!=null)
{
if(json.has("msg"))
{
return json.getString("msg");
}
.
.
.
}
catch (JSONException e) {
e.printStackTrace();
}
//flag=1;
return null;
}
protected void onPostExecute(String msg) {
pDialog.dismiss();
if(msg != null && msg.equals(TAG_MGS)){
AlertDialog alertDialog = new AlertDialog.Builder(
UserByIdProfile.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Your Friend Request has Been sent");
// Setting Icon to Dialog
//alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Write your code here to e
finish();
}
});
// Showing Alert Message
alertDialog.show();
}
.
.
...
}