我知道这似乎是一个很愚蠢或容易解决的问题,但是老实说,我已经做了很多尝试来解决这个问题。我的应用程序应该在完成个人资料活动后显示欢迎活动,例如添加您的名字和姓氏等。
下面是用于在填写必填字段(例如名字,姓氏,年龄和性别)的用户个人资料时使用的代码
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Grab firebaseAuth objects
firebaseAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("Users");
firebaseUser = firebaseAuth.getInstance().getCurrentUser();
//Set app to full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_profile);
//Get all views
firstName = findViewById(R.id.profile_firstName);
lastName = findViewById(R.id.profile_lastName);
male = findViewById(R.id.profile_male);
female = findViewById(R.id.profile_female);
age = findViewById(R.id.profile_age);
button = findViewById(R.id.profile_button);
button.setOnClickListener(this);
//If no user is logged in send them back to Login page
if (firebaseUser == null)
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
private void createUser()
{
String first_name = firstName.getText().toString().trim();
String last_name = lastName.getText().toString().trim();
String gender = "";
if (male.isChecked())
{
gender = "male";
}
if (female.isChecked())
{
gender = "female";
}
if (male.isChecked() && female.isChecked())
{
Toast.makeText(this, "Please only select one gender", Toast.LENGTH_SHORT).show();
return;
}
if (male.isChecked() == false && female.isChecked() == false)
{
Toast.makeText(this, "Please select a gender", Toast.LENGTH_SHORT).show();
return;
}
int years;
if (age.getText().toString().length() == 0)
{
years = 0;
}
else
{
years = Integer.parseInt(age.getText().toString());
}
boolean profileComplete;
//Check if profileComplete should be true or false
if (first_name.length() == 0 || last_name.length() == 0 || gender.length() == 0 || years == 0 || gender.length() == 0)
{
profileComplete = false;
}
else
{
profileComplete = true;
}
User user = new User(first_name, last_name, gender, years, FirebaseAuth.getInstance().getCurrentUser().getEmail(), profileComplete);
//Create a new node for each individual user
firebaseDatabase.getReference().child("Users/" + FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(user);
//Bring them into the welcome screen
Intent intent = new Intent(this, WelcomeActivity.class);
startActivity(intent);
finish();
}
@Override
public void onClick(View view)
{
if (view == button)
{
createUser();
}
}
在用户对象完全完成并发送到数据库之后,我们可以清楚地看到该活动启动了一个名为WelcomeActivity的新活动。它从一开始就有一个Log.i,它从不触发,而是运行ViewProfileActivity。
这是WelcomeActivity:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.i("WelcomeActivity", "We made it to the WelcomeActivity!");
//Set app to full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_welcome);
//Grab firebaseAuth objects
String currentUserId = firebaseAuth.getInstance().getCurrentUser().getUid();
firebaseAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("Users/" + currentUserId);
welcomeUser = findViewById(R.id.id_welcome_user);
final User user = new User();
databaseReference.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
user.setFirstName(dataSnapshot.getValue(User.class).getFirstName());
welcomeUser.setText(user.getFirstName().toString());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
}
这是ViewProfileActivity(它应该在欢迎活动之后运行。现在,welcomeActivity没有意图运行ViewProfileActivity,因为到目前为止我什至无法将它放到那里。):
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Set app to full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_view_profile);
//Grab firebaseAuth objects
String currentUserId = firebaseAuth.getInstance().getCurrentUser().getUid();
firebaseAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("Users/" + currentUserId);
final TextView view_profile_firstName = findViewById(R.id.view_profile_firstname);
final TextView view_profile_lastName = findViewById(R.id.view_profile_lastname);
final TextView view_profile_email = findViewById(R.id.view_profile_email);
final TextView view_profile_age = findViewById(R.id.view_profile_age);
final TextView view_profile_gender = findViewById(R.id.view_profile_gender);
final TextView view_profile_profileComplete = findViewById(R.id.view_profile_profilecomplete);
final User user = new User();
databaseReference.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
user.setFirstName(dataSnapshot.getValue(User.class).getFirstName());
view_profile_firstName.setText(user.getFirstName().toString());
user.setLastName(dataSnapshot.getValue(User.class).getLastName());
view_profile_lastName.setText(user.getLastName().toString());
user.setEmail(dataSnapshot.getValue(User.class).getEmail());
view_profile_email.setText(user.getEmail().toString());
user.setAge(dataSnapshot.getValue(User.class).getAge());
view_profile_age.setText(Integer.toString(user.getAge()));
user.setGender(dataSnapshot.getValue(User.class).getGender());
view_profile_gender.setText(user.getGender());
user.setProfileComplete(dataSnapshot.getValue(User.class).getProfileComplete());
view_profile_profileComplete.setText(String.valueOf(user.getProfileComplete()));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
}
起初
setContentView(R.layout.activity_welcome);
是错误的,并保留了layout.activity_view_profile。我认为解决此问题很容易解决我的问题,但似乎没有什么改变。我已经重建并清理了我的项目,完全卸载了apk并将其重新安装在模拟器和物理设备上,似乎什么也没有用。我想这是一个编码错误,但我似乎无法查明。
我的假设是其他活动正在运行,并且它们的onDataChange方法正在不断检查并执行对ViewProfileActivity的意图,但是我在每个startActivity()的末尾放置了finish()。
非常感谢所有帮助,因为这限制了我使用此应用的进度。到目前为止,我将继续研究直到找到解决方案为止。
清单:
<?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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Registration" />
<activity android:name=".ProfileActivity" />
<activity android:name=".ViewProfileActivity" />
<activity android:name=".WelcomeActivity"></activity>
</application>
更新: 在注释掉if语句中的代码之后,我终于能够使用WelcomeActivity进行显示。
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
user.setProfileComplete(dataSnapshot.getValue(User.class).getProfileComplete());
//The profileComplete method returns TRUE, so we can skip the profile creation.
if(user.getProfileComplete())
{
//Intent intent = new Intent(MainActivity.this, ViewProfileActivity.class);
//startActivity(intent);
//finish();
}
//the profileComplete method returns FALSE, we must send the user to the profile creation.
else
{
Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
startActivity(intent);
finish();
}
}
此代码位于MainActivity中,用于确定用户配置文件是否完整。如果未完成,则会发送给他们创建个人资料,否则立即发送给他们以查看个人资料。
答案 0 :(得分:0)
我认为问题在于您尚未创建要首先加载的活动。
解决方案1: 尝试点击该应用程序
并选择Edit Configuration
并将活动设置为在您指定的活动上运行
解决方案2:
在您的.xml
文件中,进行所需的活动以像这样首先启动
<activity
android:name=".Activities.SplashScreen"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <--- add this 2 lines
<category android:name="android.intent.category.LAUNCHER" /> <---
</intent-filter>
</activity>
并删除旧的。
答案 1 :(得分:0)
简而言之,我创建了一个名为loggingIn的变量,该变量在开始时为false,在用户登录后立即变为true,并且跳过或不跳过配置文件。这样可以防止将来再由MainActivity再次调用onDataChange方法。我只是没有意识到任何其他解决方案,如果有人知道另一种方法,请不要犹豫。否则,谢谢大家的支持!
databaseReference.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
if (loggedIn == false)
{
user.setProfileComplete(dataSnapshot.getValue(User.class).getProfileComplete());
//The profileComplete method returns TRUE, so we can skip the profile creation.
if(user.getProfileComplete())
{
Intent intent = new Intent(MainActivity.this, ViewProfileActivity.class);
startActivity(intent);
loggedIn = true;
}
//the profileComplete method returns FALSE, we must send the user to the profile creation.
else
{
Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
startActivity(intent);
loggedIn = true;
}
}
}