我不知道错误在哪里...
我试着调试它,但错误不断出现。
public class ActivityOwner extends Activity implements OnClickListener {
private ProgressDialog progressDialog;
private Button btnLogin;
private EditText username, password;
private TextView tvLinkToRegister;
JSONParser jsonParser = new JSONParser(); //---JSON parser class
//---Testing on android device:
private static final String OWNER_LOGIN_URL = "http://192.168.2.5/idot/owner_login.php";
//---Testing on Emulator:
//private static final String REGISTER_URL = "http://10.0.2.2:1234/idot/register.php";
//---Testing from a real server:
//Private static final String REGISTER_URL = "http://domain name/sub folder/register.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_owner);
username = (EditText) findViewById(R.id.etOwnerUsername);
password = (EditText) findViewById(R.id.etOwnerPassword);
tvLinkToRegister = (TextView) findViewById(R.id.link_to_register);
btnLogin = (Button) findViewById(R.id.btnOwnerLogin);
tvLinkToRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent registrationForm = new Intent(ActivityOwner.this, ActivityOwnerRegister.class);
startActivity(registrationForm);
}
});
btnLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
new AttemptLogin().execute();
}
class AttemptLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(ActivityOwner.this);
progressDialog.setMessage("Attempting login...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(true);
progressDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String stringUsername = username.getText().toString();
String stringPassword = password.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("owner_username", stringUsername));
params.add(new BasicNameValuePair("owner_password", stringPassword));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(OWNER_LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
// save user data
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ActivityOwner.this);
Editor edit = sp.edit();
edit.putString("owner_username", stringUsername);
edit.commit();
Intent i = new Intent(ActivityOwner.this, ActivityOwnerSuccessLogin.class);
//progressDialog.dismiss();
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
progressDialog.dismiss();
if (file_url != null) {
Toast.makeText(ActivityOwner.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
-LOGCAT -------
11-14 14:37:14.460: E/WindowManager(11609): Activity ph.idot.sp.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4101cf78 that was originally added here
11-14 14:37:14.460: E/WindowManager(11609): android.view.WindowLeaked: Activity ph.idot.sp.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4101cf78 that was originally added here
11-14 14:37:14.460: E/WindowManager(11609): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:349)
11-14 14:37:14.460: E/WindowManager(11609): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
11-14 14:37:14.460: E/WindowManager(11609): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
11-14 14:37:14.460: E/WindowManager(11609): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
11-14 14:37:14.460: E/WindowManager(11609): at android.view.Window$LocalWindowManager.addView(Window.java:537)
11-14 14:37:14.460: E/WindowManager(11609): at android.app.Dialog.show(Dialog.java:285)
11-14 14:37:14.460: E/WindowManager(11609): at ph.idot.owner.ActivityOwner$AttemptLogin.onPreExecute(ActivityOwner.java:88)
11-14 14:37:14.460: E/WindowManager(11609): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
11-14 14:37:14.460: E/WindowManager(11609): at android.os.AsyncTask.execute(AsyncTask.java:511)
11-14 14:37:14.460: E/WindowManager(11609): at ph.idot.owner.ActivityOwner.onClick(ActivityOwner.java:76)
11-14 14:37:14.460: E/WindowManager(11609): at android.view.View.performClick(View.java:3511)
11-14 14:37:14.460: E/WindowManager(11609): at android.view.View$PerformClick.run(View.java:14109)
11-14 14:37:14.460: E/WindowManager(11609): at android.os.Handler.handleCallback(Handler.java:605)
11-14 14:37:14.460: E/WindowManager(11609): at android.os.Handler.dispatchMessage(Handler.java:92)
11-14 14:37:14.460: E/WindowManager(11609): at android.os.Looper.loop(Looper.java:137)
11-14 14:37:14.460: E/WindowManager(11609): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-14 14:37:14.460: E/WindowManager(11609): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 14:37:14.460: E/WindowManager(11609): at java.lang.reflect.Method.invoke(Method.java:511)
11-14 14:37:14.460: E/WindowManager(11609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-14 14:37:14.460: E/WindowManager(11609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-14 14:37:14.460: E/WindowManager(11609): at dalvik.system.NativeStart.main(Native Method)
这里有什么问题?请帮帮我。
答案 0 :(得分:3)
泄漏窗口当视图无法找到启动或初始化的上下文时,通常会出现错误。
在开始新活动之前首先关闭进度对话框。 当您的新活动开始时,您的上一个活动对话框未找到相同的上下文。 因此,请在onpause方法或destroy方法中编写对话框dismiss代码。
在这里您正在使用doinbackground方法启动活动并在postexecute中完成对话框,这会导致泄漏窗口错误。
试试这个:
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
if(success==1){
if (progressDialog != null) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
//Start your activity here . . . .
}
}
if (file_url != null) {
Toast.makeText(ActivityOwner.this, file_url, Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:1)
您要在关闭对话框之前完成活动。 试试这个。
@Override
protected void onDestroy() {
if (progressDialog != null) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
super.onDestroy();
}
答案 2 :(得分:0)
你正在doInBackground中开始新的意图 在开始新活动之前,您需要关闭ProgressDialog
我更喜欢这段代码来解雇ProgressDialog。 pd是以下代码中的ProgressDialog
@Override
public void startActivity(Intent intent) {
if(pd!=null && pd.isShowing()){
pd.dismiss();
}
super.startActivity(intent);
}
答案 3 :(得分:0)
public class ActivityOwner extends Activity implements OnClickListener {
private ProgressDialog progressDialog;
private Button btnLogin;
private EditText username, password;
private TextView tvLinkToRegister;
JSONParser jsonParser = new JSONParser(); //---JSON parser class
//---Testing on android device:
private static final String OWNER_LOGIN_URL = "http://192.168.2.5/idot/owner_login.php";
//---Testing on Emulator:
//private static final String REGISTER_URL = "http://10.0.2.2:1234/idot/register.php";
//---Testing from a real server:
//Private static final String REGISTER_URL = "http://domain name/sub folder/register.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_owner);
username = (EditText) findViewById(R.id.etOwnerUsername);
password = (EditText) findViewById(R.id.etOwnerPassword);
tvLinkToRegister = (TextView) findViewById(R.id.link_to_register);
btnLogin = (Button) findViewById(R.id.btnOwnerLogin);
tvLinkToRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent registrationForm = new Intent(ActivityOwner.this, ActivityOwnerRegister.class);
startActivity(registrationForm);
}
});
btnLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
new AttemptLogin().execute();
}
class AttemptLogin extends AsyncTask<String, String, String> {
int success;
String stringUsername = username.getText().toString();
String stringPassword = password.getText().toString();
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(ActivityOwner.this);
progressDialog.setMessage("Attempting login...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(true);
progressDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("owner_username", stringUsername));
params.add(new BasicNameValuePair("owner_password", stringPassword));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(OWNER_LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
progressDialog.dismiss();
if (file_url != null) {
Toast.makeText(ActivityOwner.this, file_url, Toast.LENGTH_LONG).show();
if (success == 1) {
Log.d("Login Successful!", json.toString());
// save user data
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ActivityOwner.this);
Editor edit = sp.edit();
edit.putString("owner_username", stringUsername);
edit.commit();
Intent i = new Intent(ActivityOwner.this, ActivityOwnerSuccessLogin.class);
startActivity(i);
finish();
}
}
}