在onBackpress方法上工作时,应用程序没有响应

时间:2018-03-15 14:44:00

标签: android android-layout

我正在使用我的Android应用程序项目,我想用用户界面改善我的应用程序的性能,所以我正在使用onbackpress方法提供双击退出。它第一次运作良好。但当我退出并尝试再次登录我的应用程序时,应用程序正在崩溃。我不知道该怎么做 我从3个月开始从事这个项目。有人请帮帮我.. 我没有时间重新开始这个项目。我不知道请问谁请帮助我。 这是logcat的形象。

logcat image

public class DashBoard extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
DrawerLayout drawer;
NavigationView navigationView;
Toolbar toolbar = null;
private CardView notice, notes, updates, questionpapers, results;
private FirebaseAuth auth;
private TextView txtWelcome;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dash_board);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    txtWelcome = (TextView)findViewById(R.id.textView);
    notice = (CardView) findViewById(R.id.noticecardId);
    notes = (CardView) findViewById(R.id.notescardId);
    questionpapers = (CardView) findViewById(R.id.questioncardId);
    updates = (CardView) findViewById(R.id.updatescardId);
    results = (CardView) findViewById(R.id.resultscardId);
    // add click listeners to cards
    notice.setOnClickListener(this);
    notes.setOnClickListener(this);
    questionpapers.setOnClickListener(this);
    results.setOnClickListener(this);
    updates.setOnClickListener(this);
// initialize firebase auth
    auth = FirebaseAuth.getInstance();
//check login status
    if(auth.getCurrentUser() == null){
  //  txtWelcome.setText(" welcome");
        startActivity(new Intent(DashBoard.this,MainActivity.class));
        finish();
}
}
@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
       super.onBackPressed();
        finish();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.dash_board, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    //here is the main place where we need to work on.
    int id=item.getItemId();
    switch (id) {

        case R.id.nav_notice:
            Intent h = new Intent(DashBoard.this, NoticeActivity.class);
            startActivity(h);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            break;
        case R.id.nav_notes:
            Intent i = new Intent(DashBoard.this, NotesActivity.class);
            startActivity(i);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            break;
        case R.id.nav_question_papers:
            Intent g = new Intent(DashBoard.this, QuestionPaperActivity.class);
            startActivity(g);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            break;
        case R.id.nav_update:
            Intent s = new Intent(DashBoard.this, UpdatesActivity.class);
            startActivity(s);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            break;
        case R.id.nav_logout:
            Intent k = new Intent(DashBoard.this,LogoutActivity.class);
            startActivity(k);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            break;
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;

}
@Override
public void onClick(View view) {
    Intent i;
    switch (view.getId()) {
        case R.id.noticecardId:
            i = new Intent(this, NoticeActivity.class);
            startActivity(i);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            break;
        case R.id.notescardId:
            i = new Intent(this, NotesActivity.class);
            startActivity(i);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            break;
        case R.id.questioncardId:
            i = new Intent(this, QuestionPaperActivity.class);
            startActivity(i);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            break;
        case R.id.updatescardId:
            i = new Intent(this, UpdatesActivity.class);
            startActivity(i);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            break;
        /*case R.id.resultscardId:
            i = new Intent(this, ResultsActivity.class);
            startActivity(i);
            break;
        default:
            break;*/
    }

}
@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

}

这是我的 MainActivity.java 文件

package com.example.madhu.management;



import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
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.RelativeLayout;
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;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button btnLogin;
EditText input_email,input_password;
TextView btnSignup,btnForgotPass;
public static final int NO_NETWORK = 1;

RelativeLayout activity_main;
private ProgressDialog progressDialog;
private FirebaseAuth auth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    progressDialog = new ProgressDialog(this);
    //View
    btnLogin = (Button)findViewById(R.id.login_btn_login);
    input_email = (EditText)findViewById(R.id.login_email);
    input_password = (EditText)findViewById(R.id.login_password);
    btnSignup = (TextView)findViewById(R.id.login_btn_signup);
    btnForgotPass = (TextView)findViewById(R.id.login_btn_forgot_password);
    activity_main = (RelativeLayout)findViewById(R.id.activity_main);

    btnSignup.setOnClickListener(this);
    btnForgotPass.setOnClickListener(this);
    btnLogin.setOnClickListener(this);

    //Init Firebase Auth
    auth = FirebaseAuth.getInstance();

    //Check already session , if ok-> DashBoard
    if(auth.getCurrentUser() != null)
        startActivity(new Intent(MainActivity.this,DashBoard.class));
}

@Override
public void onClick(View view) {
    if(view.getId() == R.id.login_btn_forgot_password)
    {
        startActivity(new Intent(MainActivity.this,ForgotPassword.class));
        finish();
    }
    else if(view.getId() == R.id.login_btn_signup)
    {
        startActivity(new Intent(MainActivity.this,SignUp.class));
        finish();
    }
    else if(view.getId() == R.id.login_btn_login)
    {
        if(TextUtils.isEmpty(input_email.getText().toString()) && TextUtils.isEmpty(input_password.getText().toString())){

            Toast.makeText(MainActivity.this, "Please fill Required fields", Toast.LENGTH_LONG).show();

        }else {
            loginUser(input_email.getText().toString(), input_password.getText().toString());
        }
    }
}

private void loginUser(String email, final String password) {
    progressDialog.setMessage("Loging in please wait...");
    progressDialog.show();
    auth.signInWithEmailAndPassword(email,password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    progressDialog.dismiss();
                    if(!task.isSuccessful()) {
                        Toast.makeText(MainActivity.this, "Unable to login...["+task.getException()+"]", Toast.LENGTH_LONG).show();


                        if(password.length() < 6)
                        {
                            Toast.makeText(MainActivity.this, "Password length must be over 6", Toast.LENGTH_LONG).show();

//                                Snackbar snackBar = Snackbar.make(activity_main,"Password length must be over 6",Snackbar.LENGTH_LONG);
  //                              snackBar.show();
                        }
                    }
                    else{

                        startActivity(new Intent(MainActivity.this,DashBoard.class));
                    }
                }
            });
}
}

并且错误指向此行

 auth.signInWithEmailAndPassword(email,password)

和这个     loginUser(input_email.getText().toString(), input_password.getText().toString());

0 个答案:

没有答案