2

我有一个 loginActivity 会将我重定向到 userViewActivity,但出现以下错误:

10-03 00:50:11.628: E/WindowManager(1820): Activity com.example.mediastore.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b558aee8 V.E..... R.....ID 0,0-456,144} that was originally added here
10-03 00:50:11.628: E/WindowManager(1820): android.view.WindowLeaked: Activity com.example.mediastore.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b558aee8 V.E..... R.....ID 0,0-456,144} that was originally added here
10-03 00:50:11.628: E/WindowManager(1820):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
10-03 00:50:11.628: E/WindowManager(1820):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
10-03 00:50:11.628: E/WindowManager(1820):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-03 00:50:11.628: E/WindowManager(1820):  at android.app.Dialog.show(Dialog.java:281)
10-03 00:50:11.628: E/WindowManager(1820):  at com.example.mediastore.LoginActivity$userLogin.onPreExecute(LoginActivity.java:80)
10-03 00:50:11.628: E/WindowManager(1820):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-03 00:50:11.628: E/WindowManager(1820):  at android.os.AsyncTask.execute(AsyncTask.java:534)
10-03 00:50:11.628: E/WindowManager(1820):  at com.example.mediastore.LoginActivity$1.onClick(LoginActivity.java:63)
10-03 00:50:11.628: E/WindowManager(1820):  at android.view.View.performClick(View.java:4202)
10-03 00:50:11.628: E/WindowManager(1820):  at android.view.View$PerformClick.run(View.java:17340)
10-03 00:50:11.628: E/WindowManager(1820):  at android.os.Handler.handleCallback(Handler.java:725)
10-03 00:50:11.628: E/WindowManager(1820):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-03 00:50:11.628: E/WindowManager(1820):  at android.os.Looper.loop(Looper.java:137)
10-03 00:50:11.628: E/WindowManager(1820):  at android.app.ActivityThread.main(ActivityThread.java:5039)
10-03 00:50:11.628: E/WindowManager(1820):  at java.lang.reflect.Method.invokeNative(Native Method)
10-03 00:50:11.628: E/WindowManager(1820):  at java.lang.reflect.Method.invoke(Method.java:511)
10-03 00:50:11.628: E/WindowManager(1820):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-03 00:50:11.628: E/WindowManager(1820):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-03 00:50:11.628: E/WindowManager(1820):  at dalvik.system.NativeStart.main(Native Method)

这是登录活动:

package com.example.mediastore;

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 com.example.mediastore.JSONParser;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.InputFilter.LengthFilter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

    Button login;
    EditText inputusername;
    EditText inputpassword;
    private static String url_users = "http://10.0.2.2/mediastore/android/usersLogin.php";
    private ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();
    private static final String TAG_SUCCESS = "success";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        login = (Button) findViewById(R.id.loginbtn);
        inputusername = (EditText) findViewById(R.id.username);
        inputpassword = (EditText) findViewById(R.id.password);

        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

               if(v == login) {
                    login.setBackgroundDrawable(getResources().getDrawable(R.drawable.button2));
               }
               String u = inputusername.getText().toString();
               String p = inputpassword.getText().toString();

                if(u.equals("")||p.equals(""))
                    Toast.makeText(getApplicationContext(), "Please fill these two fields", Toast.LENGTH_SHORT).show();
                else
                    new userLogin().execute();
            }
        });
    }

    class userLogin extends AsyncTask<String, String, String> {

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

        /**
         * Creating product
         * */
        @SuppressLint("ShowToast")
        protected String doInBackground(String... args) {
            String username = inputusername.getText().toString();
            String password = inputpassword.getText().toString();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", username));
            params.add(new BasicNameValuePair("password", password));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_users,
                    "POST", params);

            // check log cat fro response
            Log.d("Login Response", json.toString());

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

                if (success == 1) {
                    // successfully created product
                    Intent i = new Intent(getApplicationContext(), UserViewActivity.class);
                    startActivity(i);

                    // closing this screen
                    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();
        }
    }

}

我对android不太熟悉,你能帮我找到这个问题的解决方案吗?

新错误:

10-03 01:30:20.138: W/dalvikvm(1831): threadid=20: thread exiting with uncaught exception (group=0xb4c90908)
10-03 01:30:20.267: D/dalvikvm(1831): GC_CONCURRENT freed 399K, 6% free 8620K/9132K, paused 5ms+27ms, total 114ms
10-03 01:30:20.267: E/AndroidRuntime(1831): FATAL EXCEPTION: AsyncTask #3
10-03 01:30:20.267: E/AndroidRuntime(1831): java.lang.RuntimeException: An error occured while executing doInBackground()
10-03 01:30:20.267: E/AndroidRuntime(1831):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at java.lang.Thread.run(Thread.java:856)
10-03 01:30:20.267: E/AndroidRuntime(1831): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.mediastore/com.example.mediastore.UserViewActivity}; have you declared this activity in your AndroidManifest.xml?
10-03 01:30:20.267: E/AndroidRuntime(1831):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at android.app.Activity.startActivityForResult(Activity.java:3370)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at android.app.Activity.startActivityForResult(Activity.java:3331)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at android.app.Activity.startActivity(Activity.java:3566)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at android.app.Activity.startActivity(Activity.java:3534)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at com.example.mediastore.LoginActivity$userLogin.doInBackground(LoginActivity.java:120)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at com.example.mediastore.LoginActivity$userLogin.doInBackground(LoginActivity.java:1)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-03 01:30:20.267: E/AndroidRuntime(1831):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-03 01:30:20.267: E/AndroidRuntime(1831):     ... 4 more
4

3 回答 3

2

我认为问题在于您正在调用finish()并且doInBackground()没有正确关闭您的ProgressDialog. 尝试覆盖你的finish()并在它打开时将其关闭

@Override
public void finish()
{
    if (pDialog != null && pDialog.isShowing())
    {
         pDialog.dismiss();
    }
}
于 2013-10-03T01:31:20.107 回答
0

我认为问题是您在完成进度对话框之前开始活动。在关闭进度对话框后开始活动。首先定义像 startActivity 这样的布尔变量

 if (success == 1) {
                // successfully created product
                startActivity=true;

                // closing this screen
                finish();
            } else {
                // failed to create product
            }

     protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
      if(startActivity)
      Intent i = new Intent(getApplicationContext(), UserViewActivity.class);
            startActivity(i);
    }
于 2013-10-03T06:14:07.297 回答
0

视图应该在 Activity 类而不是子类中声明,在设置监听器之前将这些代码行从 onPreExecute() 方法移动到 onCreate() :

  pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Logging user..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);

AsyncTask然后在您调用using的位置上方显示对话框new userLogin().execute();

        pDialog.show();

看起来@CodeMagic 明白了:尝试像这样在后台更改您的操作。请记住,布尔标志位于 doInBackground 方法的上方:

        boolean startNewActivity = false;

        protected String doInBackground(String... args) {
        String username = inputusername.getText().toString();
        String password = inputpassword.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("username", username));
        params.add(new BasicNameValuePair("password", password));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_users,
                "POST", params);

        // check log cat fro response
        Log.d("Login Response", json.toString());

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

            if (success == 1) {
                // successfully created product
                startNewActivity=true;
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    } 

现在像这样在 onPostExecute() 上启动新活动

 protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
        pDialog.dismiss();
        if(startNewActivity) {
        Intent i = new Intent(getApplicationContext(), UserViewActivity.class);
                startActivity(i);
       }
      }

     }
于 2013-10-03T01:17:51.630 回答