Reputation: 43
I'm trying to update a textview with the value of a seekbar, and I'm having trouble setting an OnSeekBarChangeListener. I don't have any compile errors, but every time I try to run the app, I get a null pointer exception.
The error says:
Attempt to invoke virtual method 'void android.widget.SeekBar.setOnSeekBarChangeListener(android.widget.SeekBar$OnSeekBarChangeListener)' on a null object reference
I can't for the life of me figure out why, despite trawling through StackOverflow. Every similar problem, or tutorial on how to set an OnSeekBarChangeListener seems to suggest I do exactly what I've done. Help!
public class MainActivity extends AppCompatActivity {
public TextView time;
public SeekBar bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
time = (TextView) findViewById(R.id.time);
bar = (SeekBar) findViewById(R.id.bar);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar.setOnSeekBarChangeListener(listener);
}
private SeekBar.OnSeekBarChangeListener listener = new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
time.setText((String.valueOf(progress/100) + ":" + String.valueOf(progress % 100)));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};}
Upvotes: 1
Views: 5626
Reputation: 1655
This error occur too when you call an wrong activity or when you call the correct activity and call another wrong activity that do not have the component you want, example
setContentView(R.layout.correct_activity_with_your_component);
some code.... setContentView(R.layout.wrong_activity_without_your_component_bar_activity_main);
bar = (SeekBar) findViewById(R.id.bar);
So one solution is write the line similar to setContentView(R.layout.correct_activity_with_your_component); before your componenet and verify if there is not another command similar to
setContentView(R.layout.correct_activity_with_your_component);
before you call your component with:
bar = (SeekBar) findViewById(R.id.bar);
Upvotes: 0
Reputation: 2740
I would try to set these lines
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first in the onCreate-method, and after that, try to find the seekBar view with
bar = (SeekBar) findViewById(R.id.bar);
Please, let me know if it solves your problem!
The findViewById() method will try to find the view with the specified ID. But to do that, it has to know which layout file is used for the activity, so that it knows where to search. That's what you point out with the setContentView, and that's why it has to come first.
Upvotes: 10
Reputation: 7027
Replace your onCreate
with:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = (TextView) findViewById(R.id.time);
bar = (SeekBar) findViewById(R.id.bar);
bar.setOnSeekBarChangeListener(listener);
}
You should call setContentView()
before using findViewById
!
Upvotes: 2