所以我是android开发的新面孔,我遇到了一个问题,我一直在为它循环。
我在这里有三个活动,它们用于注册,我不想实现scrollview,因为它不是那么好看,所以我做了三个表单活动,
第一个活动收集来自XML的所有信息,并将值作为意图传递给第二个活动, 然后第二个活动收集来自它自己的XML的所有信息,然后将它与第一个活动的Extra一起传递给第三个活动,第三个活动连同第一个和第二个活动一起传递加上它最后,将值保存到firebase中。
是的,这些值已经保存了,但我的问题是第三个活动没有进入欢迎课程,而该课程据说是根据其意图撰写的。我尝试过重建和清洁,但它没有用。我无法找到工作中的错误,任何人都可以帮助我吗?任何帮助表示赞赏。感谢
这是我的三个java类,我也会在欢迎课程中加入你可能要求它:
Profile.java
public class Profile extends Activity {
//DECLARE FIELDS
EditText name,username, phone, age, birth;
Button saveBtn;
//FIREBASE REF
DatabaseReference userRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
//FIREBASE DATABASE REF
userRef = FirebaseDatabase.getInstance().getReference("Users");
//ASSIGN ID's
saveBtn = (Button) findViewById(R.id.profileBtn);
name = (EditText) findViewById(R.id.profileName);
username = (EditText) findViewById(R.id.profileUsername);
age = (EditText) findViewById(R.id.profileAge);
phone = (EditText) findViewById(R.id.profilePhone);
birth = (EditText) findViewById(R.id.profileBirth);
// SAVE BUTTON LOGIC
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addUser();
}
});
}
private void addUser(){
String userNameString = name.getText().toString();
String userPhoneString = phone.getText().toString();
String userAgeString = age.getText().toString();
String userBirthString = birth.getText().toString();
String userUserNameString = username.getText().toString();
//GET USER KEY FROM INTENT
String userKey = getIntent().getStringExtra("USER_KEY");
String userEmail = getIntent().getStringExtra("USER_EMAIL");
String userPass = getIntent().getStringExtra("USER_PASS");
String userID = getIntent().getStringExtra("USER_ID");
if (!TextUtils.isEmpty(userNameString) && !TextUtils.isEmpty(userPhoneString) && !TextUtils.isEmpty(userAgeString) && !TextUtils.isEmpty(userBirthString) && !TextUtils.isEmpty(userUserNameString)) {
Toast.makeText(Profile.this, "Next!", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(Profile.this, Profile2.class);
myIntent.putExtra("USER_KEY", userKey);
myIntent.putExtra("USER_NAME", userNameString);
myIntent.putExtra("USER_PHONE", userPhoneString);
myIntent.putExtra("USER_AGE", userAgeString);
myIntent.putExtra("USER_BIRTH", userBirthString);
myIntent.putExtra("USER_USERNAME", userUserNameString);
myIntent.putExtra("USER_EMAIL", userEmail);
myIntent.putExtra("USER_PASS", userPass);
myIntent.putExtra("USER_ID", userID);
startActivity(myIntent);
} else {
Toast.makeText(Profile.this, "Please Enter Correct Profile Details!", Toast.LENGTH_LONG).show();
startActivity(new Intent(Profile.this, Profile.class));
}
}
}
Profile2.java
public class Profile2 extends Activity {
//DECLARE FIELDS
EditText Address, FSN, bType, Height, Weight;
Button saveBtn;
//FIREBASE REF
DatabaseReference userRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile2);
//FIREBASE DATABASE REF
userRef = FirebaseDatabase.getInstance().getReference("Users");
//ASSIGN ID's
saveBtn = (Button) findViewById(R.id.profileBtn);
Address = (EditText) findViewById(R.id.Address);
FSN = (EditText) findViewById(R.id.FSN);
bType = (EditText) findViewById(R.id.bType);
Height = (EditText) findViewById(R.id.Height);
Weight = (EditText) findViewById(R.id.Weight);
//MOVE TO LOGIN
// SAVE BUTTON LOGIC
saveBtn.setOnClickListener(new View.OnClickListener() {
// @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View view) {
addUser();
}
});
}
private void addUser(){
String AddressString = Address.getText().toString();
String FSNString = FSN.getText().toString();
String WeightString = Weight.getText().toString();
String bTypeString = bType.getText().toString();
String HeightString = Height.getText().toString();
//GET USER KEY FROM INTENT
String userKey = getIntent().getStringExtra("USER_KEY");
String userNameString = getIntent().getStringExtra("USER_NAME");
String userPhoneString = getIntent().getStringExtra("USER_PHONE");
String userAgeString = getIntent().getStringExtra("USER_AGE");
String userBirthString = getIntent().getStringExtra("USER_BIRTH");
String userUserNameString = getIntent().getStringExtra("USER_USERNAME");
String userPass = getIntent().getStringExtra("USER_PASS");
String userID = getIntent().getStringExtra("USER_ID");
String userEmail = getIntent().getStringExtra("USER_EMAIL");
if (!TextUtils.isEmpty(AddressString) && !TextUtils.isEmpty(FSNString) && !TextUtils.isEmpty(WeightString) && !TextUtils.isEmpty(bTypeString) && !TextUtils.isEmpty(HeightString)) {
Toast.makeText(Profile2.this, "Go mamshie!", Toast.LENGTH_LONG).show();
Intent myIntent2 = new Intent(Profile2.this, Profile3.class);
myIntent2.putExtra("USER_KEY", userKey);
myIntent2.putExtra("USER_NAME", userNameString);
myIntent2.putExtra("USER_PHONE", userPhoneString);
myIntent2.putExtra("USER_AGE", userAgeString);
myIntent2.putExtra("USER_BIRTH", userBirthString);
myIntent2.putExtra("USER_USERNAME", userUserNameString);
myIntent2.putExtra("USER_ADDRESS", AddressString);
myIntent2.putExtra("USER_FSN", FSNString);
myIntent2.putExtra("USER_BTYPE", bTypeString);
myIntent2.putExtra("USER_HEIGHT", HeightString);
myIntent2.putExtra("USER_WEIGHT", WeightString);
myIntent2.putExtra("USER_PASS", userPass);
myIntent2.putExtra("USER_ID", userID);
myIntent2.putExtra("USER_EMAIL", userEmail);
startActivity(myIntent2);
} else {
Toast.makeText(Profile2.this, "Please Enter Correct Profile Details!", Toast.LENGTH_LONG).show();
startActivity(new Intent(Profile2.this, Profile2.class));
}
}
}
Profile3.java
public class Profile3 extends Activity {
//DECLARE FIELDS
EditText NPP, PCS, MC, SB, PPH;
Button saveBtn;
//FIREBASE REF
DatabaseReference mDataRef, userRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile3);
//FIREBASE DATABASE REF
userRef = FirebaseDatabase.getInstance().getReference("Users");
//ASSIGN ID's
saveBtn = (Button) findViewById(R.id.profileBtn);
NPP = (EditText) findViewById(R.id.previous);
PCS = (EditText) findViewById(R.id.previouscs);
MC = (EditText) findViewById(R.id.miscar);
SB = (EditText) findViewById(R.id.stillbirth);
PPH = (EditText) findViewById(R.id.post);
//MOVE TO LOGIN
// SAVE BUTTON LOGIC
saveBtn.setOnClickListener(new View.OnClickListener() {
// @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View view) {
addUser();
}
});
}
private void addUser(){
String NoPrevPregnancies = NPP.getText().toString();
String PreviousCS = PCS.getText().toString();
String Miscarriages = MC.getText().toString();
String Stillbirth = SB.getText().toString();
String PostPartumHem = PPH.getText().toString();
//GET USER KEY FROM INTENT
String userKey = getIntent().getStringExtra("USER_KEY");
String userEmail = getIntent().getStringExtra("USER_EMAIL");
String userPass = getIntent().getStringExtra("USER_PASSWORD");
String userID = getIntent().getStringExtra("USER_ID");
String userNameString = getIntent().getStringExtra("USER_NAME");
String userPhoneString = getIntent().getStringExtra("USER_PHONE");
String userAgeString = getIntent().getStringExtra("USER_AGE");
String userBirthString = getIntent().getStringExtra("USER_BIRTH");
String userUserNameString = getIntent().getStringExtra("USER_USERNAME");
String AddressString = getIntent().getStringExtra("USER_ADDRESS");
String FSNString = getIntent().getStringExtra("USER_FSN");
String bTypeString = getIntent().getStringExtra("USER_BTYPE");
String HeightString = getIntent().getStringExtra("USER_HEIGHT");
String WeightString = getIntent().getStringExtra("USER_WEIGHT");
mDataRef = userRef.child(userNameString);
if (!TextUtils.isEmpty(PreviousCS) && !TextUtils.isEmpty(NoPrevPregnancies) && !TextUtils.isEmpty(PostPartumHem) && !TextUtils.isEmpty(Miscarriages) && !TextUtils.isEmpty(Stillbirth)) {
User3 user = new User3(PreviousCS, NoPrevPregnancies, PostPartumHem, Miscarriages, Stillbirth);
userRef.child(userNameString).setValue(user);
mDataRef.child("USER_KEY").setValue(userKey);
mDataRef.child("USER_PASS").setValue(userPass);//
mDataRef.child("USER_ID").setValue(userID);//
mDataRef.child("USER_NAME").setValue(userNameString);//
mDataRef.child("USER_PHONE").setValue(userPhoneString);
mDataRef.child("USER_AGE").setValue(userAgeString);
mDataRef.child("USER_BIRTH").setValue(userBirthString);
mDataRef.child("USER_USERNAME").setValue(userUserNameString);//
mDataRef.child("USER_ADDRESS").setValue(AddressString);
mDataRef.child("USER_FSN").setValue(FSNString);
mDataRef.child("USER_BTYPE").setValue(bTypeString);
mDataRef.child("USER_HEIGHT").setValue(HeightString);
mDataRef.child("USER_WEIGHT").setValue(WeightString);
mDataRef.child("USER_EMAIL").setValue(userEmail);//
Toast.makeText(Profile3.this, "User Details Completed and Saved!", Toast.LENGTH_LONG).show();
Intent myIntent1 = new Intent(Profile3.this, Welcome.class);
myIntent1.putExtra("USER_KEY", userKey);
myIntent1.putExtra("USER_EMAIL", userEmail);
myIntent1.putExtra("USER_PHONE", userPhoneString);
myIntent1.putExtra("USER_BIRTH", userBirthString);
myIntent1.putExtra("USER_USERNAME", userUserNameString);
myIntent1.putExtra("USER_NAME", userNameString);
myIntent1.putExtra("USER_AGE", userAgeString);
myIntent1.putExtra("USER_ID", userID);
startActivity(myIntent1);
} else {
Toast.makeText(Profile3.this, "Please Enter Correct Profile Details!", Toast.LENGTH_LONG).show();
startActivity(new Intent(Profile3.this, Profile3.class));
}
}
}
我的欢迎活动获取一些数据并显示
public class Welcome extends AppCompatActivity {
private static final String TAG = "ViewDatabase";
//ADD FIREBASE STUFF
//DECLARE FIELDS
Button outBtn;
TextView welcome;
private DatabaseReference myRef, mDataRef, userRef;
private FirebaseDatabase mFirebaseDatabase;
private String userIDPassed;
private String userID;
private String userKey;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private ListView mListView;
//FIREBASE AUTH FIELDS
private FirebaseAuth nAuth;
private FirebaseAuth.AuthStateListener nAuthlistener;
//GET USER KEY FROM INTENT
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
//DRAWER LAYOUT
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
//ASSIGN IDS
outBtn = (Button) findViewById(R.id.logoutBtn);
welcome = (TextView) findViewById(R.id.WelcomeName);
mListView = (ListView) findViewById(R.id.listview);
//ASSIGN INSTANCE
myRef = FirebaseDatabase.getInstance().getReference().child("Users");
nAuth = FirebaseAuth.getInstance();
userRef = FirebaseDatabase.getInstance().getReference("Users");
FirebaseUser User = nAuth.getCurrentUser();
userID = User.getUid();
//navigation Drawer
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView mNavigationView = (NavigationView) findViewById(R.id.nav_menu);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override public boolean onNavigationItemSelected(MenuItem menuItem)
{ switch (menuItem.getItemId())
{
case(R.id.nav_account): Intent accountActivity = new Intent(getApplicationContext(), Welcome.class);
startActivity(accountActivity);
break;
case(R.id.nav_exercises): Intent accountActivity1 = new Intent(getApplicationContext(), Video.class);
startActivity(accountActivity1);
break;
case(R.id.nav_tips): Intent accountActivity2 = new Intent(getApplicationContext(), Image.class);
startActivity(accountActivity2);
break;
case(R.id.nav_scheduler): Intent accountActivity3 = new Intent(getApplicationContext(), CalendarActivity.class);
startActivity(accountActivity3);
break;
case(R.id.nav_logout): Intent accountActivity4 = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(accountActivity4);
finish();
break;
case(R.id.nav_settings): Intent accountActivity5 = new Intent(getApplicationContext(), Profile.class);
startActivity(accountActivity5);
break;
case(R.id.nav_info): Intent accountActivity6 = new Intent(getApplicationContext(), Info.class);
startActivity(accountActivity6);
break;
}
return true;
} });
//Navigation Drawer
nAuthlistener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser User = firebaseAuth.getCurrentUser();
if (User != null){
Log.d(TAG, "onAuthStateChanged:signed_in:" + User.getUid());
Toast.makeText(Welcome.this, "Successfully signed in with: " + User.getEmail(), Toast.LENGTH_LONG).show();
}else{
Log.d(TAG, "onAuthStateChanged:signed_out" + userID);
Toast.makeText(Welcome.this, "Successfully signed out.", Toast.LENGTH_LONG).show();
}
}
};
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange (DataSnapshot dataSnapshot){
showData(dataSnapshot);
}
@Override
public void onCancelled (DatabaseError databaseError){
}
});
outBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nAuth.signOut();
finish();
startActivity(new Intent(Welcome.this, MainActivity.class));
}
});
}
private void showData(DataSnapshot dataSnapshot) {
//GET USER KEY FROM INTENT
String userKey = getIntent().getStringExtra("USER_KEY");
String userEmail = getIntent().getStringExtra("USER_EMAIL");
String userPhone = getIntent().getStringExtra("USER_PHONE");
String userBirth = getIntent().getStringExtra("USER_BIRTH");
String userUserName = getIntent().getStringExtra("USER_USERNAME");
String userName = getIntent().getStringExtra("USER_NAME");
String userAge = getIntent().getStringExtra("USER_AGE");
String userID = getIntent().getStringExtra("USER_ID");
mDataRef = userRef.child(userName);
if (!TextUtils.isEmpty(userKey) && !TextUtils.isEmpty(userEmail) && !TextUtils.isEmpty(userID) && !TextUtils.isEmpty(userBirth) && !TextUtils.isEmpty(userUserName)) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
Log.d(TAG, "showData: snapshot: " + ds);
Log.d(TAG, "showData: snapshot: " + ds.child("Users"));
//display all info taken
Log.d(TAG, "showData: USER_NAME: " + userName);
Log.d(TAG, "showData: USER_AGE: " + userAge);
Log.d(TAG, "showData: USER_BIRTH: " + userBirth);
Log.d(TAG, "showData: USER_PHONE: " + userPhone);
Log.d(TAG, "showData: USER_USERNAME: " + userUserName);
ArrayList<String> array = new ArrayList<>();
array.add(userName);
array.add(userAge);
array.add(userBirth);
array.add(userPhone);
array.add(userUserName);
ArrayAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,array);
mListView.setAdapter(adapter);
}
} else {
}
}
@Override
protected void onStart() {
super.onStart();
nAuth.addAuthStateListener(nAuthlistener);
}
@Override
protected void onStop() {
super.onStop();
nAuth.removeAuthStateListener(nAuthlistener);
}
//FOR NAVIGATION DRAWER
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
//Navigation Drawer End
}
我做错了什么?把它想象成流动的水,我认为这些价值一直持续到最后一次活动?
答案 0 :(得分:0)
在个人资料1中,将意图更改为个人资料,即个人资料2,例如
Toast.makeText(Profile.this,“请输入正确的配置文件详细信息!”,Toast.LENGTH_LONG).show(); startActivity(new Intent(Profile.this,Profile2.class));
并对配置文件2和配置文件3进行相同操作。
答案 1 :(得分:-1)
通过删除您的else语句尝试调试,并可能将它们全部添加到上一个活动