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.
我想做一个简单的登录和注册应用,这样用户就可以创建一个帐户。(姓名,用户名,密码)我使用WAMP和MYSQL数据库来存储账户。
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
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
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
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
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
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>";
}
?>
6 个解决方案
#1
55
Edit:
This was a bug in Android that was fixed in later versions of Marshmallow
这是Android中的一个bug,在后来版本的棉花糖中得到了修复
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.
我试过在两款Nexus 5s上运行同样的应用——一款运行棒棒糖,另一款是棉花糖,这些日志打印只出现在棉花糖版本上——每个应用都有,而不仅仅是我正在构建的应用。
Not sure why this happens, but I opened a report here.
不知道为什么会这样,但我在这里打开了一个报告。
#2
4
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
有关PersistentState的更多信息
Happy coding :)
编码快乐:)
#3
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());
使用appContext: new BackgroundTask(Register.this.getApplicationContext());
-
Or, Wait for BackgroundTask finish : remove finish() after .execute(...) and add finish() onPostExecute
或者,等待后台任务完成:在.execute(…)之后删除finish(),并在postexecute上添加finish()
#4
2
Update your android OS to 6.0.1.
将你的android操作系统升级到6.0.1。
This was an open issue found here. The issue is fixed in Android 6.0.1 which was publicly released recently.
这是一个公开的问题。这个问题在最近发布的Android 6.0.1中得到了解决。
#5
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" />
< 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
在我的Nexus 5上测试了Android 6.0,并在build.gradle中使用了targetSdkVersion 23。
#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.
由于android更改了权限请求(一些权限,如android.permission。READ_PHONE_STATE或android.permission.READ_CONTACTS),当您在运行时请求这些权限时,不要添加权限标记(
So just use tag permissions like old versions and add request permission at runtime in newer versions.
因此,只需像旧版本那样使用标记权限,并在新版本的运行时添加请求权限。
#1
55
Edit:
This was a bug in Android that was fixed in later versions of Marshmallow
这是Android中的一个bug,在后来版本的棉花糖中得到了修复
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.
我试过在两款Nexus 5s上运行同样的应用——一款运行棒棒糖,另一款是棉花糖,这些日志打印只出现在棉花糖版本上——每个应用都有,而不仅仅是我正在构建的应用。
Not sure why this happens, but I opened a report here.
不知道为什么会这样,但我在这里打开了一个报告。
#2
4
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
有关PersistentState的更多信息
Happy coding :)
编码快乐:)
#3
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());
使用appContext: new BackgroundTask(Register.this.getApplicationContext());
-
Or, Wait for BackgroundTask finish : remove finish() after .execute(...) and add finish() onPostExecute
或者,等待后台任务完成:在.execute(…)之后删除finish(),并在postexecute上添加finish()
#4
2
Update your android OS to 6.0.1.
将你的android操作系统升级到6.0.1。
This was an open issue found here. The issue is fixed in Android 6.0.1 which was publicly released recently.
这是一个公开的问题。这个问题在最近发布的Android 6.0.1中得到了解决。
#5
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" />
< 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
在我的Nexus 5上测试了Android 6.0,并在build.gradle中使用了targetSdkVersion 23。
#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.
由于android更改了权限请求(一些权限,如android.permission。READ_PHONE_STATE或android.permission.READ_CONTACTS),当您在运行时请求这些权限时,不要添加权限标记(
So just use tag permissions like old versions and add request permission at runtime in newer versions.
因此,只需像旧版本那样使用标记权限,并在新版本的运行时添加请求权限。