1. Check out our companion app, Forums for Android! Download from Google Play

Nullpointer when updating simple TextView

Discussion in 'Application Development' started by hirden, Sep 28, 2010.

  1. hirden

    hirden Guest
    Thread Starter

    Hi, I am writing my first android GUI, it consists of a TabLayout with 3 tabs. In one of these I want to update the view from a back-end thread that fires events containing a number that I want to write to the screen. Everything works fine until I actually get the call on the updateView method sent to the UI from my observer, then the App crashes from a nullpointer in my getViewById() call :S

    This is the activity for the tab that I want to update the view in. It initiates fine and sets the textview to display the initial value of 0, but when the event arrives it crashes and I get a nullpointer at the comment.

    I can get my TabActivity to update its view, but I cant get the separate Activity to update its own tab.

    Any thoughts?

    Code (Text):
    1.  
    2. public class Realtime extends Activity{
    3.    
    4.     private BigInteger data = BigInteger.valueOf(0);
    5.     private Handler handler = new Handler();
    6.    
    7.     private Runnable updateTask = new Runnable(){
    8.  
    9.         @Override
    10.         public void run() {
    11.             updateData();
    12.             handler.postDelayed(this, 1000);
    13.         }        
    14.     };
    15.  
    16.     public void onCreate(Bundle savedInstanceState) {
    17.         super.onCreate(savedInstanceState);
    18.         this.setContentView(R.layout.main);
    19.        
    20.         updateData();
    21.        
    22.         handler.removeCallbacks(updateTask);
    23.         handler.postDelayed(updateTask, 1000);    
    24.     }
    25.    
    26.     private void updateData(){
    27.        
    28.         final TextView textview = (TextView)findViewById(R.id.blips); //This is where I get my nullpointer
    29.         textview.setText(this.data.toString());
    30.        
    31.     }
    32.    
    33.     public void updateView(BigInteger data){
    34.         this.data = data;
    35.         handler.postDelayed(updateTask, 1000);
    36.        
    37.     }
    38.    
    39.     @Override
    40.     protected void onStop() {
    41.         super.onStop();
    42.         handler.removeCallbacks(updateTask);
    43.     }
    44.    
    45.     @Override
    46.     protected void onResume() {
    47.         super.onResume();
    48.         handler.removeCallbacks(updateTask);
    49.         handler.postDelayed(updateTask, 1000);
    50.     }
    51.    
    52.     @Override
    53.     protected void onDestroy() {
    54.         super.onDestroy();
    55.         if ( handler != null )
    56.             handler.removeCallbacks(updateTask);
    57.         handler = null;        
    58.     }
    59. }
    60.  
     

    Advertisement

Share This Page

Loading...