致命异常:AsyncTask #1 java.lang.RuntimeException:执行 doInBackground() 时发生错误

Posted

技术标签:

【中文标题】致命异常:AsyncTask #1 java.lang.RuntimeException:执行 doInBackground() 时发生错误【英文标题】:FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occurred while executing doInBackground() 【发布时间】:2018-01-11 11:28:19 【问题描述】:

我正在做一个登录的应用程序,昨天一切正常,但今天 android studio 给了我这个错误

08-03 14:29:07.894 13703-14015/complete.lyne.myapplication E/AndroidRuntime: 致命异常: AsyncTask #1 进程:complete.lyne.myapplication,PID:13703 java.lang.RuntimeException:执行 doInBackground() 时出错 在 android.os.AsyncTask$3.done(AsyncTask.java:318) 在 java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) 在 java.util.concurrent.FutureTask.setException(FutureTask.java:223) 在 java.util.concurrent.FutureTask.run(FutureTask.java:242) 在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 在 java.lang.Thread.run(Thread.java:761) 引起:java.lang.IllegalArgumentException:意外的 url:192.168.1.100/lyne/logar.php 在 okhttp3.Request$Builder.url(Request.java:143) 在 complete.lyne.myapplication.Conexao.postDados(Conexao.java:23) 在 complete.lyne.myapplication.Login$SolicitaDados.doInBackground(Login.java:91) 在 complete.lyne.myapplication.Login$SolicitaDados.doInBackground(Login.java:87) 在 android.os.AsyncTask$2.call(AsyncTask.java:304) 在 java.util.concurrent.FutureTask.run(FutureTask.java:237) 在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 在 java.lang.Thread.run(Thread.java:761) 08-03 14:29:08.039 13703-13756/complete.lyne.myapplication D/OpenGLRenderer: endAllActiveAnimators on 0xa5420f00 (RippleDrawable) with handle 0x8e8d5af0

这是创建帐户时的错误

package complete.lyne.myapplication;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Cadastro extends AppCompatActivity 

EditText criarNome, criarSobre, criarEmail, criarSenha;
TextView refpLogin;
Button btCriar;

String url = "";
String parametro = "";

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cadastro);

    criarNome = (EditText)findViewById(R.id.criarNome);
    criarSobre = (EditText)findViewById(R.id.criarSobre);
    criarEmail = (EditText)findViewById(R.id.criarEmail);
    criarSenha = (EditText)findViewById(R.id.criarSenha);
    btCriar = (Button)findViewById(R.id.btCriar);
    refpLogin = (TextView)findViewById(R.id.refpLogin);

    refpLogin.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            Intent abreLogin = new Intent(Cadastro.this, Login.class);
            startActivity(abreLogin);
        
    );

    btCriar.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 

            ConnectivityManager connectivityManager = (ConnectivityManager)
                    getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isConnected()) 

                String criaNome = criarNome.getText().toString();
                String criaSobre = criarSobre.getText().toString();
                String criaEmail = criarEmail.getText().toString();
                String criaSenha = criarSenha.getText().toString();


                if(criarNome.getText().length()==0) 
                    criarNome.setError("Por favor insira seu Nome.");
                 else if(criarSobre.getText().length()==0) 
                    criarSobre.setError("Por favor insira seu Sobrenome.");
                 else if(criarEmail.getText().length()==0) 
                    criarEmail.setError("Por favor insira seu endereço de Email.");
                 else if(criarSenha.getText().length()==0) 
                    criarSenha.setError("Por favor insira uma senha.");
                 else if(criarSenha.getText().length()<8) 
                    criarSenha.setError("Sua senha deve conter no mínimo 8 caracteres.");
                 else if (criaNome.isEmpty() && criaSobre.isEmpty() && criaEmail.isEmpty() && criaSenha.isEmpty()) 
                    Toast.makeText(getApplicationContext(), "Nenhum campo pode ficar vazio.", Toast.LENGTH_LONG).show();
                 else 

                    // Casa
                    url = "192.168.1.100/lyne/registrar.php";

                    // Badran
                    // url = "http://172.16.2.15/lyne/logar.php";

                    parametro = "nome" + criaNome + "&sobrenome" + criaSobre + "&email=" + criaEmail + "&senha=" + criaSenha;

                    new SolicitaDados().execute(url);

                

             else 
                Toast.makeText(getApplicationContext(), "Nenhuma conexão com a Internet foi encontrada.", Toast.LENGTH_LONG).show();
            

        
    );



private class SolicitaDados extends AsyncTask<String, Void, String> 
    @Override
    protected String doInBackground(String... urls) 

        return Conexao.postDados(urls[0], parametro);

    

    @Override
    protected void onPostExecute(String resultado) 

        if(resultado.contains("email_erro")) 
            criarEmail.setError("Esse endereço de Email já está cadastrado!");
         else if (resultado.contains("registro_ok")) 
                Intent abreLogin2 = new Intent(Cadastro.this, Login.class);
                startActivity(abreLogin2);
             else 
                Toast.makeText(getApplicationContext(), "Ocorreu algum erro", Toast.LENGTH_LONG).show();
            
        
    

@Override
protected void onPause() 
    super.onPause();
    finish();




这里是登录

package complete.lyne.myapplication;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

public class Login extends AppCompatActivity 

EditText loginEmail, loginSenha;
Button btLogar;
TextView refCadastrar;

String url = "";
String parametro = "";

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    loginEmail = (EditText)findViewById(R.id.loginEmail);
    loginSenha = (EditText)findViewById(R.id.loginSenha);
    btLogar = (Button)findViewById(R.id.btLogar);
    refCadastrar = (TextView)findViewById(R.id.refCadastrar);

    refCadastrar.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            Intent abreCadastro = new Intent(Login.this, Cadastro.class);
            startActivity(abreCadastro);
        
    );

    btLogar.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 

            ConnectivityManager connectivityManager = (ConnectivityManager)
                    getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isConnected()) 

                String email = loginEmail.getText().toString();
                String senha = loginSenha.getText().toString();

                if(email.isEmpty() && senha.matches(".*[a-z].*")) 
                    loginEmail.setError("Insira seu endereço de Email.");
                 else if (email.matches(".*[a-z].*") && senha.isEmpty()) 
                    loginSenha.setError("Insira sua Senha.");
                 else if (email.isEmpty() && senha.isEmpty()) 
                        Toast.makeText(getApplicationContext(), "Nenhum campo pode ficar vazio.", Toast.LENGTH_LONG).show();
                 else 

                    // Casa
                    url = "192.168.1.100/lyne/logar.php";

                    // Badran
                    // url = "http://172.16.2.15/lyne/logar.php";

                    parametro = "email=" + email + "&senha=" + senha;

                    new SolicitaDados().execute(url);

                

             else 
                Toast.makeText(getApplicationContext(), "Nenhuma conexão com a Internet foi encontrada.", Toast.LENGTH_LONG).show();
            


        
    );
    

private class SolicitaDados extends AsyncTask<String, Void, String> 
    @Override
    protected String doInBackground(String... urls) 

            return Conexao.postDados(urls[0], parametro);

    

    @Override
    protected void onPostExecute(String resultado) 

        if(resultado != null) 
            if (resultado.contains("login_ok")) 
                Intent abreHome = new Intent(Login.this, Home.class);
                startActivity(abreHome);
             else 
                Toast.makeText(getApplicationContext(), "Usuário ou senha incorretos.", Toast.LENGTH_LONG).show();
            
        
    




@Override
protected void onPause() 
    super.onPause();
    finish();



【问题讨论】:

【参考方案1】:

您的堆栈跟踪会告诉您问题所在。尝试解析 url 时出现非法参数异常:

192.168.1.100/lyne/logar.php

【讨论】:

没问题。很高兴我能帮忙

以上是关于致命异常:AsyncTask #1 java.lang.RuntimeException:执行 doInBackground() 时发生错误的主要内容,如果未能解决你的问题,请参考以下文章

E/AndroidRuntime:致命异常:Android 的 AsyncTask #1 错误

获得致命异常:使用 Async 类的 AsyncTask #1

致命异常:AsyncTask #1,java.lang.RuntimeException:执行 doInBackground() 时发生错误

致命异常:AsyncTask #1 java.lang.RuntimeException:执行 doInBackground() 时发生错误

到 phpmyadmin 的致命异常 AsyncTask 连接

E / AndroidRuntime:致命异常:Android的AsyncTask#1 ERROR