Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.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 如何解决从settingactivity移动到mainactivity的问题?_Java_Android_Firebase - Fatal编程技术网

Java 如何解决从settingactivity移动到mainactivity的问题?

Java 如何解决从settingactivity移动到mainactivity的问题?,java,android,firebase,Java,Android,Firebase,我有settingactivity,当用户按下更新配置文件按钮时,我希望用户将其从settingactivity移动到mainactivity。我尝试删除@NonNull,但它没有影响我尝试更改的任何内容,即使是意图也没有影响,但代码没有影响 package com.example.chat; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.

我有settingactivity,当用户按下更新配置文件按钮时,我希望用户将其从settingactivity移动到mainactivity。我尝试删除@NonNull,但它没有影响我尝试更改的任何内容,即使是意图也没有影响,但代码没有影响

package com.example.chat;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.HashMap;

import de.hdodenhof.circleimageview.CircleImageView;

public class SettingsActivity extends AppCompatActivity {

    private Button UpdateAccountSettings;
    private EditText userName, userStatus;
    private CircleImageView userProfileImage;
    private String currentUserID;
    private FirebaseAuth mAuth;
    private DatabaseReference RootRef;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        mAuth =FirebaseAuth.getInstance();
        currentUserID = mAuth.getCurrentUser().getUid();
        RootRef = FirebaseDatabase.getInstance().getReference();
        InitializeFields();
        UpdateAccountSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(@NonNull View view) {
                UpdateSettings();
            }
        });
    }

    private void InitializeFields() {
        UpdateAccountSettings = findViewById(R.id.update_setting_button);
        userName = findViewById(R.id.set_user_name);
        userStatus = findViewById(R.id.set_profile_status);
        userProfileImage = findViewById(R.id.profile_image);
    }

    private void UpdateSettings() {
        String setUserName = userName.getText().toString();
        String setStatus = userStatus.getText().toString();
        if (TextUtils.isEmpty(setUserName)){
            Toast.makeText(this, "Username Is Empty!!!", Toast.LENGTH_LONG).show();
        }
        if (TextUtils.isEmpty(setStatus)){
            Toast.makeText(this, "Status Is Empty!!!", Toast.LENGTH_LONG).show();
        }
        else {
            HashMap<String, Object> profileMap = new HashMap<>();
            profileMap.put("uid", currentUserID);
            profileMap.put("name", setUserName);
            profileMap.put("status", setStatus);
            RootRef.child("User").child(currentUserID).setValue(profileMap)
                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(Task<Void> task) {
                            if (task.isSuccessful()){
                                SendUserToMainActivity();
                                Toast.makeText(SettingsActivity.this, "Profile Updated.", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                String message = task.getException().toString();
                                Toast.makeText(SettingsActivity.this, "Error " + message, Toast.LENGTH_LONG).show();
                            }
                        }
                    });
        }
    }

    private void SendUserToMainActivity() {
        Intent mainIntent = new Intent(SettingsActivity.this, MainActivity.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(mainIntent);
        finish();
    }
}
package com.example.chat;
导入androidx.annotation.NonNull;
导入androidx.appcompat.app.appcompat活动;
导入android.content.Intent;
导入android.os.Bundle;
导入android.text.TextUtils;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
导入com.google.android.gms.tasks.OnCompleteListener;
导入com.google.android.gms.tasks.Task;
导入com.google.firebase.auth.FirebaseAuth;
导入com.google.firebase.database.DatabaseReference;
导入com.google.firebase.database.FirebaseDatabase;
导入java.util.HashMap;
导入de.hdodenhof.circleimageview.circleimageview;
公共类设置活动扩展了AppCompatingActivity{
私人按钮更新计数设置;
私有编辑文本用户名、用户状态;
私有CircleImageView用户档案图像;
私有字符串currentUserID;
私人消防队;
私有数据库引用RootRef;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u设置);
mAuth=FirebaseAuth.getInstance();
currentUserID=mAuth.getCurrentUser().getUid();
RootRef=FirebaseDatabase.getInstance().getReference();
初始化字段();
UpdateAccountSettings.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(@NonNull视图){
更新设置();
}
});
}
私有void InitializeFields(){
UpdateAccountSettings=findViewById(R.id.update\u设置\u按钮);
userName=findviewbyd(R.id.set\u用户名);
userStatus=findviewbyd(R.id.set\u profile\u status);
userProfileImage=findviewbyd(R.id.profile\u image);
}
私有void更新设置(){
字符串setUserName=userName.getText().toString();
字符串setStatus=userStatus.getText().toString();
if(TextUtils.isEmpty(setUserName)){
Toast.makeText(这个“用户名为空!!!”,Toast.LENGTH\u LONG.show();
}
if(TextUtils.isEmpty(setStatus)){
Toast.makeText(这个“状态为空!!!”,Toast.LENGTH\u LONG.show();
}
否则{
HashMap profileMap=新的HashMap();
profileMap.put(“uid”,currentUserID);
profileMap.put(“name”,setUserName);
profileMap.put(“状态”,setStatus);
RootRef.child(“用户”).child(currentUserID).setValue(profileMap)
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(任务){
if(task.issusccessful()){
SendUserToMainActivity();
Toast.makeText(SettingsActivity.this,“配置文件更新”,Toast.LENGTH_SHORT.show();
}
否则{
字符串消息=task.getException().toString();
Toast.makeText(SettingsActivity.this,“Error”+消息,Toast.LENGTH_LONG.show();
}
}
});
}
}
私有void SendUserToMainActivity(){
Intent maintent=新的Intent(setingsactivity.this,MainActivity.class);
maintent.addFlags(Intent.FLAG_活动_新任务| Intent.FLAG_活动_清除任务);
星触觉(主旨);
完成();
}
}
当我按下更新配置文件按钮时,它会显示配置文件已更新,但不会转到主活动
提前感谢。

在您的主要活动中尝试此功能

 private void SendUserToSettingsActivity() {
            Intent mainIntent = new Intent(MainActivity.this, SettingsActivity.class);
            startActivity(mainIntent)
        }
    private void VerifyUserExistance() {
            String currentUserID = mAuth.getCurrentUser().getUid();
            RootRef.child("Users").child(currentUserID).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.child("name").exists()){
                        Toast.makeText(MainActivity.this, "Welcome", Toast.LENGTH_SHORT).show();
                    }
                    else {
Toast.makeText(MainActivity.this, "CALLED AGAIN", Toast.LENGTH_SHORT).show();
                        SendUserToSettingActivity();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
和来自设置活动

 private void SendUserToMainActivity() {

            finish();
        }
我调试了您的代码,上面的代码运行良好,您在这个主要活动中添加了一个数据更改侦听器

 private void SendUserToSettingsActivity() {
            Intent mainIntent = new Intent(MainActivity.this, SettingsActivity.class);
            startActivity(mainIntent)
        }
    private void VerifyUserExistance() {
            String currentUserID = mAuth.getCurrentUser().getUid();
            RootRef.child("Users").child(currentUserID).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.child("name").exists()){
                        Toast.makeText(MainActivity.this, "Welcome", Toast.LENGTH_SHORT).show();
                    }
                    else {
Toast.makeText(MainActivity.this, "CALLED AGAIN", Toast.LENGTH_SHORT).show();
                        SendUserToSettingActivity();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
当您从“设置”活动更新子项时,将调用
onDataChange
侦听器,并且您正在设置的新用户名不存在,并且您的else案例将再次调用“设置”活动


MainActivity>SettingsActivity>调用更新时,设置活动会被销毁,但会从上面的部分再次调用。尝试上面的方法,您将看到toast“再次调用”

如果您不一定需要这些标志,我建议您现在删除它们

另外,在您从Github()共享的代码中,
SendUserToMainActivity()
函数的代码是:

private void SendUserToMainActivity() {
    Intent mainIntent = new Intent(SettingsActivity.this, MainActivity.class);
    setResult(Activity.RESULT_OK, mainIntent);
    finish();
}
在这段代码中,您正在创建意图,但从未使用它。尽管您在StackOverflow上共享的代码确实包含此部分,但Github上的代码却没有。将Github上的代码替换为:

private void SendUserToMainActivity()
    startActivity(new Intent(this, MainActivity.class);
}
此外,您应该使用调试器或调试消息来了解程序是否实际达到此函数。虽然可能应该这样做,因为您正在调用该函数,但是,检查不会有任何伤害。因此,使用此代码了解函数是否使用调试消息执行(或直接使用调试器):


除此之外,我不能在您的代码中发现任何其他错误。

@LakhwinderSingh
SendUserToMainActivity()
是一个具有意图的函数。@LakhwinderSingh如果数据正确存储在数据库中,我该怎么办@SiddHmistry它应该可以工作,你的代码是正确的,告诉我你的
main活动
@Ashish是的,它正在正确存储。很好,现在工作正常。我只是重新编写整个代码。谢谢大家的帮助