[Solved] Button.setOnClickListener

Put your problem here if it does not fit any of the other categories.

[Solved] Button.setOnClickListener

Postby felix » Tue Feb 19, 2008 10:33 pm

I'm Trying do change an layout if the user press a button but on the method setOnClickListener raises an exception "java.lang.nullPointer exeception
meu codigo abaixo

Code: Select all

public class ChangeLayout extends Activity{
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
       
        btnNovo = (Button)findViewById(R.id.btnNovo);

        btnNovo.setOnClickListener( new Button.OnClickListener(){

         @Override
         public void onClick(View arg0) {
            
            setContentView(R.layout.start);
                       }
         }
}

}


what is wrong?

thanks for you attention
Last edited by felix on Wed Feb 20, 2008 11:44 am, edited 1 time in total.
felix
Junior Developer
Junior Developer
 
Posts: 18
Joined: Thu Feb 14, 2008 4:25 pm
Location: Brasil

Top

Postby Jesmiatka » Wed Feb 20, 2008 8:52 am

Hi,

I use the following for OnClickListeners on my buttons

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         Button btRemoveIcon = (Button) findViewById(R.id.remove_icon);
  2.  
  3.                 btRemoveIcon.setOnClickListener(new View.OnClickListener() {
  4.  
  5.                         public void onClick(View view) {
  6.  
  7.                                 //Code here
  8.  
  9.                         }
  10.  
  11.                 });
  12.  
  13.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Notice that I use the View.OnClickListener
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby plusminus » Wed Feb 20, 2008 10:02 am

Hello felix,

an essential information is where/when the error occurs.

I never tried it myself, but I found two hints in the sdk, that it maybe is only possible within onCreate():
When Android compiles your application, it compiles each file into an android.view.View resource that you can load in code by calling setContentView(R.layout.layout_file_name) in your Activity.onCreate() implementation.


onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.


BUT there at least exists a function that indicates that is maybe posbbile:
onContentChanged()
This hook is called whenever the content view of the screen changes (due to a call to setContentView().)


So let us know on which line the exception is thrown (directly when you start your app or just when clicking the button).

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby felix » Wed Feb 20, 2008 11:44 am

oh shit, i'm not using the tag "andoid:id" and my button is set with null, changing this atributte on the button it's work

:)

thanks for your help
felix
Junior Developer
Junior Developer
 
Posts: 18
Joined: Thu Feb 14, 2008 4:25 pm
Location: Brasil

Postby res » Wed Feb 20, 2008 5:03 pm

felix wrote:oh shit, i'm not using the tag "andoid:id" and my button is set with null, changing this atributte on the button it's work

:)

thanks for your help


yeah thats a pain with the new m5 SDK, after you change it once i'm sure you will remember.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Postby plusminus » Wed Feb 20, 2008 5:53 pm

res wrote:
felix wrote:oh shit, i'm not using the tag "andoid:id" and my button is set with null, changing this atributte on the button it's work

:)

thanks for your help


yeah thats a pain with the new m5 SDK, after you change it once i'm sure you will remember.


But I think it was a simplification. Definitely in AndroidManifest. Almost all tags are now "android:name", thats easier to learn.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests