null pointer buttonlistener

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

null pointer buttonlistener

Postby sommeralex » Wed Jan 23, 2008 1:11 pm

Hello.. i am really down.. what do i do wrong concerning the button listener? i always get null-poiner exception..

my main class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. package com.airwriting.android;
  5.  
  6.  
  7.  
  8. import com.airwriting.android.gui.AirView;
  9.  
  10.  
  11.  
  12. import android.app.Activity;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.view.View;
  17.  
  18. import android.view.View.OnClickListener;
  19.  
  20.  
  21.  
  22.  
  23.  
  24. public class AirMain extends Activity {
  25.  
  26.     /** Called when the activity is first created. */
  27.  
  28.  
  29.  
  30.         public AirView airView;
  31.  
  32.          
  33.  
  34.     public void onCreate(Bundle icicle) {
  35.  
  36.         super.onCreate(icicle);
  37.  
  38.         //setContentView(R.layout.main);
  39.  
  40.  
  41.  
  42.        
  43.  
  44.         airView = new AirView(this);    
  45.  
  46.         airView.setLoginView();
  47.  
  48.        
  49.  
  50.                
  51.  
  52.     }
  53.  
  54.  
  55.  
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


and now the gui-class

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. package com.airwriting.android.gui;
  5.  
  6.  
  7.  
  8. import com.airwriting.android.AirMain;
  9.  
  10. import com.airwriting.android.R;
  11.  
  12. import com.airwriting.android.gui.listeners.AirListeners;
  13.  
  14.  
  15.  
  16. import android.view.View;
  17.  
  18. import android.view.ViewGroup;
  19.  
  20. import android.view.View.OnClickListener;
  21.  
  22.  
  23.  
  24. import android.widget.Button;
  25.  
  26. import android.widget.EditText;
  27.  
  28. import android.widget.LinearLayout;
  29.  
  30. import android.widget.TextView;
  31.  
  32.  
  33.  
  34. public class AirView {
  35.  
  36.  
  37.  
  38.         public AirMain airMain;
  39.  
  40.        
  41.  
  42.         public Button login;
  43.  
  44.         public TextView labelMail;
  45.  
  46.         public TextView labelPass;
  47.  
  48.        
  49.  
  50.         public EditText entryMail;
  51.  
  52.         public EditText entryPass;
  53.  
  54.        
  55.  
  56.        
  57.  
  58.         public AirView(AirMain airMain){               
  59.  
  60.                
  61.  
  62.                 this.airMain = airMain;
  63.  
  64.                
  65.  
  66.                 login = (Button) airMain.findViewById(R.id.login);    
  67.  
  68.                
  69.  
  70.                
  71.  
  72.                 labelPass = (TextView) airMain.findViewById(R.id.labelPass);
  73.  
  74.                 labelMail = (TextView) airMain.findViewById(R.id.labelMail);           
  75.  
  76.                
  77.  
  78.                 entryMail = (EditText) airMain.findViewById(R.id.entryMail);
  79.  
  80.                 entryPass = (EditText) airMain.findViewById(R.id.entryPass);
  81.  
  82.  
  83.  
  84.                 login.setOnClickListener(loginListener);
  85.  
  86.                                
  87.  
  88.         }
  89.  
  90.        
  91.  
  92.        
  93.  
  94.         public void setLoginView(){
  95.  
  96.                
  97.  
  98.                
  99.  
  100.             airMain.setContentView(R.layout.login);        
  101.  
  102.        
  103.  
  104.        
  105.  
  106.         }
  107.  
  108.  
  109.  
  110.         OnClickListener loginListener = new OnClickListener() {
  111.  
  112.                 public void onClick(View v) {
  113.  
  114.                        
  115.  
  116.                        
  117.  
  118.                 //airMain.airView.labelMail.setText("hallo");
  119.  
  120.                         System.out.println("Hello");
  121.  
  122.  
  123.  
  124.                        
  125.  
  126.                        
  127.  
  128.                 }
  129.  
  130.         };
  131.  
  132.  
  133.  
  134.  
  135.  
  136.        
  137.  
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Top

Postby plusminus » Wed Jan 23, 2008 5:09 pm

Hello sommeralex,

calls to findViewById(...); search for Views inflated from the xml-file you passed to setContentView() in the onCreate() of the Main-Activity.
If you did not do it, will simply return null and you cannot set anything to a null-Object.

Hope I could help you. Feel free to ask further. :)

Regards,
plusminus
Last edited by plusminus on Wed Jan 23, 2008 5:44 pm, edited 1 time in total.
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

thx

Postby sommeralex » Wed Jan 23, 2008 5:15 pm

dont bother, i will ask a hole ;-)
sommeralex
Experienced Developer
Experienced Developer
 
Posts: 95
Joined: Sun Jan 20, 2008 3:12 am
Location: Vienna

Postby snowtiger » Wed Jan 23, 2008 5:50 pm

Well and you won't see a simple "sysout" :-) Android will do nothing. Try Log.i("title", "message") and you can/could maybe :-) see at least something happening in LogCat.
snowtiger
Developer
Developer
 
Posts: 34
Joined: Fri Dec 21, 2007 10:44 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 10 guests