Android TextView bo'sh qoladi

MenuText ustida setText() ni ishga tushirishda nullpointerni olishni davom ettiraman.

mening onCreate:

setContentView(R.layout.activity_main);
TextView menuText = (TextView) findViewById(R.id.personals);

activity_main:



        

        

    

    

sarlavha:


Bu muammosiz ishlamoqda, istisno quyidagi hollarda tashlanadi:

menuText.setText("text goes here");

Istisno:

Caused by: java.lang.NullPointerIstisno: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

Shuni aytib o'tishim kerakki, yaxshi ish bo'ldi, men o'zgardi: com.android.support:appcompat-v7:25.1.0 dan 22 dan 25 gacha qo'shilgan va qo'shilgan com.karumi: dexter: 3.0.2 , ruxsatlarni berdi va endi bu masalani olishdi.

Nima qilsam, menuText null qoladi, hatto naychali bilan in'ektsiya o'tkazishga harakat qilaman.

@InjectView(R.id.personals)
    TextView menuText;

Hali ham ishlamaydi. har qanday taklif?

1
setText deb qidirasiz?
qo'shib qo'ydi muallif Vyacheslav, manba
findViewById (R.id.personals) bo'sh qoldirilishi mumkin.
qo'shib qo'ydi muallif Achintya Jha, manba
TextView tartibi activity_main yoki boshqa har qanday tartibda e'lon qilindi
qo'shib qo'ydi muallif Srinivas Keerthiprakasam, manba
id "personals" bilan matnli ko'rishni activity_main ichida bo'lishiga ishonch hosil qiling
qo'shib qo'ydi muallif Ganesh Katikar, manba

6 javoblar

Navigatsiya chizig'ining sarlavhasida vidjetni muvaffaqiyatli tekshirish uchun, uni oldin getHeaderView() deb chaqirib, to'g'ri konteyner ko'rinishini olishingiz kerak:

View header = ((NavigationView)findViewById(R.id.navigation_view)).getHeaderView(0);

va keyin header o'ngga ishora qilayotgan ViewGroup odatdagidek maqsadli vidjet uchun findViewById() ni chaqirishi mumkin:

TextView iv = ((TextView) header.findViewById(R.id.personals));

Bundan tashqari, siz o'zingiz qidirayotgan bo'lsangiz, @InjectView dan foydalanganingiz nima?

1
qo'shib qo'ydi
Rahmat, bu menga yordam berdi va foydali bo'ldi, men nima uchun nima qilayotganimni bilishimdan oldin ishlayotganini tushunishim kerak.
qo'shib qo'ydi muallif user1794974, manba
 NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);        
 View header = navigationView.getHeaderView(0);
 TextView username = (TextView) header.findViewById(R.id.personals);
1
qo'shib qo'ydi
NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
View header=navigationView.getHeaderView(0);

TextView tv= (TextView)header.findViewById(R.id. personals);
tv.setText("text goes here");
0
qo'shib qo'ydi
NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
View headerView = vNavigation.getHeaderView(0);
TextView text = headerView.findViewById(R.id.personals);
text.setText("It is working now");

Sinab ko'rsating, u albatta ishlaydi

0
qo'shib qo'ydi
agar sizga yordam bergan bo'lsa, bu javobni qabul qiling.
qo'shib qo'ydi muallif Nitesh Mishra, manba

Iltimos, ushbu kodni kiriting

 NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header=navigationView.getHeaderView(0);

menuText = (TextView)header.findViewById(R.id. personals);
menuText.setText("text goes here");
0
qo'shib qo'ydi
NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);
View head = navigationView.getHeaderView(0);
TextView username = (TextView) head.findViewById(R.id.personals);

Bu ish kerak.

0
qo'shib qo'ydi