Android Studio :How to solve this execution error - java

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

Related

How to Get Data from AutoComplete Text View on Button Press?

I want to use the if-else statement on Button press - if-else statement get the string from autoComplete text view... but it not works...
I want to make the calculation when selecting the first product - but it does not work.
public class MainActivity extends AppCompatActivity {
TextInputLayout textInputLayout,alkalinityInput,waterVoume;
TextInputEditText waterText,alkaText;
TextView result;
AutoCompleteTextView dropDownText;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
waterVoume=findViewById(R.id.waterVolumeInputText);
textInputLayout = findViewById(R.id.productTextDropdown);
dropDownText = findViewById(R.id.dropdown_text);
alkalinityInput = findViewById(R.id.alkalinityInputText);
waterText = findViewById(R.id.waterVtext);
alkaText = findViewById(R.id.alkaVtext);
result=findViewById(R.id.resultTextView);
String[] items = new String[] {
"AquaForest - KH Plus",
"Brightwell - Reef Code B",
};
ArrayAdapter<String> adapter = new ArrayAdapter<>(
MainActivity.this,
R.layout.dropdown_item,
items
);
dropDownText.setAdapter(adapter);
button=findViewById(R.id.calculateButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(dropDownText.getText().equals("AquaForest - KH Plus"))
{makeCalculations();
}
}
private void makeCalculations() {
double vouumeValue = Double.valueOf(waterText.getText().toString());
double alkvalue = Double.valueOf(alkaText.getText().toString());
double resultView = 1*vouumeValue*alkvalue;
result.setText("The result is: " + resultView );
double resultView = 5*vouumeValue*alkvalue;
result.setText("The result is: " + resultView );}*/
});
}
}

Android seekbar textView

I have 3 seekbar to change the background color. Each seekbar har a textView from 0-255 to show the colorcode in RGB. When I slide one of the seekbar, the RGB textView change on all. How can I specify, so only the textView that belong to the seekbar, that I slide will change?
private SeekBar.OnSeekBarChangeListener seekBarChangeListener
= new SeekBar.OnSeekBarChangeListener()
{
TextView seekBar1Progress;
TextView seekBar2Progress;
TextView seekBar3Progress;
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
updateBackground();
seekBar1Progress = (TextView) findViewById(R.id.viewSlider1);
seekBar1Progress.setText(" " + progress);
seekBar2Progress = (TextView) findViewById(R.id.viewSlider2);
seekBar2Progress.setText(" " + progress);
seekBar3Progress = (TextView) findViewById(R.id.viewSlider3);
seekBar3Progress.setText(" " + progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
You are getting SeekBar object in OnProgressChanged method, you can compare its id to your individual SeekBar. Try below code:
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(seekBar.getId()==R.id.seekBar1){
seekBar1Progress = (TextView) findViewById(R.id.viewSlider1);
seekBar1Progress.setText(" " + progress);
}else if(seekBar.getId()==R.id.seekBar2){
seekBar2Progress = (TextView) findViewById(R.id.viewSlider2);
seekBar2Progress.setText(" " + progress);
}else{
seekBar3Progress = (TextView) findViewById(R.id.viewSlider3);
seekBar3Progress.setText(" " + progress);
}
}
Hope this will help!!

radiobuttons (change condition to onclick)

In general have this screen:
How to put conditions from OnClick to onCheckedChanged (radiobutton). It works only with one variable price_53_off in OnClick, but I want when checked other radiobutton (500 gr.) program parses other variable (price_73_off) and calculate other cycle.
Some code below.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
ImageView plus;
ImageView minus;
TextView tvResult;
TextView price;
TextView price_53_off;
TextView price_73_off;
RadioGroup radioGroup_price;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plus = (ImageView) findViewById(R.id.plus);
plus.setOnClickListener(this);
minus= (ImageView) findViewById(R.id.minus);
minus.setOnClickListener(this);
tvResult = (TextView) findViewById(R.id.tvResult);
price = (TextView) findViewById(R.id.price);
price_53_off = (TextView) findViewById(R.id.price_53_off);
price_73_off = (TextView) findViewById(R.id.price_73_off);
radioGroup_price = (RadioGroup) findViewById(R.id.radioGroup_price);
radioGroup_price.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radioButton_250:
price.setText("53");
break;
case R.id.radioButton_500:
price.setText("73");
break;
}
}
});
}
#Override
public void onClick(View v) {
int parse_quant;
int parse_price_53;
int num2 = 1;
int result = 0;
int resultprice=0;
parse_quant = Integer.parseInt(tvResult.getText().toString());
parse_price_53 = Integer.parseInt(price_53_off.getText().toString());
switch (v.getId()) {
case R.id.plus:
result = parse_quant + num2;
resultcena= result*parse_price_53;
}
switch (v.getId()) {
case R.id.minus:
result = parse_quant - num2;
if (parse_quant==1){
return;
}
resultprice= result*parse_price_53;
}
tvResult.setText( String.valueOf(result) );
price.setText( String.valueOf(resultprice) );
}
}
In order for result and resultPrice variables to be available in both onCheckChanged and onClick, you need to store them at the class level instead of local inside the onClick method.

Get Position value of Dynamically added view in layout android

I m trying this tutorial : http://android-er.blogspot.in/2014/01/get-text-from-dynamically-added-view.html \
where i m trying to edit the text when user click the insert button, it works fine only if i put position as static value.
but when i try to get position this app unfortunat stop.
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
Button buttonShowAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textIn = (EditText)findViewById(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
final TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
Button buttonInsert = (Button)addView.findViewById(R.id.insert);
buttonInsert.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//int position = (Integer)v.getTag(); // I m trying to get position of button here but stops the app
int position = 2; // Static value for changing the textview works fine
String showallPrompt = "";
View childView = container.getChildAt(position);
TextView childTextView = (TextView) (childView.findViewById(R.id.textout));
String childTextViewText = (String) (childTextView.getText());
showallPrompt += position + ": " + childTextViewText + "\n";
Toast.makeText(MainActivity.this,
showallPrompt,
Toast.LENGTH_LONG).show();
((TextView) (childTextView.findViewById(R.id.textout))).setText("HELLO ");
}});
container.addView(addView, 0);
}});
LayoutTransition transition = new LayoutTransition();
container.setLayoutTransition(transition);
}
}
So here, i used to get position by
//int position = (Integer)v.getTag(); // I m trying to get position of button here but stops the app
int position = 2; // Static value for changing the textview works fine
but not working . How to solve this problem.Actually i m tring to do listview with editable text and show as textview.

OnSeekBarChangeListener always evaluates to null in Android

I have an android program that I am making with 4 seekbars, and 4 textviews to accompany each seekbar. Right now, I am trying to make each textview's contents equal to the integer progress of the seekbars. This is my code as it stands, and I have tried many ways to properly implement the OnSeekBarChangeListener, but LogCat still shows that it is null when sb1-sb4 sets their OnSeekBarChangeListener to OnSeekBarProgress. Please help me identify my problem :(
public class TippopotamusActivity extends Activity {
SeekBar sb1;
SeekBar sb2;
SeekBar sb3;
SeekBar sb4;
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
sb1 = (SeekBar) findViewById(R.id.seekBar1);
sb2 = (SeekBar) findViewById(R.id.seekBar2);
sb3 = (SeekBar) findViewById(R.id.seekBar3);
sb4 = (SeekBar) findViewById(R.id.seekBar4);
sb1.setOnSeekBarChangeListener(OnSeekBarProgress);
sb2.setOnSeekBarChangeListener(OnSeekBarProgress);
sb3.setOnSeekBarChangeListener(OnSeekBarProgress);
sb4.setOnSeekBarChangeListener(OnSeekBarProgress);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
OnSeekBarChangeListener OnSeekBarProgress =
new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar s, int progress, boolean touch){
if(s.getId() == R.id.seekBar1)
{
tv1.setText(progress);
}
else if(s.getId() == R.id.seekBar2)
{
tv2.setText(progress);
}
else if(s.getId() == R.id.seekBar3)
{
tv3.setText(progress);
}
else
{
tv4.setText(progress);
}
}
public void onStartTrackingTouch(SeekBar s){
}
public void onStopTrackingTouch(SeekBar s){
}
};
Move your setContentView(R.layout.main); before initializing your variables. Your onCreate method should look like this
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
sb1 = (SeekBar) findViewById(R.id.seekBar1);
sb2 = (SeekBar) findViewById(R.id.seekBar2);
sb3 = (SeekBar) findViewById(R.id.seekBar3);
sb4 = (SeekBar) findViewById(R.id.seekBar4);
sb1.setOnSeekBarChangeListener(OnSeekBarProgress);
sb2.setOnSeekBarChangeListener(OnSeekBarProgress);
sb3.setOnSeekBarChangeListener(OnSeekBarProgress);
sb4.setOnSeekBarChangeListener(OnSeekBarProgress);
}

Categories