当前位置: 首页 > 知识库问答 >
问题:

Android/MySQL运行时错误

殷安顺
2023-03-14

我试图插入到一个MySQL数据库,代码工作和插入,但立即崩溃,看不出问题。

Applications.loginandRegister e/AndroidRuntime:致命异常:主进程:Applications.loginandRegister,PID:2545 Android.view.WindowManager$BadTokenException:无法添加窗口--令牌Android.os.BinderProxy@5AA47B4无效;你的活动在进行吗?在Android.view.viewrootimpl.setView(viewrootimpl.java:849)在Android.view.windowmanagerlobal.addView(windowmanagerlobal.java:337)在Android.view.windowmanagerimpl.addView(windowmanagerlobal.java:91)在Android.view.windowmanagerimpl.addView(windowmanagerimpl.java:91)在android.app.dialog.show(dialog.java:350)在Android.app.dialog.show在Android.os.asynctask.finish(asynctask.java:651),android.os.asynctask.access$500(asynctask.java:180),android.os.asynctask$internalhandler.handleMessage(asynctask.java:668),android.os.handler.dispatchMessage(handler.java:102),android.os.handler.loop(looper.java:158),android.app.activitythread.main Method.invoke(本机方法)位于com.android.internal.os.zygoteinit$methodandargsCaller.run(zygoteinit.java:1230)在com.android.internal.os.zygoteinit.main(zygoteinit.java:1120)

package applications.loginandregister;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.renderscript.ScriptGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.Buffer;

import javax.net.ssl.HttpsURLConnection;

/**
 * Created by Mitch on 2016-06-04.
 */
public class BackgroundTask extends AsyncTask<String,Void,String>
{

    SharedPreferences preferences;
    SharedPreferences.Editor editor;

    Context context;

    BackgroundTask(Context ctx){
        this.context = ctx;
    }

    @Override
    protected String doInBackground(String... params)
    {

        preferences = context.getSharedPreferences("MYPREFS", Context.MODE_PRIVATE);
        editor = preferences.edit();
        editor.putString("flag","0");
        editor.commit();

        String urlRegistration = "http://domain.com/LoginAndRegister-register.php";
        String urlLogin  = "http://domain.com/LoginAndRegister-login.php";
        String task = params[0];

        if(task.equals("register")){
            String regName = params[1];
            String regEmail = params[2];
            String regPassword = params[3];



            try {
                URL url = new URL(urlRegistration);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
                BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
                String myData = URLEncoder.encode("identifier_name","UTF-8")+"="+URLEncoder.encode(regName,"UTF-8")+"&"
                        +URLEncoder.encode("identifier_email","UTF-8")+"="+URLEncoder.encode(regEmail,"UTF-8")+"&"
                        +URLEncoder.encode("identifier_password","UTF-8")+"="+URLEncoder.encode(regPassword,"UTF-8");
                bufferedWriter.write(myData);
                bufferedWriter.flush();
                bufferedWriter.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                inputStream.close();

                editor.putString("flag","register");
                editor.commit();
                return "Successfully Registered " + regName;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        if(task.equals("login")){
            String loginEmail = params[1];
            String loginPassword = params[2];
            try {
                URL url = new URL(urlLogin);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);

                //send the email and password to the database
                OutputStream outputStream = httpURLConnection.getOutputStream();
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
                BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
                String myData = URLEncoder.encode("identifier_loginEmail","UTF-8")+"="+URLEncoder.encode(loginEmail,"UTF-8")+"&"
                        +URLEncoder.encode("identifier_loginPassword","UTF-8")+"="+URLEncoder.encode(loginPassword,"UTF-8");
                bufferedWriter.write(myData);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

                //get response from the database
                InputStream inputStream = httpURLConnection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String dataResponse = "";
                String inputLine = "";
                while((inputLine = bufferedReader.readLine()) != null){
                    dataResponse += inputLine;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();

                System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                System.out.println(dataResponse);

                editor.putString("flag","login");
                editor.commit();
                return  dataResponse;

            } catch (MalformedURLException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    //This method willbe called when doInBackground completes... and it will return the completion string which
    //will display this toast.
    @Override
    protected void onPostExecute(String s)
    {
        String flag = preferences.getString("flag","0");


        if(flag.equals("register")) {
            Toast.makeText(context,s,Toast.LENGTH_LONG).show();
        }
        if(flag.equals("login"))
        {
            String test = "false";
            String name = "";
            String email = "";
            String[] serverResponse = s.split(",");


            test = serverResponse[0];
            name = serverResponse[1];
            email = serverResponse[2];
            //display("Message ",serverResponse[2]);

            if(test.equals("true"))
            {

               editor.putString("name",name);
               editor.commit();
               editor.putString("email",email);
               editor.commit();
               Intent intent = new Intent(context,LogginIn.class);
               context.startActivity(intent);
            }
            else
            {
               display("Login Failed...", "That email and password do not match our records :(.");
            }
        }

        else
       {
           display("Login Failed...","Something weird happened :(.");
       }
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    public void display(String title, String message){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }
}

似乎我注释掉了finish();它工作,但仍然不对显示登录失败,一些奇怪的事情发生了。

这是注册页面

public class Register extends AppCompatActivity {
    EditText etName, etEmail, etPassword;
    String name, email, password;
    Button btnRegister;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);
        etName = (EditText) findViewById(R.id.etNewName);
        etEmail = (EditText) findViewById(R.id.etNewEmail);
        etPassword = (EditText) findViewById(R.id.etNewPassword);
        btnRegister = (Button) findViewById(R.id.btnNewRegister);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = etName.getText().toString();
                email = etEmail.getText().toString();
                password = etPassword.getText().toString();
                String task = "register";
                BackgroundTask backgroundTask = new BackgroundTask(Register.this);
                backgroundTask.execute(task,name, email, password);
                //finish();


            }
        });


    }


}

共有1个答案

公冶昆杰
2023-03-14

您需要在UI线程上显示对话框。

 runOnUiThread(new Runnable() {
            @Override
            public void run() {
              display("Login Failed...","Something weird happened :(.");  
            }
        });
 类似资料:
  • 问题内容: 我的意图是读取原始资源,然后使用JSON将其转换为Java代码。执行此操作的类和外部库存储在另一个项目中。当我运行我的应用程序时,在日志猫中出现此错误: 我可以说该错误是由JSONArray的创建引起的。我已经在stackoverflow上遇到过此错误,但是没有明确的方法来解决此问题。任何人都可以向我推荐解决此问题的方法。帮助将不胜感激。 问题答案: 您正在添加似乎具有未解决依赖性的n

  • 'tools.jar'不在Android Studio类路径中。请确保JAVA_HOME指向JDK而不是JRE 这里有人能帮我吗?

  • 有时,当我使用即时运行时,我会得到这个错误(主要是在layout.xml更改之后): 失败:生成失败,出现异常。 编辑:在此处打开了google代码中的问题 编辑2:若要在cmd中重现此错误,我必须更改layout.xml(如注释视图),然后运行。 输出: 失败:生成失败,出现异常。 生成失败 总时间:11.114秒

  • 如果你能帮我解决这个问题,我会非常非常感激的。 我已经审阅了logcat,但不能理解它,除了按钮(onClick)是问题所在。我不知道怎么修好它。 这在logcat中每隔几秒钟就会以红色显示:E/TZ_CCM_Server:仅支持“CCM” 以下是与运行时崩溃相关的完整错误日志: 编辑:MainActivity/java代码: XML代码:

  • 当我启动Android Studio时,我遇到此错误: 当我单击“重试”时,会出现相同的错误:

  • 当我想运行反应-本机运行-Android命令我看到 失败:构建失败,有一个异常。 > 哪里出错了:配置根项目'reactNativeCliTest'时出现问题。 无法解析配置“:classpath”的所有依赖项。无法下载gradle core。jar(com.android.tools.build:gradle-core:2.2.3)无法获取资源的https://jcenter.bintray.c