1

I'm new to Android studio. I follow tutorials on Youtube and Google errors, but this time I cant seem to find how to fix this problem.

I'm creating a simple login-in and register app, that uses a mysql database. When I launch the app and click on the register button the app closes and states: "App stopped working".

I checked out the logcat and found this:

09-13 17:23:19.607    2229-2236/com.example.appname.appname E/art﹕ Failed sending reply to debugger: Broken pipe
09-13 17:23:47.756    2229-2229/com.example.appname.appname E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.appname.appname, PID: 2229
    java.lang.IllegalStateException: Could not find method userReg(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button
            at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4479)
            at android.view.View$DeclaredOnClickListener.onClick(View.java:4443)
            at android.view.View.performClick(View.java:5198)
            at android.view.View$PerformClick.run(View.java:21147)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Mainactivity.java

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
public void userReg()
{
    startActivity(new Intent(this, Register.class));
}
    public void userLogin(View view)
    {


    }


}

Register.java

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class Register extends Activity {
    EditText ET_NAME,ET_USER_NAME,ET_USER_PASS;
    String name,user_name,user_pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);
        ET_NAME = (EditText) findViewById(R.id.name);
        ET_USER_NAME = (EditText) findViewById(R.id.new_user_name);
        ET_USER_PASS = (EditText) findViewById(R.id.new_user_pass);
    }

    public void userReg(View view)
    {
        name = ET_NAME.getText().toString();
        user_name = ET_USER_NAME.getText().toString();
        user_pass = ET_USER_PASS.getText().toString();
        String method = "register";
        BackgroundTask backgroundTask = new BackgroundTask(this);
        backgroundTask.execute(method,name,user_name,user_pass);
        finish();
    }
}

Backgroundtask.java

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
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 BackgroundTask extends AsyncTask<String, Void, String> {
    Context ctx;
    BackgroundTask(Context ctx)
    {
        this.ctx=ctx;

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

    @Override
    protected String doInBackground(String... params) {
        String reg_url = "http://10.0.2.2.2/webapp/register.php";
        String login_url = "http://10.0.2.2.2/webapp/login.php";
        String method = params[0];
        if(method.equals("register"))
        {
        String name = params [1];
            String user_name = params[2];
            String user_pass = params[3];
            try {
                URL url = new URL(reg_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter (new OutputStreamWriter(OS, "UTF-8"));
            String data = URLEncoder.encode("user", "UTF-8") +"=" +URLEncoder.encode(name, "UTF-8") + "&"+
                    URLEncoder.encode("user_name", "UTF-8") +"=" +URLEncoder.encode(user_name, "UTF-8") + "&"+
                    URLEncoder.encode("user_pass", "UTF-8") +"=" +URLEncoder.encode(user_pass, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                return "Registration Success..";
            } 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) {
        Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
    }
}
Rob12
  • 423
  • 1
  • 4
  • 8

2 Answers2

0

Change in your Mainactivity.java

public void userReg()
{
    startActivity(new Intent(this, Register.class));
}

to

public void userReg(View v)
{
    startActivity(new Intent(Mainactivity.this, Register.class));
}

And in Register.java

public void userReg(View view)
{
    name = ET_NAME.getText().toString();
    user_name = ET_USER_NAME.getText().toString();
    user_pass = ET_USER_PASS.getText().toString();
    String method = "register";
    BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute(method,name,user_name,user_pass);
    finish();
}

to

public void userReg(View view)
{
    name = ET_NAME.getText().toString();
    user_name = ET_USER_NAME.getText().toString();
    user_pass = ET_USER_PASS.getText().toString();
    String method = "register";
    BackgroundTask backgroundTask = new BackgroundTask(Register.this);
    backgroundTask.execute(method,name,user_name,user_pass);
    finish();
}
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
0

The error appeared because the app couldn't find userReg(View) method that was written in the onClick attribute of your button in xml. You need to add parameter "View" in your method. Simply fix these lines of code:

MainActivity.java

public void userReg() {
    startActivity(new Intent(this, Register.class));
}

to

public void userReg(View v) {
    startActivity(new Intent(MainActivity.this, Register.class));
}
Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57