Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Studio-重定向到另一个页面_Java_Android_Xml_Database - Fatal编程技术网

Java Android Studio-重定向到另一个页面

Java Android Studio-重定向到另一个页面,java,android,xml,database,Java,Android,Xml,Database,我已经制作了一个android应用程序来登录数据库,我需要它在点击按钮时重定向到用户配置文件页面,到目前为止,它只显示一条消息说登录成功,然后不做任何其他事情。我下面的三个代码片段是应用程序的XML、同一页面的Java和BackgroundWorker,其中完成了许多底层工作。有人能告诉我如何重定向到名为Activity\u user\u profile.xml的页面吗 编辑答案: 我添加了一行:startActivity(newintent(this,UserProfileActivity.c

我已经制作了一个android应用程序来登录数据库,我需要它在点击按钮时重定向到用户配置文件页面,到目前为止,它只显示一条消息说登录成功,然后不做任何其他事情。我下面的三个代码片段是应用程序的XML、同一页面的Java和BackgroundWorker,其中完成了许多底层工作。有人能告诉我如何重定向到名为Activity\u user\u profile.xml的页面吗

编辑答案:

我添加了一行:startActivity(newintent(this,UserProfileActivity.class));对于我的onLogin方法,这允许我将它链接到我想要的页面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.programmingknowledge.finalyearproject2017.MainActivity">

<Button
    android:text="Register"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnRegister"
    android:onClick="OpenReg"
    android:layout_below="@+id/btnLogin"
    android:layout_alignLeft="@+id/btnLogin"
    android:layout_alignStart="@+id/btnLogin"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:id="@+id/et_doctor_number"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="Doctor number "
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:inputType="number" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:ems="10"
    android:id="@+id/et_Password"
    android:layout_below="@+id/et_doctor_number"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:hint="Password" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Login"
    android:id="@+id/btnLogin"
    android:onClick="OnLogin"
    android:layout_below="@+id/et_Password"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

</RelativeLayout>
}

后台工作人员:
包com.example.programmingknowledge.finalyearproject2017;
导入android.content.Context;
导入android.os.AsyncTask;
导入android.app.AlertDialog;
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.OutputStream;
导入java.io.OutputStreamWriter;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.net.urlcoder;
/**
*约翰于2017年2月25日创作。
*/
公共类BackgroundWorker扩展异步任务{
语境;
警报对话框警报对话框;
BackgroundWorker(上下文ctx){
上下文=ctx;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串类型=参数[0];
字符串登录\u url=”http://10.0.2.2/login.php";
字符串寄存器\u url=”http://10.0.2.2/register.php";
if(type.equals(“登录”)){
试一试{
字符串email=params[1];
字符串密码=参数[2];
URL=新URL(登录\ URL);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“POST”);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OutputStream=httpURLConnection.getOutputStream();
BufferedWriter BufferedWriter=新的BufferedWriter(新的OutputStreamWriter(outputStream,utf-8));
字符串post_data=urlcoder.encode(“电子邮件”,“UTF-8”)+“=”+urlcoder.encode(电子邮件,“UTF-8”)+“&+urlcoder.encode(“密码”,“UTF-8”)+“=”+urlcoder.encode(密码,“UTF-8”);
bufferedWriter.write(post_数据);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream,“iso-8859-1”);
字符串结果=”;
字符串行=”;
而((line=bufferedReader.readLine())!=null){
结果+=行;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
返回结果;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}else if(type.equals(“寄存器”)){
试一试{
字符串patient_name=params[1];
字符串检查日期=参数[2];
字符串房间号=参数[3];
串床编号=参数[4];
字符串注释=参数[5];
URL=新URL(注册URL);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“POST”);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OutputStream=httpURLConnection.getOutputStream();
BufferedWriter BufferedWriter=新的BufferedWriter(新的OutputStreamWriter(outputStream,utf-8));
字符串post_data=URLEncoder.encode(“患者姓名”,“UTF-8”)+”=“+URLEncoder.encode(患者姓名,“UTF-8”)+”&”
+URLEncoder.encode(“签入日期”,“UTF-8”)+”=“+URLEncoder.encode(签入日期,“UTF-8”)+”&”
+URLEncoder.encode(“房间号”,“UTF-8”)+”=“+URLEncoder.encode(房间号,“UTF-8”)+”&”
+URLEncoder.encode(“床号”、“UTF-8”)+”=“+URLEncoder.encode(床号”、“UTF-8”)+”&”
+URLEncoder.encode(“注释”,“UTF-8”)+“=”+URLEncoder.encode(注释,“UTF-8”);
bufferedWriter.write(post_数据);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream,“iso-8859-1”);
字符串结果=”;
字符串行=”;
而((line=bufferedReader.readLine())!=null){
结果+=行;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
返回结果;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回null;
}
@凌驾
受保护的void onPreExecute(){
alertDialog=新建alertDialog.Builder(context.create();
alertDialog.setMessage(“登录状态”);
}
@凌驾
受保护的void onPostExecute(字符串结果){
alertDialog.setMessage(结果);
alertDialog.show();
}
@凌驾
受保护的void onProgressUpdate(void…值){
super.onProgressUpdate(值);
}

}

在您执行PostExecute时,调用Intent以启动您的用户配置文件活动
package com.example.programmingknowledge.finalyearproject2017;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

EditText EmailEt, PasswordEt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EmailEt = (EditText) findViewById(R.id.et_doctor_number);
    PasswordEt = (EditText) findViewById(R.id.et_Password);
}

public void OnLogin(View view){
    String email = EmailEt.getText().toString();
    String password = PasswordEt.getText().toString();
    String type = "Login";

    BackgroundWorker backgroundWorker = new BackgroundWorker(this);
    backgroundWorker.execute(type, email, password);
    startActivity(new Intent(this,UserProfileActivity.class));
}

public void OpenReg(View view){
    startActivity(new Intent(this,Register.class));
}
Background Worker:
package com.example.programmingknowledge.finalyearproject2017;

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

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;

/**
* Created by john on 25-Feb-17.
*/

public class BackgroundWorker extends AsyncTask<String, Void, String> {
    Context context;
    AlertDialog alertDialog;
    BackgroundWorker (Context ctx){
        context = ctx;
    }
    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://10.0.2.2/login.php";
        String register_url = "http://10.0.2.2/register.php";
        if (type.equals("login")){
            try {
                String email = params[1];
                String password = params[2];
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
                String post_data = URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(email, "utf-8")+"&"+URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password, "utf-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result ="";
                String line ="";
                while ((line=bufferedReader.readLine())!=null){
                    result+=line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e){
                e.printStackTrace();
            }
        } else if(type.equals("register")){
            try {
                String patient_name = params[1];
                String check_in_date = params[2];
                String room_number = params[3];
                String bed_number = params[4];
                String notes = params[5];
                URL url = new URL(register_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
                String post_data = URLEncoder.encode("patient_name", "UTF-8")+"="+URLEncoder.encode(patient_name, "utf-8")+"&"
                    + URLEncoder.encode("check_in_date", "UTF-8")+"="+URLEncoder.encode(check_in_date, "utf-8")+"&"
                    + URLEncoder.encode("room_number", "UTF-8")+"="+URLEncoder.encode(room_number, "utf-8")+"&"
                    + URLEncoder.encode("bed_number", "UTF-8")+"="+URLEncoder.encode(bed_number, "utf-8")+"&"
                    + URLEncoder.encode("notes", "UTF-8")+"="+URLEncoder.encode(notes, "utf-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
            String result ="";
            String line ="";
            while ((line=bufferedReader.readLine())!=null){
                result+=line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    return null;
}

@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setMessage("Login Status");
}

@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    alertDialog.show();
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    alertDialog.show();

activity.startActivity(new Intent(activity,UserProfile.class));
}