java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.myapplication / com.example.myapplication.Main2Activity}:

时间:2020-01-19 12:27:40

标签: java android xml firebase android-studio

我的应用程序具有主要活动,注册活动,登录活动和主要活动2,我想在其中使用底部导航活动

它在我创建新内容时起作用,然后在通知片段中进行了一些修改之后,所以知道当我oggin用户应重定向2 mainactivity2但未重定向

时,会发生此运行时错误

Mainacyivity2 xml代码


    package com.example.myapplication;

    import android.os.Bundle;

    import com.google.android.material.bottomnavigation.BottomNavigationView;

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.navigation.NavController;
    import androidx.navigation.Navigation;
    import androidx.navigation.ui.AppBarConfiguration;
    import androidx.navigation.ui.NavigationUI;

    public class Main2Activity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            BottomNavigationView navView = findViewById(R.id.nav_view);
            // Passing each menu ID as a set of Ids because each
            // menu should be considered as top level destinations.
            AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                    R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                    .build();
            NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
            NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
            NavigationUI.setupWithNavController(navView, navController);
        }

    }


Mainactivity2 java code

package com.example.myapplication;

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

}

这是我的LoginActivity

package com.example.myapplication;

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.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.Model.Users;
import com.example.myapplication.Prevalent.Prevalent;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rey.material.widget.CheckBox;

import io.paperdb.Paper;

public class Login extends AppCompatActivity
{
    private EditText inputphone, inputpassword;
    private Button LoginButton;
    private ProgressBar pb;
    private String ParentDatabaseName= "Users";
    private CheckBox chkBowRememberme;
    private TextView Admin,NotAdmin;

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

        LoginButton= (Button) findViewById(R.id.loginbutton);

        inputphone = (EditText) findViewById(R.id.phno);
        inputpassword = (EditText) findViewById(R.id.password2);


        LoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginUser();
            }
        });

        Admin = (TextView) findViewById(R.id.admin);
        NotAdmin = (TextView) findViewById(R.id.Notadmin);


        chkBowRememberme = (CheckBox) findViewById(R.id.checkbox);
        Paper.init(this);


        Admin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginButton.setText("Login Admin");
                Admin.setVisibility(View.INVISIBLE);
                NotAdmin.setVisibility(View.VISIBLE);
                ParentDatabaseName = "Admin";
            }
        });

        NotAdmin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginButton.setText("Login");
                NotAdmin.setVisibility(View.INVISIBLE);
                Admin.setVisibility(View.VISIBLE);
                ParentDatabaseName = "Users";
            }
        });

    }

    private void LoginUser()
    {
        String l_phno= inputphone.getText().toString();
        String l_password= inputpassword.getText().toString();

        if (TextUtils.isEmpty(l_phno))
        {
            Toast.makeText(this, "Write Phone No", Toast.LENGTH_SHORT).show();
        }

        else if (TextUtils.isEmpty(l_password))
        {
            Toast.makeText(this, "Write Your Password", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Allow_User(l_phno,l_password);
        }
    }

    private void Allow_User(final String l_phno, final String l_password)
    {

        if (chkBowRememberme.isChecked())
        {
            Paper.book().write(Prevalent.UserPhoneKey, l_phno);
            Paper.book().write(Prevalent.UserPasswordKey, l_password);

        }


        final DatabaseReference RootRef;
        RootRef = FirebaseDatabase.getInstance().getReference();

        RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
            {
                if (dataSnapshot.child(ParentDatabaseName).child(l_phno).exists())
                {
                    Users userData = dataSnapshot.child(ParentDatabaseName).child(l_phno).getValue(Users.class);

                    if (userData.getPhone().equals(l_phno))
                    {
                        if (userData.getPassword().equals(l_password))
                        {

                            if (ParentDatabaseName.equals("Admin"))
                            {
                                Toast.makeText(Login.this, "Logged in ", Toast.LENGTH_SHORT).show();

                                Intent intent = new Intent (  Login.this, Main2Activity.class);
                                startActivity(intent);
                            }
                            else if (ParentDatabaseName.equals("Users"))
                            {
                                Toast.makeText(Login.this, "Logged in ", Toast.LENGTH_SHORT).show();

                                Intent intent = new Intent (  Login.this, Main2Activity.class);
                                startActivity(intent);
                            }
                        }
                        else
                        {
                            Toast.makeText(Login.this, "Password is incorect ", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
                else
                {
                    Toast.makeText(Login.this, "Create Account With This" + l_phno + "Number", Toast.LENGTH_SHORT).show();
                }
            }

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

            }
        });

    }
}


Eror

    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.myapplication, PID: 23908
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
            at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
            at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
            at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:201)
            at android.app.ActivityThread.main(ActivityThread.java:6810)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
            at androidx.navigation.ui.ActionBarOnDestinationChangedListener.setTitle(ActionBarOnDestinationChangedListener.java:48)
            at androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener.onDestinationChanged(AbstractAppBarOnDestinationChangedListener.java:100)
            at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:218)
            at androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.java:220)
            at com.example.myapplication.Main2Activity.onCreate(Main2Activity.java:26)
            at android.app.Activity.performCreate(Activity.java:7224)
            at android.app.Activity.performCreate(Activity.java:7213)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081) 
            at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
            at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
            at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) 
            at android.os.Handler.dispatchMessage(Handler.java:106) 
            at android.os.Looper.loop(Looper.java:201) 
            at android.app.ActivityThread.main(ActivityThread.java:6810) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 




0 个答案:

没有答案