Teddy Fernandez Teddy Fernandez - 3 months ago 26
Java Question

Android Login Activity crashing soon as a press login

first of I must state that I am new to the world of android programming and I searched for the answer for my problem on all ends of stack exchange.. but to know avail.. so if the answer is already present please guide me to that :)

I have created a small app just with the login screen according to a tutorial.. but as soon as I click the login button it crashes.. and my logcat doesnt give a specific answer as to the cause.

Here is my main activity

public class MainActivity extends AppCompatActivity {
EditText emailET,passwordET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final EditText emailET = (EditText) findViewById(R.id.email);
final EditText passwordET = (EditText) findViewById(R.id.password);

}

public void onLogin(View view)
{
String email = emailET.getText().toString();
String password = passwordET.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type,email,password);


}
}


here is my asynctask

import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;

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;


public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx){
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params [0];
String Login_url = "localhost/login.php";
if (type.equals("login"))
{
try {
String user_name = params[1];
String password = params[2];
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 post_data =URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"+ URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
return null;
}

@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}

@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}

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


and here is my layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="testlib.login.MainActivity">

<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/email"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:hint="Email"
android:textColorHint="#FFFFFF"
android:textAlignment="center" />

<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/password"
android:layout_below="@+id/email"
android:layout_alignStart="@+id/email"
android:layout_marginTop="44dp"
android:textColorHint="#ffffff"
android:textIsSelectable="false"
android:hint="Password"
android:textAlignment="center" />



<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/Login_button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="#ffffff"
android:onClick="onLogin"/>

<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Register"
android:id="@+id/Register_button"
android:layout_below="@+id/Login_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:background="#ffffff" />
</RelativeLayout>


and finally here is my logcat

08-26 23:29:26.629 2206-2206/testlib.login E/libprocessgroup: failed to make and chown /acct/uid_10058: Read-only file system
08-26 23:29:26.629 2206-2206/testlib.login W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
08-26 23:29:26.632 2206-2206/testlib.login I/art: Not late-enabling -Xcheck:jni (already on)
08-26 23:29:26.847 2206-2219/testlib.login I/art: Background sticky concurrent mark sweep GC freed 6926(360KB) AllocSpace objects, 6(122KB) LOS objects, 42% free, 643KB/1117KB, paused 7.036ms total 19.067ms
08-26 23:29:33.916 2206-2206/testlib.login W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-26 23:29:33.978 2206-2719/testlib.login D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

[ 08-26 23:29:33.981 2206: 2206 D/ ]
HostConnection::get() New Host Connection established 0xb3ee8ca0, tid 2206
08-26 23:29:33.983 2206-2206/testlib.login D/Atlas: Validating map...
08-26 23:29:34.048 2206-2719/testlib.login I/OpenGLRenderer: Initialized EGL, version 1.4
08-26 23:29:34.074 2206-2719/testlib.login D/OpenGLRenderer: Enabling debug mode 0
08-26 23:29:34.126 2206-2719/testlib.login W/EGL_emulation: eglSurfaceAttrib not implemented
08-26 23:29:34.127 2206-2719/testlib.login W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xae139ba0, error=EGL_SUCCESS
08-26 23:29:52.779 2206-2206/testlib.login D/AndroidRuntime: Shutting down VM


--------- beginning of crash
08-26 23:29:52.781 2206-2206/testlib.login E/AndroidRuntime: FATAL EXCEPTION: main
Process: testlib.login, PID: 2206
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:289)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at testlib.login.MainActivity.onLogin(MainActivity.java:22)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
08-26 23:29:58.050 2206-2206/testlib.login I/Process: Sending signal. PID: 2206 SIG: 9

Answer

Well I don't think your code will compile at all, because of Object EditText declare both inside and outside the method with same name emailET,passwordET to avoid NPE check your editext properly contain the String value

if(editext.getText().toString().length<0){//dosomething}

Here is code :

public class MainActivity extends AppCompatActivity {
EditText emailET,passwordET;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onLogin(View view)
{
    emailET = (EditText) findViewById(R.id.email);
    passwordET = (EditText) findViewById(R.id.password);
    if(emailET.getText().toString()<0){//doSomething}
    String email = emailET.getText().toString();
    String password = passwordET.getText().toString();
    String type = "login";

    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(type,email,password);
}
}
Comments