Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

changing dialog text alignment

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone. i've made a public static method in a separate class to show a dialog box. i want to customize the dialogs text but i cant select the dialogs textview from the static public method and encounter a null pointer exception.is there any solution? this is what ive done so far:



and i call this method from another activity using:
 
Rancher
Posts: 5009
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

encounter a null pointer exception


What line does it happen on and what variable has the null value?
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

encounter a null pointer exception


What line does it happen on and what variable has the null value?



The line after the on i have commented encounters error and the variable is messageText
 
Norm Radder
Rancher
Posts: 5009
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at SO: http://stackoverflow.com/questions/3264610/findviewbyid-returns-null
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Take a look at SO: http://stackoverflow.com/questions/3264610/findviewbyid-returns-null




i didn't find my answer
 
Norm Radder
Rancher
Posts: 5009
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try what was suggested?
Is the View you are trying to access being shown?
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Did you try what was suggested?
Is the View you are trying to access being shown?



do you think i am allowed to define TextView messeagText after the last line? :

       return builder.create();
 
Norm Radder
Rancher
Posts: 5009
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Statements after a return statement are not reachable.

Is the view with R.id.message being shown when you call  findViewById?
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Statements after a return statement are not reachable.

Is the view with R.id.message being shown when you call  findViewById?



Yes if i comment out the next line to avoid crash. but with the default appearance.
 
Norm Radder
Rancher
Posts: 5009
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If findViewById returns null, then the code must not be doing what you think.
How do you know the content is being shown when findViewById is called?
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:If findViewById returns null, then the code must not be doing what you think.
How do you know the content is being shown when findViewById is called?



because i see an alert dialog which contains text
 
Norm Radder
Rancher
Posts: 5009
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the content is being shown, I'd expect findViewById to work.  
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:If the content is being shown, I'd expect findViewById to work.  



the content would have shown anyway. what we want to do is center the text which we couldn't.
 
Norm Radder
Rancher
Posts: 5009
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try calling setContentView BEFORE calling findViewById?
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Did you try calling setContentView BEFORE calling findViewById?



i already mentioned the whole thing is inside a separate helper class and in the activity that is called it is the last thing to be called
 
Norm Radder
Rancher
Posts: 5009
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I did not see the code where setContentView is called or the xml for the layout.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hang on.
If the layout is incorrect then why not fix the layout, rather than trying to do this in code?
 
Mohsen Dehghani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Hang on.
If the layout is incorrect then why not fix the layout, rather than trying to do this in code?



because there is no xml layout. were dealing with alert dialog which generates layout whenever called.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But there can be, that's the point.
If you have a specific layout in mind, that is not the "out of the box" one, then you assign a view to it.

It's in the docs for dialogs.
Specifically this section on custom layouts.
 
Norm Radder
Rancher
Posts: 5009
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is there a view defined with the id of message?
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic