main method not being called in Android Activity - java

So i have this code which solves the quadratic equation in java (android development) and it isnt doing anything!!!! The button when i press it does not give the answer at all... i cant even check if it is doing it correctly.
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class QuadraticEquationSolver extends Activity {
public void main(String[] args){
Button calc = (Button) findViewById(R.id.Calculate);
calc.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
EditText X2 = (EditText)findViewById(R.id.X2);
EditText X = (EditText)findViewById(R.id.X);
EditText Num = (EditText)findViewById(R.id.Num);
TextView ans= (TextView) findViewById(R.id.finalans);
double x2 = Integer.parseInt(X2.getText().toString());
double x = Integer.parseInt(X.getText().toString());
double num = Integer.parseInt(Num.getText().toString());
double finalNum = ((x*-1) + (Math.sqrt((x*x)-(4*x*num))))/(2*x2);
ans.setText("answer: " + finalNum);
}
});
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quadratic_equation_solver);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_quadratic_equation_solver, menu);
return true;
}

First of all, welcome to Android development. I would highly recommend as a starting point you read the App Fundamentals and related guides on the SDK documentation site, as they will help you greatly in your new endeavour.
Android does not use a single entry point (i.e. main method) so your code will not be called. You will want to move all that code into onCreate().
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quadratic_equation_solver);
Button calc = (Button) findViewById(R.id.Calculate);
calc.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
EditText X2 = (EditText)findViewById(R.id.X2);
EditText X = (EditText)findViewById(R.id.X);
EditText Num = (EditText)findViewById(R.id.Num);
TextView ans= (TextView) findViewById(R.id.finalans);
double x2 = Integer.parseInt(X2.getText().toString());
double x = Integer.parseInt(X.getText().toString());
double num = Integer.parseInt(Num.getText().toString());
double finalNum = ((x*-1) + (Math.sqrt((x*x)-(4*x*num))))/(2*x2);
ans.setText("answer: " + finalNum);
}
});
}

I don't do any android programming, but I doubt Android will ever call your main method. The content of this main method must probably be in the onCreate method.

onCreate() is your main() equivalent in Android. Your main() function will never be called. The contents of main() should go in onCreate().

Related

Android studio: How do I show an updating variable at all times using a Textview?

The android studio code works as intended. However, points are made in the background since it's an idle game. An on click method only shows the points that were made as of the last click. I need the points to be shown at all times.
I have been testing this new added bit of code but the points are not updating when the button is clicked. Example in photo. Evolution Points = 0; The on click method is updating but there are also points made in the background that need to be shown without a button click. Any suggestions?
visibletotals.setText("Evolutions Points: " + clicks);
full code for reference
package com.example.idleclicker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
TextView points;
TextView visibletotals;
Button click;
Button upgradebtn;
TextView Leveltext;
int clicks = 0;
int clickcost = 10;
int upgradelevel = 1;
Timer myTimer = new Timer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = (Button) findViewById(R.id.click);
points = (TextView) findViewById(R.id.points);
upgradebtn = (Button) findViewById(R.id.upgradebtn);
Leveltext = (TextView) findViewById(R.id.leveltext);
visibletotals = (TextView) findViewById(R.id.visibletotals);
visibletotals.setText("Evolutions Points: " + clicks);
click.setEnabled(true);
upgradebtn.setEnabled(true);
myTimer.schedule(new TimerTask() {
#Override
public void run() {
clicks+= upgradelevel;
}
}, 0, 1000);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
clicks++;
points.setText(getResources().getString(R.string.evol) + clicks);
}
});
upgradebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (clicks >= clickcost) {
clicks -= clickcost;
upgradelevel += 1;
clickcost *= 2;
upgradebtn.setText(getResources().getString(R.string.Upgrade) +
clickcost +
getString(R.string.LevelText)
+ upgradelevel);
};
}
});
}}
I can see that your clicks is changing(dynamic) while your set text is static that means it is only replying at the start and it is not triggered from the next time you do it. one way to tackle this is put the settext value in the trigger block(i.e onclicklistener) and make it set the fresh text as per triggered.

How to send string or intger value from android studio App in Java to Raspberry Pi

I have managed to create a simple counter app in android studios. I would like to know if there is a possible way to send the integer value to raspberry pi from my android app. I am a beginner in coding and thus have difficulty understanding and implementing some of the solutions suggested online.
****The counter button was working last week, however, for some reason, it is not working now.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button buttonINC, buttonDEC;
TextView tvDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonINC = findViewById(R.id.btn_inc_id);
buttonDEC = findViewById(R.id.btn_dec_id);
buttonINC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String currentvalue = tvDisplay.getText().toString();
int value = Integer.parseInt(currentvalue);
value++;
tvDisplay.setText(String.valueOf(value));
}
});
buttonDEC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String currentvalue = tvDisplay.getText().toString();
int value = Integer.parseInt(currentvalue);
value--;
tvDisplay.setText(String.valueOf(value));
}
});
}
}

Declare multiple variables in one class in Android Studio

I have an assignment to build a simple multi-widget application in Android Studio and I am trying to figure out how to declare multiple variables inside one class for a multi-page photo rating app. I have 4 pages, and 4 photos with a RatingBar underneath each of them. I would like to have the average of all the ratings displayed on the top of the page. For simplicity I have it being cast to the page title using SetTitle
How can I write this java in such a way that I can access all 4 of my ratings and apply the basic math to them? This is technically beyond what we have been taught so far in this class.
package ca.bcit.comp2052.a00587366.multiplewidgetsapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Start of rating bar
RatingBar ratingBar1 = (RatingBar) findViewById(R.id.ratingBar1);
ratingBar1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar1, float rating, boolean fromUser) {
// Implement your logic here
float total = 0;
total += ratingBar1.getRating();
// total += ratingBar2.getRating();
// total += ratingBar3.getRating();
// total += ratingBar4.getRating();
float average = total / 4;
setTitle("Average: " + average);
}
});
// end of rating bar
Button buttonNext = (Button) findViewById(R.id.nextButton);
buttonNext.setOnClickListener(new
android.view.View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
Main2Activity.class));
}
});
}
}
You should probably have them as R.id.ratingBars[0] and so forth, but since you don't, assuming it's R.id.ratingBar1 through R.id.ratingBar4, you can do something like this:
final RatingBar[] ratingBars = {
(RatingBar) findViewById(R.id.ratingBar1),
(RatingBar) findViewById(R.id.ratingBar2),
(RatingBar) findViewById(R.id.ratingBar3),
(RatingBar) findViewById(R.id.ratingBar4)
};
for (final RatingBar ratingBar : ratingBars) {
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar bar, float rating, boolean fromUser) {
// Implement your logic here
float total = 0.0f;
for (final RatingBar ratingBar : ratingBars) {
total += ratingBar.getRating();
}
float average = total / 4.0f;
setTitle("Average: " + average);
}
});
}
This also makes it easier to add new RatingBars if necessary. You just add them to the original array initialization list at the top and everything is taken care of.
You could try calling them by
Main2Activity.ratingBar2.getRating()
But they should be public, which is not too good.
Elsewhere you can declare them private and the create public getter methods to retrieve them from external classes.

Decimal part is lost when displaying the result of division

I'm fairly new to Android Studio. I've tried to create an app that calculates how much you can spend everyday based on you income. My problem is that it doesn't display any decimals. Heres the code:
package com.example.amazi.howmuchmoney;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText et1,et2;
TextView tvResult;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText) findViewById(R.id.editText1);
et2=(EditText) findViewById(R.id.editText2);
tvResult=(TextView) findViewById(R.id.textView3);
button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
int num1=Integer.parseInt(et1.getText().toString());
int num2=Integer.parseInt(et2.getText().toString());
int result=num1/num2;
tvResult.setText("Each day you can spend "+result);
}
});
}
}
Mathematical operations performed on integers are exact, therefore;
(int) 3 / (int) 2 = (int) 1
Using double as your data type will fix your problem, although;
Integer.parseInt
Will still return an integer value, try;
Double.parseDouble

Buttons onClick method restarts if clicked to fast

I have little application where you can tell your fortune (it's actualy just random), there are four edittextfields that you can type in and one button that choose one of the edittextfields, and remove it. Pretty simple. But i have problem, if someone presses the button multiple times too fast then all of the code in the onClick() method doesn't execute (probably because it is called again). Is there some way that I can prevent this from happening (I want all of the code in the onClick() method to execute before it can be called again)?
Here is the code:
package com.foretell.lukas.spamedprick;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
ArrayList<EditText> tfa = new ArrayList<EditText>();
int x = 0;
boolean tf = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// References to XML widgets
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.background);
final Button daButton = (Button) findViewById(R.id.button);
Button restartButton = (Button) findViewById(R.id.restartButton);
final EditText ruta1 = (EditText) findViewById(R.id.editText);
final EditText ruta2 = (EditText) findViewById(R.id.editText2);
final EditText ruta3 = (EditText) findViewById(R.id.editText3);
final EditText ruta4 = (EditText) findViewById(R.id.editText4);
final TextView outputText = (TextView) findViewById(R.id.outputText);
tfa.add(ruta1);
tfa.add(ruta2);
tfa.add(ruta3);
tfa.add(ruta4);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.spar);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.hurra);
daButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
if(tf) {
daButton.setEnabled(false);
mp.start();
try {
tf = false;
Thread.sleep(2000);
} catch (InterruptedException e) {
e.getStackTrace();
outputText.setText("Nått gick åt h***ete!");
}
Collections.shuffle(tfa);
x++;
if (x <= 2) {
outputText.setText("Det är inte " + tfa.get(0).getText() + "...");
tfa.get(0).setText("");
tfa.remove(0);
} else if (x == 3) {
outputText.setText("Det är...");
} else if (x == 4) {
tfa.get(1).setText("");
tfa.remove(1);
outputText.setText("Det är " + tfa.get(0).getText() + "!");
mp2.start();
}
tf = true;
daButton.setEnabled(true);
}else{
System.out.println("YO!");
}
}
}
);
restartButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
tfa.add(ruta1);
tfa.add(ruta2);
tfa.add(ruta3);
tfa.add(ruta4);
ruta1.requestFocus();
outputText.setText("");
x = 0;
ruta1.setText("");
ruta2.setText("");
ruta3.setText("");
ruta4.setText("");
}
}
);
}
}
Thanks.
I dont think that an "onClick" can be started before the last "onClick" is finished.
Here is the qoute from the android documentation:
The system does not create a separate thread for each instance of a component. All components that run in the same process are instantiated in the UI thread, and system calls to each component are dispatched from that thread. Consequently, methods that respond to system callbacks (such as onKeyDown() to report user actions or a lifecycle callback method) always run in the UI thread of the process.
http://developer.android.com/guide/components/processes-and-threads.html
Maybe you do something in the onClick method, that makes trouble, but without the code it is hard to tell:)
You have to do something like this
Button button= (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button.setEnabled(false);
// do your work here
// make it true after your work is done
button.setEnabled(true);
}
});

Categories