代码之家  ›  专栏  ›  技术社区  ›  Wahdat Stark Kashmiri

在emulator和真实设备中启动新活动时,应用程序崩溃

  •  -1
  • Wahdat Stark Kashmiri  · 技术社区  · 7 年前

    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)
    

    家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 回复  |  直到 7 年前
        1
  •  3
  •   gautam    7 年前

    尝试以下代码。

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

    不要使用下面的这行

     txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
     txtFullName.setText(Common.currentUser.getName());
    
        2
  •  1
  •   Wahdat Jan    7 年前

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