簡體   English   中英

使用php和mysql為Android構建登錄,注冊系統,但應用程序可以運行

[英]Build login,registration system with php and mysql for android,but app can run

我目前正在使用用於Android應用程序的登錄和注冊系統。我使用mysql作為數據庫,使用php進行此操作,但是在完成所有系統文件后,該應用程序僅在單擊登錄按鈕時停止。.pls幫助,以下是所有我寫的代碼

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/images" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="22dp"
        android:layout_marginLeft="19dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="2dp"
            android:text="Plot Out"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_alignTop="@+id/textView1"
            android:layout_marginTop="1dp"
            android:text="Bring Me Out"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="84dp"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="53dp"
            android:layout_marginTop="2dp"
            android:src="@drawable/logo" />

        <Button
            android:id="@+id/btnLogin"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginBottom="1dp"
            android:layout_marginLeft="40dp"
            android:text="Login"
            android:width="100sp" />

        <Button
            android:id="@+id/btnRegister"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="40dp"
            android:text="Register"
            android:width="100sp" />

        <TextView
            android:id="@+id/link_to_forgotpassword"
            android:clickable="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linearLayout2"
            android:layout_centerHorizontal="true"
            android:text="Forgot Your Password?"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

</RelativeLayout>

login.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:fillViewport="true">
  <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:background="#ffffff">

       <!--  Header Starts-->

<LinearLayout
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@layout/header_gradient"
    android:orientation="vertical"
    android:paddingBottom="5dip"
    android:paddingTop="5dip" >

        <!-- Logo Start-->
        <ImageView android:src="@drawable/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"/>
        <!-- Logo Ends -->
</LinearLayout>
<!--  Header Ends -->

       <!-- Footer Start -->
<LinearLayout android:orientation="horizontal"
    android:id="@+id/footer"
    android:layout_width="fill_parent"
    android:layout_height="90dip"
    android:background="@layout/footer_repeat"
    android:layout_alignParentBottom="true">
</LinearLayout>
<!-- Footer Ends -->

        <!-- Login Form -->
        <LinearLayout

          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="10dip"
          android:layout_below="@id/header">
          <!--  Email Label -->
          <TextView android:layout_width="fill_parent"
                android:id="@+id/loginEmail"
                android:layout_height="wrap_content"
                android:textColor="#372c24"
                android:text="Email"/>
          <EditText android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:layout_marginBottom="20dip"
                android:singleLine="true" />

          <!--  Password Label -->
          <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#372c24"
                android:text="Password"/>

          <EditText android:layout_width="fill_parent"
                android:id="@+id/loginPassword"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:singleLine="true"
                android:password="true"/>

           <!--  Error message -->
        <TextView android:id="@+id/login_error"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#e30000"
                    android:padding="10dip"
                    android:textStyle="bold"/>

          <!-- Login button -->
          <Button android:id="@+id/btnLogin"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:text="Login"/>
          <!-- Link to Registration Screen -->

          <TextView
              android:id="@+id/link_to_register"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_marginBottom="40dip"
              android:layout_marginTop="40dip"
              android:gravity="center"
              android:text="New to Plot Out?Register here"
              android:textColor="#0b84aa"
              android:textSize="20sp" />

</LinearLayout>
<!-- Login Form Ends -->

  </RelativeLayout>
</ScrollView>

register.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:fillViewport="true">
  <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:background="#fff">

        <!--  Header  Starts-->
        <LinearLayout android:id="@+id/header"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@layout/header_gradient"
                android:paddingTop="5dip"
                android:paddingBottom="5dip">
                <!-- Logo Start-->
                <ImageView android:src="@drawable/logo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dip"/>
                <!-- Logo Ends -->
        </LinearLayout>
        <!--  Header Ends -->
        <!-- Footer Start -->
        <LinearLayout android:id="@+id/footer"
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="90dip"
                android:background="@layout/footer_repeat"
                android:layout_alignParentBottom="true">
        </LinearLayout>
        <!-- Footer Ends -->

        <!-- Registration Form -->
        <!-- Registration Form Ends -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:orientation="vertical"
            android:padding="10dip" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Full Name"
                android:textColor="#372c24" />

            <EditText
                android:id="@+id/registerName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dip"
                android:layout_marginTop="5dip"
                android:singleLine="true" >

                <requestFocus />
            </EditText>

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Email"
                android:textColor="#372c24" />

            <EditText
                android:id="@+id/registerEmail"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dip"
                android:layout_marginTop="5dip"
                android:singleLine="true" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Password"
                android:textColor="#372c24" />

            <EditText
                android:id="@+id/registerPassword"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:password="true"
                android:singleLine="true" />

           <!--  Error message -->
        <TextView android:id="@+id/register_error"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#e30000"
                    android:padding="10dip"
                    android:textStyle="bold"/>

            <Button
                android:id="@+id/btnRegister"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:text="Register New Account" />

            <TextView
                android:id="@+id/link_to_login"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="40dip"
                android:layout_marginTop="40dip"
                android:gravity="center"
                android:text="Already has account! Login here"
                android:textColor="#ffffff"
                android:textSize="15sp" />
        </LinearLayout>

  </RelativeLayout>
</ScrollView>

LoginActivity.java

package com.plotout.loginandregister;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.plotout.loginandregister.library.DatabaseHandler;
import com.plotout.loginandregister.library.UserFunctions;

public class LoginActivity extends Activity {
    Button btnLogin;
    EditText inputEmail;
    EditText inputPassword;
    TextView loginErrorMsg;
    TextView link_to_register;

    // JSON Response node names
    private static String KEY_SUCCESS = "success";
    private static String KEY_ERROR = "error";
    private static String KEY_ERROR_MSG = "error_msg";
    private static String KEY_UID = "uid";
    private static String KEY_NAME = "name";
    private static String KEY_EMAIL = "email";
    private static String KEY_CREATED_AT = "created_at";

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

        // Importing all assets like buttons, text fields
        inputEmail = (EditText) findViewById(R.id.loginEmail);
        inputPassword = (EditText) findViewById(R.id.loginPassword);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        link_to_register = (TextView) findViewById (R.id.link_to_register);
        loginErrorMsg = (TextView) findViewById(R.id.login_error);

        // Login button Click Event
        btnLogin.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                String email = inputEmail.getText().toString();
                String password = inputPassword.getText().toString();
                UserFunctions userFunction = new UserFunctions();
                JSONObject json = userFunction.loginUser(email, password);

                // check for login response
                try {
                    if (json.getString(KEY_SUCCESS) != null) {
                        loginErrorMsg.setText("");
                        String res = json.getString(KEY_SUCCESS); 
                        if(Integer.parseInt(res) == 1){
                            // user successfully logged in
                            // Store user details in SQLite Database
                            DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                            JSONObject json_user = json.getJSONObject("user");

                            // Clear all previous data in database
                            userFunction.logoutUser(getApplicationContext());
                            db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                        

                            // Launch Dashboard Screen
                            Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);

                            // Close all views before launching Dashboard
                            dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(dashboard);

                            // Close Login Screen
                            finish();
                        }else{
                            // Error in login
                            loginErrorMsg.setText("Incorrect username/password");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        TextView registerScreen = (TextView) findViewById(R.id.link_to_login);

        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // Switching to Register screen
                Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(i);
            }
        });
    }
}

RegisterAvtivity.java

package com.plotout.loginandregister;

import org.json.JSONException;
import org.json.JSONObject;

import com.plotout.loginandregister.library.DatabaseHandler;
import com.plotout.loginandregister.library.UserFunctions;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RegisterActivity extends Activity {
    Button btnRegister;
    TextView link_to_login;
    EditText inputFullName;
    EditText inputEmail;
    EditText inputPassword;
    TextView registerErrorMsg;

    // JSON Response node names
    private static String KEY_SUCCESS = "success";
    private static String KEY_ERROR = "error";
    private static String KEY_ERROR_MSG = "error_msg";
    private static String KEY_UID = "uid";
    private static String KEY_NAME = "name";
    private static String KEY_EMAIL = "email";
    private static String KEY_CREATED_AT = "created_at";

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

        // Importing all assets like buttons, text fields
        inputFullName = (EditText) findViewById(R.id.registerName);
        inputEmail = (EditText) findViewById(R.id.registerEmail);
        inputPassword = (EditText) findViewById(R.id.registerPassword);
        btnRegister = (Button) findViewById(R.id.btnRegister);
        link_to_login= (TextView) findViewById(R.id.link_to_login);
        registerErrorMsg = (TextView) findViewById(R.id.register_error);

        // Register Button Click event
        btnRegister.setOnClickListener(new View.OnClickListener() {         
            public void onClick(View view) {
                String name = inputFullName.getText().toString();
                String email = inputEmail.getText().toString();
                String password = inputPassword.getText().toString();
                UserFunctions userFunction = new UserFunctions();
                JSONObject json = userFunction.registerUser(name, email, password);

                // check for login response
                try {
                    if (json.getString(KEY_SUCCESS) != null) {
                        registerErrorMsg.setText("");
                        String res = json.getString(KEY_SUCCESS); 
                        if(Integer.parseInt(res) == 1){
                            // user successfully registred
                            // Store user details in SQLite Database
                            DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                            JSONObject json_user = json.getJSONObject("user");

                            // Clear all previous data in database
                            userFunction.logoutUser(getApplicationContext());
                            db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                        
                            // Launch Dashboard Screen
                            Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
                            // Close all views before launching Dashboard
                            dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(dashboard);
                            // Close Registration Screen
                            finish();
                        }else{
                            // Error in registration
                            registerErrorMsg.setText("Error occured in registration");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        TextView loginScreen = (TextView) findViewById(R.id.link_to_login);

        // Listening to Login Screen link
        loginScreen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                                // Closing registration screen
                // Switching to Login Screen/closing register screen
                finish();
            }
        });
    }
}

下面是ecplise中顯示的logcat 忙碌的貓

我只是更改登錄xml中編輯文本的ID,解決了ClassCastException錯誤。

EditText android:layout_width="fill_parent"
                android:id="@+id/loginEmail"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:layout_marginBottom="20dip"
                android:singleLine="true" />

現在變成了一個新的錯誤,“ java.lang.NullPointerException” ..那又是什么原因呢?

這里的所有專家請看ya.pls幫助..謝謝

您的按鈕沒有任何作用,因為LoginActivity的以下兩行拋出ClassCastException因為您無法從Android.widget.TextView投射到Android.widget.EditView

inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);

main.xml將前兩個<TextView ... />更改為<EditText ... />

<TextView
    android:id="@+id/textView1"
    ...
    android:textAppearance="?android:attr/textAppearanceLarge" />

改成 :

<EditText
    android:id="@+id/textView2"
    ...
    android:textAppearance="?android:attr/textAppearanceLarge" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM