I am new to android studio, and I don't understand why my app crash when I start using the seekbar. Could someone please help me?
public class MainActivity extends AppCompatActivity { SeekBar seekBar; //declare seekbar object TextView textView, textView2; ListView simpleListview; //declare member variables for SeekBar int discrete; int start = 50; int start_position = 50; //progress tracker int temp = 0; //declare objects for ViewStub ViewStub stub; CheckBox checkBox; ListView lv; //declare Listview object Button button; View view;
public MainActivity() {
discrete = 0;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//declare viewstub object
stub = findViewById(R.id.viewStub1);
@SuppressWarnings("unused")
View inflated = stub.inflate();
stub.setVisibility(View.INVISIBLE);
//ViewStub logic
checkBox = findViewById(R.id.checkBox1);
//handle checkbox click event
checkBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if (isChecked) {
//remove objs from parent view to allow for child view objs
checkBox.setVisibility(View.GONE);
seekBar.setVisibility(View.GONE);
textView.setVisibility(View.GONE);
stub.setVisibility(View.VISIBLE);
}
}
});
//seekbar logic
textView = findViewById(R.id.textview);
textView = findViewById(R.id.textView2);
textView.setText(" Celsius at 0 degrees");
//set default view
seekBar = findViewById(R.id.seekbar);
seekBar.setProgress(start_position);
//create event handler for SeekBar
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (temp == 0) {
//for initial view result
//Toast.makeText(getBaseContext(), "Fahrenheit result: 32 degrees",
//Toast.LENGTH_SHORT).show();
textView2.setText("Fahrenheit result 32 degrees") ;
}
else{
//Toast.makeText(getBaseContext(), "Fahrenheit result: "
//+ String.valueOf(discrete) +
//" degrees",Toast.LENGTH_SHORT).show();
textView2.setText("Fahrenheit result:" + String.valueOf(discrete) + "degrees");
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar){ }
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
// To convert progress passed as discrete (Fahrenheit) value
temp = progress - start;
discrete = (int) Math.round((((temp * 9.0) /
5.0) + 32));
//convert C to F temp textView.setText(" Celsius at " + temp + " degrees");
textView.setText(" Celsius at " + temp + " degrees");
}
});
textView = findViewById(R.id.textview);
textView = findViewById(R.id.textView2);
textView2 is not initialised here...
Change above code to
textView = findViewById(R.id.textview);
textView2 = findViewById(R.id.textView2);
TextView textView = findViewById(R.id.textview); TextView textView2 = findViewById(R.id.textView2);
initialize textView2 You seem to have missed that part
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.