• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Nullpointer when updating simple TextView

  • Thread starter Thread starter hirden
  • Start date Start date
H

hirden

Guest
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:
public class Realtime extends Activity{
    
    private BigInteger data = BigInteger.valueOf(0);
    private Handler handler = new Handler();
    
    private Runnable updateTask = new Runnable(){

        @Override
        public void run() {
            updateData();
            handler.postDelayed(this, 1000);
        }        
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
        
        updateData();
        
        handler.removeCallbacks(updateTask);
        handler.postDelayed(updateTask, 1000);    
    }
    
    private void updateData(){
        
        final TextView textview = (TextView)findViewById(R.id.blips); //This is where I get my nullpointer
        textview.setText(this.data.toString());
        
    }
    
    public void updateView(BigInteger data){
        this.data = data;
        handler.postDelayed(updateTask, 1000);
        
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        handler.removeCallbacks(updateTask);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        handler.removeCallbacks(updateTask);
        handler.postDelayed(updateTask, 1000);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if ( handler != null )
            handler.removeCallbacks(updateTask);
        handler = null;        
    }
}
 
Back
Top Bottom