java - Android登录无法连接DB

标签 java android mysql database

我正在使用this tutorial制作一个Android登录应用程序。我已遵循他的所有步骤,并成功能够在模拟器或我的手机上加载应用程序,但是当我尝试创建帐户时出现错误 - 我按照他提供的说明说明如何测试系统,引用此处:

  1. Testing the App

For a beginner it will be always difficult to run this project for the first time. But don’t worry, the following steps will helps you testing this app. (The ip address looks like 192.168.0.100)

Make sure that both devices (the device running the PHP project and the android device) are on the same wifi network.

Give correct username , password and database name of MySQL in Config.php

Replace the URL ip address of URL_LOGIN and URL_REGISTER in AppConfig.java with your machine ip address. You can get the ip address by running ipconfig in cmd

我的 IPv4 地址是 192.168.0.11, 所以我将其添加到 AppConfig 文件中,如下所示:

package android.loginfirstattempt.com.loginfirstattempt.app;


public class AppConfig {
    // Server user login url
    public static String URL_LOGIN = "http://192.168.0.11/android_login_api/login.php";


    // Server user register url
    public static String URL_REGISTER = "http://192.168.0.11/android_login_api/register.php";

}

我正在使用 Android Studio 的内置模拟器,因此两个设备都位于同一网络上。

我正在使用 Lampp,并且默认情况下我假设这是用户名和密码:

<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "android_api");
?>

我启动了本地服务器并通过加载演示页面进行测试以确保它们正在运行。

我还创建了 MySQL 数据库和表,如他的示例所示。

我的服务器端文件结构显示在this picture

但是当我尝试创建用户时,我遇到了这个崩溃:

                                                                                         beginning of crash

06-26 19:12:13.205 2668-2668/android.loginfirstattempt.com.loginfirstattempt E/AndroidRuntime: FATAL EXCEPTION: main Process: android.loginfirstattempt.com.loginfirstattempt, PID: 2668 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.loginfirstattempt.com.loginfirstattempt.app.AppController.addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference at android.loginfirstattempt.com.loginfirstattempt.activity.RegisterActivity.registerUser(RegisterActivity.java:189) at android.loginfirstattempt.com.loginfirstattempt.activity.RegisterActivity.access$300(RegisterActivity.java:35) at android.loginfirstattempt.com.loginfirstattempt.activity.RegisterActivity$1.onClick(RegisterActivity.java:84) 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)

我找不到任何明显的错误,想知道是否有人可以提供帮助!

最佳答案

您没有按照教程中的说明将 AppController 添加到 AndroidManifest.xml 中的应用程序标记中。现在,您的 AppController 在应用程序初始化期间不会被调用,也不会被初始化。当您在 registerUser 方法中调用 AppController.getInstance().addToRequestQueue(strReq, tag_string_req); 时,AppController.getInstance() 返回 null。这会导致您的 nullpointerException。将 AppController 添加到 list 中,您的代码应该可以正常工作。

您的应用程序标签应如下所示:

 <application
    android:name="com.loginfirstattempt.app.AppController"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

关于java - Android登录无法连接DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38041683/

相关文章:

java - 如何解决javax.net.ssl.SSLHandshakeException : Connection closed by peer

php - 将 jQuery 保存位置拖放到 mysql 数据库

python - 在同一列标题下拉出下一个值

OS X EL Capitan 上的 MySQL 5.7.13 Homebrew : ERROR! 服务器在启动时退出而不更新 PID 文件

java - Spring XML 模式

java - 线程 "main"java.lang.Error : Unresolved compilation problem: 中出现异常

java - DateFormat 已设置但仍然出现错误 - gson 库中无法解析日期

java - 在 Spring 中处理嵌套 REST 资源调用的更优雅的方式?

android - PhoneStateListener Android 错误值

android - 将项目从 Github 导入到 Android Studio 时显示错误