Navigation drawer errors [duplicate] - android

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, ""));

Related

Calling onSaveInstanceState on Activity

I inherited a project, trying to understand and solve an old bug.
When I start an activity and let it sit for a bit (sometimes it's almost immediate), but it appears to go into a pause state or something and attempts to reload the activity and gets an error.
"Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String ... ' on a null object reference"
I found that when I add this to my activity:
#Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
//super.onSaveInstanceState(savedInstanceState);
}
It appears to work perfectly.
However, when I uncomment the line above to super.onSaveInstanceState(savedInstanceState);, I get the error again.
My activity is extending the ActionBarActivity, if that is helpful.
Any help would be appreciated. Thank you!
Related Code:
#Override
protected void prepareLayout() {
textMessage = (TextView) findViewById(R.id.textMessage);
textNotes = (EditText) findViewById(R.id.textNotes);
textMessage.setText(getOneOfMyItems().getText());
// ... the above line is where the problem error is thrown.
}
Stack Trace:
Process: com.myproj.myproj, PID: 9272
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproj.myproj/com.myproj.activities.MyActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.myproj.data.MyItems.getText()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3384)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5433)
at android.app.ActivityThread.access$1200(ActivityThread.java:226)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1810)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7270)
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 'java.lang.String com.myproj.data.MyItems.getText()' on a null object reference
at com.myproj.xyz.MyFragment.prepareLayout(MyFragment.java:44)
at com.myproj.fragments.BaseFragment.prepareAndSetView(BaseFragment.java:49)
at at com.myproj.xyz.BaseMyFragment.onCreateView(BaseMyFragment.java:21)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2143)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2089)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1129)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2143)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:615)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1266)
at android.app.Activity.performStart(Activity.java:6943)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3384) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5433) 
at android.app.ActivityThread.access$1200(ActivityThread.java:226) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1810) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7270) 
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) 

android app crash when i call EditText [duplicate]

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

SetText on navigation view header [duplicate]

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

Limit error in Firebase chatting in Android?

Some time ther is not error its run without error but most of time got error in this code, I try many thing for this. I try call this code on onStart() and I try this on onActivityCreted() too. but i get same error. This is my coding for show previous chat. I got error in limit().
final ListView listView = getListView();
mChatListAdapter = new FirebaseListAdapter<>(mFirebaseRef.limit(50), Chat.class, R.layout.chat_item, R.layout.chat_item_other,
R.layout.third_layout_chat, R.layout.fourth_layout_chat, getActivity(), chat_id);
listView.setAdapter(mChatListAdapter);
mChatListAdapter.registerDataSetObserver(new DataSetObserver() {
#Override
public void onChanged() {
super.onChanged();
listView.setSelection(mChatListAdapter.getCount() - 1);
}
});
This is error.
FATAL EXCEPTION: main Process: com.socialapp.android, PID: 8112 Theme: themes:{} java.lang.RuntimeException: Unable to start activity ComponentInfo{com.socialapp.android/com.socialapp.chat.ChatViewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.firebase.client.Query com.firebase.client.Firebase.limit(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.firebase.client.Query com.firebase.client.Firebase.limit(int)' on a null object reference
at com.socialapp.chat.ChatViewFragment.onStart(ChatViewFragment.java:609)
at android.app.Fragment.performStart(Fragment.java:2244)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1002)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1130)
at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1958)
at android.app.FragmentController.dispatchStart(FragmentController.java:163)
at android.app.Activity.performStart(Activity.java:6274)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5466) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Android app null error

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.

Categories

Resources