android


Change TextView when Navigation Drawer Item is selected


I'm trying to make three different options in my navigation drawer use the same fragment but with different values in the TextView in that fragment.
I created a standard Navigation Drawer Activity with Android Studio and tried to use setText in the same method that changes the title, see below:
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
Namn.setText("Item 1");
break;
case 2:
mTitle = getString(R.string.title_section2);
Namn.setText("Item 2");
break;
case 3:
mTitle = getString(R.string.title_section3);
Namn.setText("Item 3");
break;
}
}
When I try to run the app it force closes instantly and I get this error:
Process: com.test.je.test, PID: 22605
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.je.test/com.test.je.test.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
at android.app.ActivityThread.access$800(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5034)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.test.je.test.MainActivity.onSectionAttached(MainActivity.java:68)
at com.test.je.test.MainActivity$PlaceholderFragment.onAttach(MainActivity.java:153)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5251)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2160)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
            at android.app.ActivityThread.access$800(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5034)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
            at dalvik.system.NativeStart.main(Native Method)
I have declared the TextView with TextView Namn; and Namn = (TextView)findViewById(R.id.twnamn);
What am I doing wrong here?
So user #feresr helped me understand what was wrong with his comment.
I created an if statement in the onCreateView() method instead and it solved the problem.
I had to declare the textview after the fragment had been created also.
Here is the code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
Bundle args = getArguments();
int currentView = getArguments().getInt(ARG_SECTION_NUMBER);
if(currentView == 1){
rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView Namn = (TextView) rootView.findViewById(R.id.twnamn);
Namn.setText("Val 1");
}else if(currentView == 2){
rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView Namn = (TextView) rootView.findViewById(R.id.twnamn);
Namn.setText("Val 2");
}else if(currentView == 3){
rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView Namn = (TextView) rootView.findViewById(R.id.twnamn);
Namn.setText("Val 3");
}else {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
}
return rootView;
}

Related Links

Android command is depreciated after updating SDK
The resolution doesn't fit the screen
Camera2 API Real-Time OCR
Android ListView generated from SQLite database not displaying on phone screen (but displaying on emulator)
How to filter (not search) Listview BaseAdapter
The Activity restart after saving data in Firebase
How do I use ConstraintSet in Android to stack TextViews horizontally?
Turbolinks Android Adaptor - back button where to start
How to fix Android ListView Rendering Error where the checkboxes of the list items disappear?
SharedPreferences on first run are not saved when pressing Back Button
getfeatures of kml layer
How to add Firebase ChildEventlistener in Broadcast receiver
Running ex. Facebook app INSIDE my app frame
GPS over Bluetooth - Android
Migration and Realm database version
Load link in WebView Android

Categories

HOME
sonarqube
gridview
turing-machines
developer-tools
jmeter
3d
flex
numbers
shiro
knitr
lanczos
vimdiff
mautic
line
angular-dart
naivebayes
pybossa
shared-libraries
http-post
entitymanager
searchview
powershell-v5.0
easyphp
digital-signature
breadth-first-search
named-entity-recognition
expressionengine3
maze
syntax-highlighting
jasmine-node
has-and-belongs-to-many
iis-express
google-earth
linkedhashmap
android-gridview
jwplayer6
browser-extension
levenshtein-distance
tcc
xmldocument
apache-directory
snapchat
puredata
mesosphere
xbmc
proget
corruption
azure-cli
yii2-user
change-tracking
gets
simplepie
pytables
dimension
project-organization
ellucian-scribe
beanstalk
jparsec
concept-insights
atan2
boomerang
r-package
google-books
android-launcher
extjs-grid
text-search
branch-and-bound
fail2ban
rrule
adehabitathr
spy++
linkedin-jsapi
nsmenu
ray
bin
hibernate-entitymanager
cnf
joomla-template
font-awesome-4.0.0
windows-98
cascadingdropdown
device-width
qtconcurrent
wiimote
expression-blend-4
update-statement
ie-compatibility-mode
message-passing
scrollbars
build-environment
complex-event-processing
contentpresenter
pbcopy
blind
pnrp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App