Android/MySQL 运行时错误

Posted

技术标签:

【中文标题】Android/MySQL 运行时错误【英文标题】:Android/MySQL Runtime error 【发布时间】:2017-05-31 07:00:53 【问题描述】:

我正在尝试插入 mysql 数据库,并且代码可以工作并且确实插入但立即崩溃,看不到问题。

applications.loginandregister E/AndroidRuntime: FATAL EXCEPTION: main 进程:applications.loginandregister,PID:2545 android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 android.os.BinderProxy@5aa47b4 无效;是你的活动 跑步? 在 android.view.ViewRootImpl.setView(ViewRootImpl.java:849) 在 android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:337) 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 在 android.app.Dialog.show(Dialog.java:350) 在 android.app.AlertDialog$Builder.show(AlertDialog.java:1113) 在 applications.loginandregister.BackgroundTask.display(BackgroundTask.java:206) 在 applications.loginandregister.BackgroundTask.onPostExecute(BackgroundTask.java:192) 在 applications.loginandregister.BackgroundTask.onPostExecute(BackgroundTask.java:32) 在 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.Looper.loop(Looper.java:158) 在 android.app.ActivityThread.main(ActivityThread.java:7224) 在 java.lang.reflect.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】:

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

 runOnUiThread(new Runnable() 
            @Override
            public void run() 
              display("Login Failed...","Something weird happened :(.");  
            
        );

【讨论】:

不知道为什么会出现问题 - 你能详细说明一下 您正在后台线程中运行 UI 操作,因此它正在崩溃。

以上是关于Android/MySQL 运行时错误的主要内容,如果未能解决你的问题,请参考以下文章

运行Loadrunner脚本时,运行错误,提示如下:(见补充) 求高手怎么解决

JSP运行时505错误,麻烦帮我改好

运行项目时这个运行时错误是什么?

excel vba 运行时错误

在运行SQL时出现错误,请问如何解决?

QT运行出错