Setting onSeekBarChangeListener causes null object exception - java

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) {
}
};}

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.

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!

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);

Related

Trying to execute multiple layout from single class

I'm trying to execute more then one layout screens from single class using onClick() method
Here goes my code
Button bt1,bt2;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.button);
bt2 = (Button)findViewById(R.id.button1);
onClick(); //onClick(View) in MainActivity cannot be applied to ()
}
public void onClick(View v){
if(v.getId()==R.id.button){
setContentView(R.layout.next1);
}
else if(v.getId()==R.id.button){
setContentView(R.layout.next1);
}
}
Kindly help me out, Thank you
Fragment is what you have to use, in this case. Load either of the fragment based on button click.
If you go for setContentView in this scenario, then your code will be clumsy and it will be very tedious for you to keep track on view.

TextView doesn't show the required text

My MainActivity class has an if that checks is something is true it starts a new Activity which sets the text of a TextView to what I want.
MainActivity:
public static int caseA = 1;
if(caseA) {
Intent i = new Intent(Timer.this, WorkTimerNotification.class);
startActivity(i);
}
then it goes to the new Activity and it should check the value of caseA.
WorkTimerNotification:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
TextView timerMetric = (TextView)findViewById(R.id.tester_texter);
if(MainActivity.caseA) {
timerMetric.setText("min");
}
Problem: It does not change the text. Also, lets say I have many other if statements in mainactivity like:
if(caseB) {}
if(caseC) {}
//and so on..
How would I perform the checks? More importantly, how do I get this caseA check to work though.
You need to call findViewById() in onCreate():
private TextView timerMetric;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
timerMetric = (TextView)findViewById(R.id.tester_texter);
if(caseA) {
timerMetric.setText("min");
}
}
Right now you are calling it before calling setContentView(), before the view hierarchy is generated. For this reason, your code fails.

onClick not working in android studio

I'm trying to learn android development with Android Studio but I can't seem to figure out why my clicks are not registering. Is there something I'm missing here?
I don't like the way Stack Overflow FORCES me to format things they way THEY want. Here is pastebin! :_
http://pastebin.com/hdzZpsud
call this changeText() method inside onCreate() method of your activity
your activity should look like this:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
changeText();
}
public void changeText(){
final Button button = (Button) findViewById(R.id.button1);
final TextView text = (TextView)findViewById(R.id.largetext);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setText("Button has been pressed");
text.setText("The large text has been changed");
}
});
}
}
In fact, you didn't set the listener. So you should add changeText() after setContentView(R.layout.activity_my);.

Null PointerExeption ALL the time?

Why is mainB_news allways null? (in the method onBackPressed())
In onCreate I set a value to the button!!! :(
PS: with findViewById() I get the same error...
public class MainActivity extends Activity {
private Button mainB_news;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainB_news = (Button) findViewById(R.id.mainB_news);
mainB_news.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.news);
}
});
}
#Override
public void onBackPressed() {
// check if page 2 is open
if (mainB_news != null && mainB_news.isShown()){
setContentView(R.layout.main); // open main view again
return;
}else
super.onBackPressed(); // allows standard use of backbutton for page 1
}
}
THANKS a lot!
Because you change contentView on button click. mainB_news doesn't exist after contentView changing. You shouldn't use setContentView() in this manner. Consider using another activity for showing news.
Because it is not defined in res/layout/main.xml ?

Cannot cast from View to ViewFlipper

Hi. I have directly copy and pasted this code but I'm getting casting error: cannot cast from view to viewflipper.
public class ViewFlipper extends Activity implements OnClickListener
{
Button next;
Button previous;
ViewFlipper vf;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
setContentView(R.layout.main);
//vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
next = (Button) findViewById(R.id.Button01);
previous = (Button) findViewById(R.id.Button02);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == next) {
vf.showNext();
}
if (v == previous) {
vf.showPrevious();
}
}
}
I haven't tried anything similar but I'd try to checkout if the ViewFlipper isn't a better way.
Another option would be to have all that as a webpage optimized for mobile view which you load locally with a WebView. That way you get full control of how many images to display and how. It is a more flexible solution in my opinion.
I had the same problem.
Make sure your class name is not ViewFlipper
Make sure that your class name is not ViewFlipper otherwise use like
android.widget.ViewFlipper vf = (android.widget.ViewFlipper)findViewById(R.id.ViewFlipper01);

Categories