0

I've dont know where's the error is...
I've try to debug it but error keeps coming and coming.

 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)

What's the problem here? Help me please.

4

4 回答 4

3

当视图无法找到启动或初始化它的上下文时,通常会出现泄漏窗口错误。

在开始新活动之前先关闭进度对话框。就像当您的前一个活动对话框没有找到相同的上下文时您的新活动开始时一样。因此,在 onpause 方法或 on destroy 方法中编写您的对话框关闭代码。

在这里,您正在 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();
            }

        }
于 2013-11-14T07:15:01.337 回答
1

您将在关闭对话框之前完成活动。试试这个。

@Override
protected void onDestroy() {
   if (progressDialog != null) {
      if (progressDialog.isShowing()) {
          progressDialog.dismiss();
      }
   }
  super.onDestroy();
}
于 2013-11-14T06:56:36.113 回答
0

在开始新活动之前,您正在 doInBackground 中开始新的意图,您需要取消 ProgressDialog

我更喜欢这个代码来关闭 ProgressDialog。pd 是下面代码中的 ProgressDialog

    @Override
public void startActivity(Intent intent) {
    if(pd!=null && pd.isShowing()){
        pd.dismiss();
    }
    super.startActivity(intent);
}
于 2013-11-14T06:57:33.323 回答
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();


            }

        }

    }
于 2013-11-14T07:00:30.917 回答