应用程序在模拟器和实际设备中启动新活动时崩溃

时间:2017-09-18 17:49:00

标签: android firebase firebase-realtime-database

当我开始新活动但应用程序因以下错误而崩溃时。

FATAL EXCEPTION: main
                                                                          Process: com.food.sheenishere.stark, PID: 9976
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.food.sheenishere.stark/com.food.sheenishere.stark.home}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                              at com.food.sheenishere.stark.home.onCreate(home.java:84)
                                                                              at android.app.Activity.performCreate(Activity.java:6679)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:154) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                              at java.lang.reflect.Method.invoke(Native Method)

home.java

这里下面有错误的地方。这是家庭活动代码的一部分。登录后,当我尝试打开此活动,但应用程序崩溃时显示上面显示的错误。它工作得很早,但突然它在虚拟设备和真实设备上崩溃。

public class home extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    FirebaseDatabase database;
    DatabaseReference category;
    TextView txtFullName;


    RecyclerView recycler_menu;
    RecyclerView.LayoutManager layoutManager;
    private ProgressDialog progressDialog;
    FirebaseRecyclerAdapter<Category,MenuViewHolder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Menu");
        setSupportActionBar(toolbar);
        progressDialog=new ProgressDialog(this);
        progressDialog.setMessage("Loading...");
        progressDialog.show();


        //init firebase
        database=FirebaseDatabase.getInstance();
        category=database.getReference("Category");




        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Intent cartIntent = new Intent(home.this, Cart.class);
                startActivity(cartIntent);
            }
        });

        DrawerLayout 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.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        //set name for user
        View headerView = navigationView.getHeaderView(0);
        txtFullName = (TextView)findViewById(R.id.txtFullName);
        txtFullName.setText(Common.currentUser.getName());

2 个答案:

答案 0 :(得分:3)

请尝试以下代码。

txtFullName = (TextView)findViewById(R.id.txtFullName); 

而不是下面这行使用

 txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
 txtFullName.setText(Common.currentUser.getName());

答案 1 :(得分:1)

请尝试以下代码。

txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);