This question already has answers here:
NavigationView get/find header layout
(9 answers)
Closed 5 years ago.
I'm working with firebase auth authentication and with the navigation drawer layout, what I need is to set the user email on the header of my navigation, the thing is as I got, is that it can't find the related textView on my layout, because the main activity is associated with my main layout and not the navigation itself, I did a test to see if the user retrieved from the database, and I got the email, but when I do this:
auth = FirebaseAuth.getInstance();
String email = auth.getCurrentUser().getEmail();
Log.d("email",email);
if (auth.getCurrentUser() != null) {
userTxt.setText(email);
}
I get a NullPointerException, that I think is related to the userTxt, so if anyone experienced it before can you guys give me a tip about how to solve this?
StackTrace
FATAL EXCEPTION: main
Process: com.esmad.pdm.friendlymanager, PID: 31473
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.esmad.pdm.friendlymanager/com.esmad.pdm.friendlymanager.MainActivity}: 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:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
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.esmad.pdm.friendlymanager.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
From the Exception it is clear that your userTxt is null.
To access NavigationView Header -- you need to first get the Header view:
From Documentation
View getHeaderView (int index) Gets the header view at the specified
position.
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
View header = navigationView.getHeaderView(0) ;
TextView userTxt = (TextView) header.findViewById(R.id.textView); //your ID
userTxt.setText(email);
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
The application crashes when I call EDITTEXT.
layout
<EditText
android:id="#+id/T_Price"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/B_LessMoney"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:text="#string/Price"
android:visibility="visible" />
Main
EditText mPrice = (EditText) findViewById(R.id.T_Price);
and this the report
E/AndroidRuntime: FATAL EXCEPTION: main
Process: budgetreport.com.budgetreport, PID: 2791
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{budgetreport.com.budgetreport/budgetreport.com.budgetreport.Report}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2488)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5765)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:54)
at android.support.v7.app.AppCompatDelegateImplV23.(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:200)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at budgetreport.com.budgetreport.Report.(Report.java:21)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1072)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2478)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5765)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
I/Process: Sending signal. PID: 2791 SIG: 9
Application terminated.
You are calling findViewById before Activity's onCreate method (in the constructor?).
What you should do is to initialize it in the Activity's onCreate:
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
mPrice = (EditText) findViewById(R.id.T_Price);
}
i have developped an application android that contains two activities , the first activity contains two Edittext and a login button , i have to enter the value 'abcd' as a username and 'EFGH' as a password and validate with the button , the probleme is when i clicked login the application has crashed ,
the error message is : unfortunately ___ is stopped
logcat: errors 11-15 19:22:56.237 783-783/? E/Zygote: accessInfo : 0
11-15 19:24:41.057 783-783/info.adem.tplogin E/AndroidRuntime: FATAL
EXCEPTION: main
Process: info.adem.tplogin, PID: 783
java.lang.RuntimeException: Unable to start activity
ComponentInfo{info.adem.tplogin/info.adem.tplogin.FriendActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'void
android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
at info.adem.tplogin.FriendActivity.onCreate(FriendActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object
Before you use a reference from your button to add an event, you need to retrieve the reference from it.
Like this:
public class YourActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_layout)
Button button = (Button)findViewById(R.id.your_button_id)
//button.setOnClickListener........
}
}
This question already has an answer here:
How to set text to view from drawer header layout in navigation drawer without inflating view
(1 answer)
Closed 4 years ago.
i tried running an app in android studio but i keep getting this weird error.
I hardly could understand what this error is all about and how to fix this, your help would be much appreciated.
here is the logcat
06-16 15:26:02.141 5270-5270/com.sensiblewallet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sensiblewallet, PID: 5270
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sensiblewallet/com.sensiblewallet.ActivityMainWallet}: 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:2452)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5481)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
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.sensiblewallet.ActivityMainWallet.onCreate(ActivityMainWallet.java:212)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5481)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
here is the piece of code it is indicating to.
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.END);
mDrawerLay = (NavigationView)findViewById(R.id.navigation_view);
mDrawerLay.setNavigationItemSelectedListener(this);
TextView navigationTotalCreditTextView = (TextView)mDrawerLay.findViewById(R.id.nav_drawer_total_credits);
navigationTotalCreditTextView.setText("Total Credits: " + PreferenceConnector.readInteger(aiContext,
PreferenceConnector.WALLETPOINTS, 0));
TextView navigationWalletIDText = (TextView)mDrawerLay.findViewById(R.id.nav_drawer_wallet_id);
navigationWalletIDText.setText("Wallet ID: " + PreferenceConnector.readString(aiContext,
PreferenceConnector.WALLETID, ""));
TextView navigationUsernameText = (TextView)mDrawerLay.findViewById(R.id.nav_drawer_username);
navigationUsernameText.setText(PreferenceConnector.readString(aiContext,
PreferenceConnector.USERNAME, ""));
// MyUtils.setFontAllView((RelativeLayout)findViewById(R.id.laymenu));
Try this code :
View v = mDrawerLay.getHeaderView(0);
TextView navigationTotalCreditTextView = (TextView)v.findViewById(R.id.nav_drawer_total_credits);
TextView navigationWalletIDText = (TextView)v.findViewById(R.id.nav_drawer_wallet_id);
navigationWalletIDText.setText("Wallet ID: " + PreferenceConnector.readString(aiContext,
PreferenceConnector.WALLETID, ""));
TextView navigationUsernameText = (TextView)v .findViewById(R.id.nav_drawer_username);
navigationUsernameText.setText(PreferenceConnector.readString(aiContext,
PreferenceConnector.USERNAME, ""));
I am creating a restaurant app that requires an expandable listview to show a menu for the waiter to take a customer's menu. The app is crashing and I think it has something to do with the code below:
public class Menu extends AppCompatActivity {
HashMap < String, List < String >> Food_menu;
List < String > Food_list;
ExpandableListView Exp_list;
FoodMenuAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
Food_menu = DataProvider.getInfo();
Food_list = new ArrayList < String > (Food_menu.keySet());
adapter = new FoodMenuAdapter(this, Food_menu, Food_list);
Exp_list.setAdapter(adapter);
}
}
When I debugged the app, it said that:
Exp_list
Food_menu
Food_list
were all coming back as null. I'm new to android studios and I'm not sure how to remedy the problem. Any help would be greatly appreciated, thanks.
Logcat:
04-11 22:05:53.841 1960-1960/com.example.ben.restaurantapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ben.restaurantapp, PID: 1960
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ben.restaurantapp/com.example.ben.restaurantapp.Menu}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
at com.example.ben.restaurantapp.Menu.onCreate(Menu.java:38)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
I think the second parameter of your FoodMenuAdapter has to be a resource layout file.
For example:
Food Food_menu = DataProvider.getInfo();
adapter = new FoodMenuAdapter(this, R.layout.your_xml_layout_here, Food_list);
Exp_list = (ExapandableListView) findViewById(R.id.exp_list);
Exp_list.setAdapter(adapter);
Where your_xml_layout_here is the layout that all the information will be displayed on. Keep in mind that an adapter is the middleman of information and what you see on your application screen.
From your logcat you can see the exact problem:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
which means that at the point you try to use setAdapter method on the Exp_List, the said variable is null.
What you can do from here on is to check if your layout activity_main actually contains an ExpandableListView with an id of exp_list (maybe post your layout?).
Your question should not be android studio null error as the null has nothing to do with Android Studio - it comes from the programming language, in this specific case - Java.
You can learn how to fix NullPointerExceptions here.
i am unable to debug this problem.i.e.when i am trying to change text name and put an icon for action bar its not supporting..i mean it is throwing an error like
02-06 11:43:39.964 9510-9510/com.example.brahmaiah.app1_menu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.brahmaiah.app1_menu, PID: 9510
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brahmaiah.app1_menu/com.example.brahmaiah.app1_menu.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setIcon(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setIcon(int)' on a null object reference
at com.example.brahmaiah.app1_menu.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
my MainActivity.java
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
android.support.v7.app.ActionBar actionBar=getSupportActionBar();
actionBar.setIcon(R.drawable.ic_image3);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
and i had support of v 7 and v 4 libraries.i am also tried to get execution like this in my activityi.e.actionBar.getSupportActionBar();.
kindly help me in that to get out of it debug.
i think no need of manifest.xml..because i had tag correctly with minversion:7 and maxVersion17..thanks in advance!!
and app theme also like...light.noActionBar
You need to set Toolbar as actionbar first then use getSupportActionBar()
You need to change the below code
android.support.v7.app.ActionBar actionBar=getSupportActionBar();
actionBar.setIcon(R.drawable.ic_image3);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
to
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
android.support.v7.app.ActionBar actionBar=getSupportActionBar();
actionBar.setIcon(R.drawable.ic_image3);
use AppCompatActivity
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
TRY this code :
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.back_dark);
actionBar.setTitle(getResources().getString(R.string.title));