简体   繁体   中英

void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at Home.onCreate

This is the error which I received when I click on the "Sign In" button in my 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 Event: crash 2019-06-28 12:59:05.828 23283-23506/com.virtual.ai D/AbstractTracker: Event success

This is the Code that I use in my Home

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();

The Error seems referring to txtFullName.setText(Common.currentUser.getNa()); Line 18, not sure what went wrong.

Anyone, any solution. I am unable to perform the Sign In Activity when I click I get the above the Logcat.

尝试这个

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

The error is related to

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

txtFullName is null and not initialized. The most probable cause is that you don't have a TextView with id txtFullName in your xml layout that you used in setContentLayout() in your activity.

The NavigationDrawer is getting a null object reference because it is not referenced properly. The navigationDrawer has a header attribute along with it which helps it to navigate and get the id of the specific fields of the drawer. Make the changes to your code as shown below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference How to resolve Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference (RecyclerView) Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference Adapter.Messageadapter.onBindViewHolder Recycler view android studioAttempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference Java Error: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference TensorFlow Lite Android App crashes with NullPointerException 'void android.widget.TextView.setText(java.lang.CharSequence)' on null object reference Recycler View - Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference May be duplicated : Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference How can I fix 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM