41

I want to make a simple login and register app, so the user can create an account. (name, username, password) I use WAMP and a MYSQL database where I store the accounts.

When I fill in the user info on the registration form and click register I get the following error:

09-14 09:30:39.864    2624-2638/com.example.appname.appname E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab7115e0
09-14 09:30:48.632    2624-2638/com.example.appname.appname E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab7125a0
09-14 09:30:51.940    2624-2638/com.example.appname.appname E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab7125a0

When I go check the database it didn't store the account.

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(View v)
    {
        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(Register.this);
        backgroundTask.execute(method,name,user_name,user_pass);
        finish();
    }
}

Backgroundtask.java

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

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 BackgroundTask extends AsyncTask<String, Void, String> {
    AlertDialog alertDialog;
    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();
    }
}

register.php

<?php
require "init.php";
$name = $_POST["user"];
$user_name = $_POST["user_name"];
$user_pass = $_POST["user_pass"];

$sql_query = "insert into user_info values('$name','$user_name','$user_pass');";

if(mysqli_query($con,$sql_query))
{
//echo"<h3> Data insertion success...</h3>";
}
else{
//echo "Data insertion error..".mysqli_error($con);
}



?>

init.php

<?php
$db_name="myDBname";
$mysql_user = "root";
$mysql_pass = "root";
$server_name="localhost";

$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
if(!$con)
{
//echo"Connection Error".mysqli_connect_error();
}
else
{
//echo"<h3> Database connection success.....</h3>";
}


?>
Rob12
  • 423
  • 1
  • 4
  • 8
  • thanks, I checked my php files and I don't think the problem lies there. – Rob12 Sep 14 '15 at 10:04
  • Okay thank you very much, appreciate it. I just added the PHP scripts, just in case. – Rob12 Sep 14 '15 at 10:18
  • I'm running through "Android Programming 2ed" and I repeatedly get errors like _E/Surface: getSlotFromBufferLocked: unknown buffer: 0xabe2d840_ . what would cause that? – Matthew Cornell Oct 14 '15 at 14:47
  • m having same prob , but my registration data will able to store in db but when i try to login, it cause this error and i can not login . anyone have solution?? – RKosamia Nov 22 '15 at 22:56

6 Answers6

58

Edit:

This was a bug in Android that was fixed in later versions of Marshmallow

Original:

I believe that this is Marshmallow specific. Are you using a Marshmallow device?

I've noticed this error is printed out every time I switch between applications (doesn't matter which) or exit out of them, and when activities are destroyed.

I've tried running the same apps on two Nexus 5s - one running Lollipop and the other Marshmallow, and these log prints only appeared on the Marshmallow version - with every app, not just the one I'm building.

Not sure why this happens, but I opened a report here.

Itai Hanski
  • 8,540
  • 5
  • 45
  • 65
  • 1
    Yep. Confirmed as @Itai Hanski said. It's marshmallow specific. Switch your API level to a different one. – kirobo Nov 08 '15 at 14:16
  • @ShylendraMadda: They fixed this problem in in Android 6.0.1: https://code.google.com/p/android/issues/detail?id=192357#c18 – mhellmeier Mar 04 '16 at 16:01
  • This is not specific for marshmallow, I have 5.1.1 (lollipop) and also I get this error. I think this is related to reading and writing data on the SQLite database. Indeed, the buffer is locked and you have not any permission for accessibility. Try https://github.com/DezQ/ContactManager , which is a complete sample integrated with SQLite. – Hosein Aqajani Jun 19 '16 at 10:00
3

I think you are finishing the context before backgrounTask finish, and Context you pass no longer exist.
You can try:

  • Use appContext : new BackgroundTask(Register.this.getApplicationContext());

  • Or, Wait for BackgroundTask finish : remove finish() after .execute(...) and add finish() onPostExecute

Mario
  • 175
  • 1
  • 7
3

I don't have specific problem's solution. But I had similar problem. Anyone who has problem like this please make sure you have below code.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.<YOUR_XML_FILE_NAME>);

by using intelligence you might have choose below code:

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.<YOUR_XML_FILE_NAME>);
}

This worked for me

For more info on PersistentState

Happy coding :)

Vamsi Abbineni
  • 479
  • 3
  • 7
  • 23
Gaurav Karia
  • 166
  • 8
2

Update your android OS to 6.0.1.

This was an open issue found here. The issue is fixed in Android 6.0.1 which was publicly released recently.

1

It's quite strange but in my case i got this error when tried loading contacts without adding

<uses-permission android:name="android.permission.READ_CONTACTS" />

into manifest. And it disappeared just I've done it.

So my guess it's might be something with new permissions in Marshmallow.

Tested on my Nexus 5 with Android 6.0 and with targetSdkVersion 23 in build.gradle

polis
  • 737
  • 7
  • 6
1

Since that android changed permissions request (some permissions like android.permission.READ_PHONE_STATE or android.permission.READ_CONTACTS), when you ask for these permissions at runtime and dont add the permission tag (<uses-permission android:name="..." />) in manifest, you will get the error.

So just use tag permissions like old versions and add request permission at runtime in newer versions.