gpt4 book ai didi

java - Activity 泄漏了窗口 com.android.internal.policy.impl.PhoneWindow$DecorView

转载 作者:行者123 更新时间:2023-12-02 06:17:57 24 4
gpt4 key购买 nike

我在此 AddRestaurantActivity 上遇到此错误

这是我的 AddRestaurantActivity.java 文件。这是更新数据库并创建一个带有 goggle geo place 纬度和经度的新餐厅的 Activity 代码。在模拟器中,当我按“创建”按钮创建餐厅时,应用程序死掉了(不幸的是,餐厅查找器已停止)。并且在下面发现了logcat中的错误

 package admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.mad.restaurantfinderrm.R;
import com.mad.restaurantfinderrm.library.DatabaseHandler;
import com.mad.restaurantfinderrm.library.JSONParser;
import com.mad.restaurantfinderrm.library.UserFunctions;

public class AddRestaurantActivity extends Activity {

UserFunctions userFunctions;

// Progress Dialog
private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputType;
EditText inputBio;
EditText inputAddressLine1;
EditText inputAddressLine2;
EditText inputAddressLine3;
EditText inputContactNum;
EditText inputEmail;
EditText inputMenuUrl;

// url to create new Restaurant

// private static String url_create_restaurant =
// "http://192.168.0.10/~robertmcnamara/RestaurantFinderAPI/createRestaurant.php";
private static String url_create_restaurant = "http://askme- jobs.com/api/createRestaurant.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";

static Double lat;
static Double lng;

// DB
private SQLiteDatabase mDb;
private Cursor mCursor;
String user_id;

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

userFunctions = new UserFunctions();

// getting user_id
DatabaseHandler mh = new DatabaseHandler(getApplicationContext());
mDb = mh.getWritableDatabase();
mCursor = mDb.query("login", null, null, null, null, null, null);

if (mCursor != null && mCursor.getCount() > 0) {
mCursor.moveToFirst();
user_id = mCursor.getString(mCursor.getColumnIndex("uid"));
}

// Edit Text
inputName = (EditText) findViewById(R.id.inputName);
inputType = (EditText) findViewById(R.id.inputType);
inputBio = (EditText) findViewById(R.id.inputBio);
inputAddressLine1 = (EditText) findViewById(R.id.inputAddress1);
inputAddressLine2 = (EditText) findViewById(R.id.inputAddress2);
inputAddressLine3 = (EditText) findViewById(R.id.inputAddress3);
inputContactNum = (EditText) findViewById(R.id.inputContactNum);
inputEmail = (EditText) findViewById(R.id.inputEmail);
inputMenuUrl = (EditText) findViewById(R.id.inputMenuUrl);

// Create button
Button btnCreateRestaurant = (Button) findViewById(R.id.btnCreateRestaurant);

// button click event
btnCreateRestaurant.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
if (inputName.getText().toString().trim().contentEquals("")
|| inputType.getText().toString().trim()
.contentEquals("")
|| inputBio.getText().toString().trim()
.contentEquals("")
|| inputAddressLine1.getText().toString().trim()
.contentEquals("")
|| inputAddressLine2.getText().toString().trim()
.contentEquals("")
|| inputContactNum.getText().toString().trim()
.contentEquals("")
|| inputEmail.getText().toString().trim()
.contentEquals("")
|| inputMenuUrl.getText().toString().trim()
.contentEquals("")) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AddRestaurantActivity.this);

// Setting Dialog Title
alertDialog.setTitle("Input validation");

// Setting Dialog Message
alertDialog
.setMessage("Please enter all details again,\nmaking sure they are correct");

// Setting Icon to Dialog
// alertDialog.setIcon(android.R.drawable.delete);

// Setting Positive "Yes" Button
alertDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});

// Showing Alert Message
alertDialog.show();
} else {
// creating new product in background thread
new CreateNewRestaurant().execute();
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}

/*
* Event Handling for Individual menu item selected Identify single menu
* item by it's id
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case R.id.menu_logout:
// logout user
// TODO check Internet here?
userFunctions.logoutUser(getApplicationContext());
Intent login = new Intent(getApplicationContext(),
LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
// Closing dashboard screen
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

/**
* Background Async Task to Create new Restaurant
* */
class CreateNewRestaurant extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AddRestaurantActivity.this);
pDialog.setMessage("Creating Restaurant..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

/**
* Creating Restaurant
* */
protected String doInBackground(String... args) {
String name = inputName.getText().toString();
String type = inputType.getText().toString();
String bio = inputBio.getText().toString();
String addressLine1 = inputAddressLine1.getText().toString();
String addressLine2 = inputAddressLine2.getText().toString();
String addressLine3 = inputAddressLine3.getText().toString();
String phoneNum = inputContactNum.getText().toString();
String email = inputEmail.getText().toString();
String menuUrl = inputMenuUrl.getText().toString();

// img
// String img = "";

String fullAddress = name + ", " + addressLine1 + ", "
+ addressLine2 + ", " + addressLine3;
Geocoder geoCoder = new Geocoder(getApplicationContext());
List<Address> addressList;
try {
addressList = geoCoder.getFromLocationName(fullAddress, 1);
Address address = addressList.get(0);
if (address.hasLatitude() && address.hasLongitude()) {
lat = address.getLatitude();
lng = address.getLongitude();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

Log.v("LAT", lat.toString());
Log.v("LNG", lng.toString());

// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("type", type));
params.add(new BasicNameValuePair("bio", bio));
params.add(new BasicNameValuePair("addressLine1", addressLine1));
params.add(new BasicNameValuePair("addressLine2", addressLine2));
params.add(new BasicNameValuePair("addressLine3", addressLine3));
params.add(new BasicNameValuePair("contactNumber", phoneNum));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("lat", lat.toString()));
params.add(new BasicNameValuePair("lng", lng.toString()));
params.add(new BasicNameValuePair("url", menuUrl));
params.add(new BasicNameValuePair("user_id", user_id));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_restaurant,
"POST", params);

// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(),
ViewRestaurantActivity.class);
i.putExtra("MESSAGE", "add");
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
overridePendingTransition(R.anim.slide_in_top,
R.anim.slide_out_bottom);
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}

}

}

logcat 错误:

01-22 07:50:28.381: E/WindowManager(924): Activity com.mad.restaurantfinderrm.TabsViewPagerFragmentActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41891c88 V.E..... R.....I. 0,0-240,147} that was originally added here
01-22 07:50:28.381: E/WindowManager(924): android.view.WindowLeaked: Activity com.mad.restaurantfinderrm.TabsViewPagerFragmentActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41891c88 V.E..... R.....I. 0,0-240,147} that was originally added here
01-22 07:50:28.381: E/WindowManager(924): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
01-22 07:50:28.381: E/WindowManager(924): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
01-22 07:50:28.381: E/WindowManager(924): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
01-22 07:50:28.381: E/WindowManager(924): at android.app.Dialog.show(Dialog.java:281)
01-22 07:50:28.381: E/WindowManager(924): at com.mad.restaurantfinderrm.Tab2Fragment.error(Tab2Fragment.java:248)
01-22 07:50:28.381: E/WindowManager(924): at com.mad.restaurantfinderrm.Tab2Fragment.displayDeals(Tab2Fragment.java:324)
01-22 07:50:28.381: E/WindowManager(924): at com.mad.restaurantfinderrm.Tab2Fragment$LoadDeals$1.run(Tab2Fragment.java:209)
01-22 07:50:28.381: E/WindowManager(924): at android.app.Activity.runOnUiThread(Activity.java:4673)
01-22 07:50:28.381: E/WindowManager(924): at com.mad.restaurantfinderrm.Tab2Fragment$LoadDeals.onPostExecute(Tab2Fragment.java:183)
01-22 07:50:28.381: E/WindowManager(924): at com.mad.restaurantfinderrm.Tab2Fragment$LoadDeals.onPostExecute(Tab2Fragment.java:1)
01-22 07:50:28.381: E/WindowManager(924): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-22 07:50:28.381: E/WindowManager(924): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-22 07:50:28.381: E/WindowManager(924): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-22 07:50:28.381: E/WindowManager(924): at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 07:50:28.381: E/WindowManager(924): at android.os.Looper.loop(Looper.java:137)
01-22 07:50:28.381: E/WindowManager(924): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-22 07:50:28.381: E/WindowManager(924): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 07:50:28.381: E/WindowManager(924): at java.lang.reflect.Method.invoke(Method.java:525)
01-22 07:50:28.381: E/WindowManager(924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-22 07:50:28.381: E/WindowManager(924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-22 07:50:28.381: E/WindowManager(924): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您在调用 onPostExecute 之前调用 finish()。我认为这会泄露你的pDialog。尝试在关闭 pDialog

后在 onPostExecute 中移动 finish()

关于java - Activity 泄漏了窗口 com.android.internal.policy.impl.PhoneWindow$DecorView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21284047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com