应用程序不会停留在主要活动上,它会一直将我重定向到设置活动

时间:2018-11-22 10:43:21

标签: java android firebase firebase-realtime-database firebase-authentication

嗨,我是编码新手,我正在开发社交媒体应用,

当我保存自己的设置活动时,它显示成功,而在Firebase数据库上,它显示该用户已注册,但在设置活动后立即发送回主活动。 在Logcat中它说: 无法启动活动ComponentInfo {aj_studio.bfit / aj_studio.bfit.loginActivity}:android.view.InflateException:二进制XML文件第2行:二进制XML文件第2行:膨胀类错误

主要活动:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        mToolbar= findViewById(R.id.main_page_toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("Home");



        mAuth= FirebaseAuth.getInstance();
        currentuserid= mAuth.getCurrentUser().getUid();
        userref=FirebaseDatabase.getInstance().getReference().child("user");


        drawerlayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerlayout,R.string.open,R.string.closer);
        drawerlayout.addDrawerListener(ActionBarDrawerToggle);
        ActionBarDrawerToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);



        navigationview=(NavigationView)findViewById(R.id.navigation);
        View nav =navigationview.inflateHeaderView(R.layout.navigation_header);


        navigationview.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {


                useMenueSelectorItem(item);
                return false;



            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();

        FirebaseUser currentUser =mAuth.getCurrentUser();

        if (currentUser== null){

            SendUserToLoginActivity();
        }
        else {
            checkforuserexistance();
        }





    }

    private void checkforuserexistance() {


        final String current_user_id;
        current_user_id = mAuth.getCurrentUser().getUid();


        userref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if(!dataSnapshot.hasChild(current_user_id))
                {
                    sendusertosetupactivity();
                }


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private void SendUserToLoginActivity() {
        Intent loginintent = new Intent(MainActivity.this,loginActivity.class);
        loginintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(loginintent);
        finish();
    }
    private void sendusertosetupactivity() {


        Intent send_user_to_setup_activity = new Intent(MainActivity.this,setupActivity.class);
        send_user_to_setup_activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(send_user_to_setup_activity);
        finish();


    }








    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(ActionBarDrawerToggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void useMenueSelectorItem(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navProfile:

                Toast.makeText(this, "profile", Toast.LENGTH_SHORT).show();
                break;



            case R.id.nav_exercises:

                Toast.makeText(this, "exercises", Toast.LENGTH_SHORT).show();
                break;


            case R.id.nav_findfriend:

                Toast.makeText(this, "find friends", Toast.LENGTH_SHORT).show();
                break;



            case R.id.nav_friends:

                Toast.makeText(this, "friends", Toast.LENGTH_SHORT).show();
                break;



            case R.id.nav_Home:

                Toast.makeText(this, "home", Toast.LENGTH_SHORT).show();
                break;



            case R.id.nav_logout:

                mAuth.signOut();
                SendUserToLoginActivity();
                break;



            case R.id.nav_messages:

                Toast.makeText(this, "messages", Toast.LENGTH_SHORT).show();
                break;


            case R.id.nav_nutrition:

                Toast.makeText(this, "nutrition", Toast.LENGTH_SHORT).show();
                break;



            case R.id.nav_settings:

                Toast.makeText(this, "settings", Toast.LENGTH_SHORT).show();
                break;



            case R.id.nav_Tips:

                Toast.makeText(this, "trips", Toast.LENGTH_SHORT).show();
                break;


        }

}}

启动活动:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);


        username = findViewById(R.id.setup_username);
        fullname = findViewById(R.id.setup_fullname);
        goal = findViewById(R.id.setup_goal);
        saveaccountsetupinformation = findViewById(R.id.setup_save);
        profileimage= findViewById(R.id.profile_image);
        Loadingprogessbar = new ProgressDialog(this);

        mAuth= FirebaseAuth.getInstance();
        current_User_ID = mAuth.getCurrentUser().getUid();
        useRef = FirebaseDatabase.getInstance().getReference().child("Users").child(current_User_ID);

        saveaccountsetupinformation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                savingandprocessingaccountinformation();
            }
        });



}

    private void savingandprocessingaccountinformation() {

        String username_1 = username.getText().toString();
        String fullname_1 = fullname.getText().toString();
        String goal_1 = goal.getText().toString();


        if (TextUtils.isEmpty(username_1))
        {
            Toast.makeText(this,"please enter username",Toast.LENGTH_SHORT).show();
        }
        if (TextUtils.isEmpty(fullname_1))
        {
            Toast.makeText(this,"please enter full name",Toast.LENGTH_SHORT).show();
        }
        if (TextUtils.isEmpty(goal_1))
        {
            Toast.makeText(this,"please enter goal",Toast.LENGTH_SHORT).show();

    }
    else{
            Loadingprogessbar.setTitle("Saving Information");
            Loadingprogessbar.setMessage("Please wait while your account is being processed");
            Loadingprogessbar.show();
            Loadingprogessbar.setCanceledOnTouchOutside(true);


            HashMap UserMap = new HashMap();
            UserMap.put("username",username_1);
            UserMap.put("fullname",fullname_1);
            UserMap.put("goal",goal_1);
            UserMap.put("Status","Get fit ..live fit... Bfit");
            UserMap.put("gender","none");
            UserMap.put("Dob","none");
            useRef.updateChildren(UserMap).addOnCompleteListener(new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    SendUserToMainActivity();
                    if(task.isSuccessful())
                    {
                        Toast.makeText(setupActivity.this,"your account was successfully setup",Toast.LENGTH_LONG).show();
                        Loadingprogessbar.dismiss();
                    }
                    else
                        {
                            String message = task.getException().getMessage();
                            Toast.makeText(setupActivity.this,"error occurred: "+message,Toast.LENGTH_LONG).show();
                            Loadingprogessbar.dismiss();
                        }
                }
            });


        }
}

    private void SendUserToMainActivity()
    {
        Intent mainIntent = new Intent(setupActivity.this, MainActivity.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(mainIntent);
        finish();

  the manifest
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        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=".loginActivity" />
        <activity android:name=".RegisterActivity" />
        <activity android:name=".setupActivity"></activity>
    </application>

</manifest>

登录活动

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.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class loginActivity extends AppCompatActivity {

    private Button loginbutton;
    private EditText useremail,userpassword;
    private TextView NeedNewAccountLink;
    private ProgressDialog Loadingprogessbar;

    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mAuth = FirebaseAuth.getInstance();


        loginbutton = findViewById(R.id.login_create_account);
        useremail=(EditText) findViewById(R.id.login_email);
        userpassword =(EditText) findViewById(R.id.login_password);
        NeedNewAccountLink=findViewById(R.id.click_to_register);
        Loadingprogessbar = new ProgressDialog(this);


        NeedNewAccountLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                sendusertoregisteractivity();

            }
        });

        loginbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                allowingusertoenteraccount();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        FirebaseUser currentUser =mAuth.getCurrentUser();

        if (currentUser != null){

            SendUserTomainActivity();
        }

        }

    private void SendUserTomainActivity() {

        Intent sendusertomainactivity = new Intent(loginActivity.this,MainActivity.class);
        sendusertomainactivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(sendusertomainactivity);
        finish();
    }


    private void allowingusertoenteraccount() {
        String email = useremail.getText().toString();
        String password = userpassword.getText().toString();


        if (TextUtils.isEmpty(email)){
            Toast.makeText(this,"please input your email",Toast.LENGTH_SHORT).show();

        }
        else if (TextUtils.isEmpty(password)){
            Toast.makeText(this,"please input your password",Toast.LENGTH_SHORT).show();

        }
        else{

            Loadingprogessbar.setTitle("Login");
            Loadingprogessbar.setMessage("Please wait, while we are accessing your account...");
            Loadingprogessbar.setCanceledOnTouchOutside(true);
            Loadingprogessbar.show();


            mAuth.signInWithEmailAndPassword(email,password)
                    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {

                            if (task.isSuccessful()){
                                sendusertomainactivity();
                                Toast.makeText(loginActivity.this,"welcome to BFit",Toast.LENGTH_SHORT).show();
                                Loadingprogessbar.dismiss();

                            }
                            else{
                                String message = task.getException().getMessage();
                                Toast.makeText(loginActivity.this,"Error Occurred: "+message,Toast.LENGTH_SHORT).show();
                                Loadingprogessbar.dismiss();
                            }

                        }
                    });

        }
    }

    private void sendusertomainactivity() {
        Intent sendusertomainactivity =new Intent(loginActivity.this,MainActivity.class);
        sendusertomainactivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(sendusertomainactivity);
        finish();
    }

    private void sendusertoregisteractivity() {

        Intent registerintent = new Intent(loginActivity.this,RegisterActivity.class);
        registerintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(registerintent);
        finish();

    }
}

0 个答案:

没有答案