注册活动是开始活动。单击文本视图,它应更改为登录活动,但应用程序在执行时崩溃。 ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... .........................................
注册活动
package com.example.saiteja.traffic_sway;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private TextView loginTV;
private EditText email;
private EditText password;
private Button regButton;
private FirebaseAuth mAuth;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null) {
//that means user is already logged in
//so close this activity
finish();
//and open profile activity
startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
}
email = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
loginTV = (TextView) findViewById(R.id.loginTV);
regButton = (Button) findViewById(R.id.button);
progressDialog = new ProgressDialog(this);
loginTV = (TextView) findViewById(R.id.loginTV);
regButton.setOnClickListener(this);
loginTV.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view == regButton){
registerUser();
}
if(view == loginTV){
//open login activity when user taps on the already registered textview
Intent i = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(i);
}
}
private void registerUser() {
//getting email and password from edit texts
String email1 = email.getText().toString().trim();
String password1 = password.getText().toString().trim();
//checking if email and passwords are empty
if (TextUtils.isEmpty(email1)) {
Toast.makeText(this, "Please enter email", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(password1)) {
Toast.makeText(this, "Please enter password", Toast.LENGTH_LONG).show();
return;
}
//if the email and password are not empty
//displaying a progress dialog
progressDialog.setMessage("Registering Please Wait...");
progressDialog.show();
//creating a new user
mAuth.createUserWithEmailAndPassword(email1, password1)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//checking if success
if (task.isSuccessful()) {
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
} else {
//display some message here
Toast.makeText(RegisterActivity.this, "Registration Error", Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
});
}
}
登录活动
package com.example.saiteja.traffic_sway;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
private TextView registerTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
registerTV = (TextView)findViewById(R.id.loginTV);
registerTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
}
});
}
}
Android Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".RegisterActivity"
android:screenOrientation="sensorPortrait"
android:label="Traffic Sway">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
<activity android:name=".Main2Activity" />
<activity android:name=".LoginActivity"/>
</application>
单击“已有用户?登录”文本View.pls帮助时,应用程序崩溃
答案 0 :(得分:0)
在启动finish()
之前,请勿调用Activity
函数。
祝您申请顺利。
答案 1 :(得分:0)
问题似乎是您的LoginActivity中的findViewById
registerTV = (TextView)findViewById(R.id.loginTV);
检查 loginTV 是否在您的 activity_login.xml 中
如果不是,那就是问题所在,一旦ID不可用, registerTV 将为空,并且您的registerTv.setOnclickListener()
将无法正常工作。