Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Getting null pointer exception changing text in fragment programmatically

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there,I'm new to Android ,i Want to change the Text in a fragment programatically .My problem is that I'm getting an error like this- Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference

I'm sending data from this Activity


to this Tab_Activity



and again I'm passing this data to a Fragment,

TipsFragment



Please any one help me clear this error.



 
Bartender
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first things that jumps out is that in line 30/31 of Tab_Activity you're creating a fragment which is then not used - so it will be be garbage-collected quickly. How are you actually using the fragment?
 
dev goku
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using this fragment in a Viewpager contained in a Tab Layout
 
dev goku
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First I'm going to send a child position of an ExpandablelistView from an Activity to TabActivity,After setting up its Layout the TabActivity(Tablayout with 2 tabs and 2 fragmnets inside viewpager) gonna send the data it got to TipsFragment and it will populate the textview according to the childposition it got and actually i have 2 fragments i posted only one,please forgive me if I didn't give the right info you needed.
 
Tim Moores
Bartender
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm using this fragment in a Viewpager contained in a Tab Layout


OK, so there's more code in Tab_Activity which you didn't post? Well, it's hard to tell what's going on without seeing the relevant code bits.

The first thing to try is to call getArguments in the onCreate method rather than the onCreateView method. But that only makes sense if  the fragment is actually being used (as opposed to just being created), and the code you posted doesn't show that part.
 
dev goku
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the full code on TabActivity,



and Thanks for Replying..
 
Tim Moores
Bartender
Posts: 7488
171
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's as I suspected: the fragment to which you add the parameter in lines 45 to 48 is not used. Instead, you create another instance of the fragments in lines 61 and 62 to which you're not adding any parameters. Those are the ones that actually get used in the ViewPager.
 
Tim Moores
Bartender
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, if you have exactly two views (or another small, fixed number of views) between which you want to switch, it would probably be easier to use a ViewFlipper rather than a ViewPager. No need to deal with fragments or adapters that way.
 
dev goku
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:It's as I suspected: the fragment to which you add the parameter in lines 45 to 48 is not used. Instead, you create another instance of the fragments in lines 61 and 62 to which you're not adding any parameters. Those are the ones that actually get used in the ViewPager.



How do i do that ?
 
dev goku
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:It's as I suspected: the fragment to which you add the parameter in lines 45 to 48 is not used. Instead, you create another instance of the fragments in lines 61 and 62 to which you're not adding any parameters. Those are the ones that actually get used in the ViewPager.



Ah.,It Worked Man as You said,I used the already created Fragment instance now.,It worked great.Thank you for your time..
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic