【问题标题】:I get an error like this, and application closes我收到这样的错误,应用程序关闭
【发布时间】:2017-01-02 10:05:19
【问题描述】:

我收到这样的错误。应用程序关闭。

这是我的代码:

BackgroundTask Class,

public class BackgroundTask extends AsyncTask<String,Void,String> {
AlertDialog alertDialog;
Context ctx;
BackgroundTask(Context ctx)
{
    this.ctx = ctx;
}
@Override
protected void onPreExecute(){
    alertDialog = new AlertDialog.Builder(ctx).create();
    alertDialog.setTitle("Login Information....");
}
@Override
protected String doInBackground(String... params) {
    String reg_url = "http://127.0.0.1/Register.php";
    String login_url = "http://127.0.0.1/Login.php";
    String method = params[0];
    if (method.equals("register"))
    {
        String isim = params [1];
        String soyisim = params[2];
        String plaka = params[3];
        String saseno = params[4];
        String email = params[5];
        String sifre = params[6];
        try {
            URL url = new URL(reg_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            //httpURLConnection.setDoInput(true);
            OutputStream OS = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
            String data = URLEncoder.encode("isim","UTF-8") +"="+URLEncoder.encode(isim,"UTF-8")+"&"+
            URLEncoder.encode("soyisim","UTF-8") +"="+URLEncoder.encode(soyisim,"UTF-8")+"&"+
            URLEncoder.encode("plaka","UTF-8") +"="+URLEncoder.encode(plaka,"UTF-8")+"&"+
            URLEncoder.encode("saseno","UTF-8") +"="+URLEncoder.encode(saseno,"UTF-8")+"&"+
            URLEncoder.encode("email","UTF-8") +"="+URLEncoder.encode(email,"UTF-8")+"&"+
            URLEncoder.encode("sifre","UTF-8") +"="+URLEncoder.encode(sifre,"UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            OS.close();
            InputStream IS = httpURLConnection.getInputStream();
            IS.close();
            //httpURLConnection.connect();
            httpURLConnection.disconnect();
            return  "Registration Success...";
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else if (method.equals("login"))
    {
        String loginmail = params[1];
        String loginsifre = params[2];
        try {
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)
            url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
        String data = URLEncoder.encode("loginmail","UTF-8")+"="+URLEncoder.encode(loginmail,"UTF-8")+"&"+
                URLEncoder.encode("loginsifre","UTF-8")+"="+URLEncoder.encode(loginsifre,"UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            String response = "";
            String line = "";
            while ((line = bufferedReader.readLine())!=null)
            {
                response+= line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return response;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}
@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
    if (result.equals("Registration Success..."))
    {
        Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
    }
    else
    {
        alertDialog.setMessage(result);
        alertDialog.show();
    }
}
}


Login Javaclass,

package com.example.serdar.tipoff;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;

public class Login extends Activity {
EditText ET_EMAİL_LOGİN,ET_SİFRE_LOGİN;
String loginmail,loginsifre;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_login);

    ET_EMAİL_LOGİN = (EditText)findViewById(R.id.etmaillogin);
    ET_SİFRE_LOGİN = (EditText)findViewById(R.id.etsifrelogin);
}
public void Regkayit (View view)
{
    startActivity(new Intent(this, Register.class));
}
public void LogGiris ( View view)
{
    loginmail = ET_EMAİL_LOGİN.getText().toString();
    loginsifre = ET_SİFRE_LOGİN.getText().toString();
    String method = "login";
    BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute(method,loginmail,loginsifre);
}
}


Register Javaclass,

package com.example.serdar.tipoff;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;

public class Register extends Activity {
EditText    ET_İSİM_REG,ET_SOYİSİM_REG,ET_PLAKA_REG,ET_SASENOREG,ET_EMAİL_REG,ET_SİFRE_REG;
String isim,soyisim,plaka,saseno,email,sifre;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_register);

    ET_İSİM_REG = (EditText)findViewById(R.id.etisimreg);
    ET_SOYİSİM_REG = (EditText)findViewById(R.id.etsoyisimreg);
    ET_PLAKA_REG = (EditText)findViewById(R.id.etplakareg);
    ET_SASENOREG = (EditText)findViewById(R.id.etsasenoreg);
    ET_EMAİL_REG = (EditText)findViewById(R.id.etmailreg);
    ET_SİFRE_REG = (EditText)findViewById(R.id.etsifrereg);
}
public void Regkaydet (View view)
{
    isim = ET_İSİM_REG.getText().toString();
    soyisim = ET_SOYİSİM_REG.getText().toString();
    plaka = ET_PLAKA_REG.getText().toString();
    saseno = ET_SASENOREG.getText().toString();
    email = ET_EMAİL_REG.getText().toString();
    sifre = ET_SİFRE_REG.getText().toString();
    String method = "register";
    BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute(method,isim,soyisim,plaka,saseno,email,sifre);
    finish();
}
}

【问题讨论】:

标签: android nullpointerexception


【解决方案1】:

我认为在您的代码中,您正在使用像 String1.equals(String2) 这样的等号函数比较两个字符串,在您的情况下,String1 为空(未初始化),这是此异常的根本原因。

例如:

String string1 = getInputText();
boolean status = false;
if(string1 != null) {
    status = string1.equals(string2);
}

所以请在调用String1.equals(String2)之前检查并确保String1不为空。

【讨论】:

  • String string1 = getInputText();布尔状态 = 假; if(string1 != null) { status = string1.equals(string2) }
  • 专业提示:添加内联代码示例时,请使用反引号而不是粗体。查看我的编辑。
  • 好的。谢谢你..我还没有解决我的问题。请在我的代码中更正此错误
  • protected void onPostExecute(String result) { if (result.equals("Registration Success...")) 问题在于此代码块中的字符串结果为空。请检查。
  • 谢谢你,Ranjith KP.. protected void onPostExecute(String result) { if (result.equals("Registration Success...")) 我检查了,但我找不到错误..我没有按照大家说的去做..我不知道该怎么做..
【解决方案2】:

如前所述,您正在使用 Equal Method 比较两个字符串。例如string1.equals(string2)

但在你的情况下 string1 是空的。请参阅下面的示例以正确理解它。

应该怎么做

String string1 = "hello";
String string2 = "hello";
string1.equals(string2) will return true

你在做什么

 String string1;
 String string2 = "hello";

string1.equals(string2) 将使应用程序崩溃,因为 string1 为空。 Equals 方法不适用于 null,因为 NUll 无法与其他进行比较。

【讨论】:

  • 次要细节:很可能将 null 与字符串进行比较。无法调用string1.equals() 的原因是空变量无法指向可行的实例或方法,因此运行时环境不知道如何处理。
  • 我无法理解答案发送我的代码..pleasee
猜你喜欢
  • 2020-04-21
  • 2011-11-29
  • 2019-06-25
  • 1970-01-01
  • 2019-04-13
  • 2012-05-17
  • 2014-07-22
  • 1970-01-01
  • 2021-04-14
相关资源
最近更新 更多