在Home.onCreate

时间:2019-06-28 09:58:33

标签: java android

这是我在Logcat中单击“登录”按钮时收到的错误:

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.virtual.ai.Home.onCreate(Home.java:92)
    at android.app.Activity.performCreate(Activity.java:7149)
    at android.app.Activity.performCreate(Activity.java:7140)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3017)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3172) 
    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:1906) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6863) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

2019-06-28 12:59:05.812 23283-23283 / com.virtual.ai W / OPDiagnose:getService:OPDiagnoseService NULL 2019-06-28 12:59:05.815 23283-23506 / com.virtual.ai D / OSTracker:OS事件:崩溃 2019-06-28 12:59:05.828 23283-23506 / com.virtual.ai D / AbstractTracker:活动成功

这是我在家中使用的代码

FloatingActionButton fab = 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.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView =(NavigationView)findViewById(R.id.nav_view);

    navigationView.setNavigationItemSelectedListener(this);
    View headerView = navigationView.getHeaderView(0);
    txtFullName = (TextView)findViewById(R.id.txtFullName);
    txtFullName.setText(Common.currentUser.getNa());
    recycler_menu = (RecyclerView)findViewById(R.id.recycler_menu);
    recycler_menu.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recycler_menu.setLayoutManager(layoutManager);

    LoadMenu();

该错误似乎是指txtFullName.setText(Common.currentUser.getNa());第18行,不确定出了什么问题。

任何人,任何解决方案。当我单击上方的Logcat时,无法执行登录活动。

3 个答案:

答案 0 :(得分:1)

尝试

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

答案 1 :(得分:0)

错误与

有关
txtFullName = (TextView)findViewById(R.id.txtFullName);
txtFullName.setText(Common.currentUser.getNa());

txtFullName为空且未初始化。最可能的原因是您在活动中在TextView中使用的xml布局中没有ID为 txtFullName setContentLayout()

答案 2 :(得分:0)

NavigationDrawer正在获取空对象引用,因为未正确引用它。 navigationDrawer带有标题属性,可帮助它导航并获取抽屉特定字段的ID。 如下所示对您的代码进行更改:

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