I'm new to programming and Android. I'm making my best attempt at a simple app and I'm stuck!
I have two editTexts (set to accept numbers only) and a button. The idea is to display the sum of the two user inputs when the button is pressed. However, my app stops working and force closes when I click the button in the emulator.
Any help would be greatly appreciated.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prevailing_torque);
Button calculatePrevailing = (Button) findViewById(R.id.button_calculatePrevailing);
calculatePrevailing.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView prevailingSetting = (TextView) findViewById(R.id.textViewSetting);
EditText editTextPrevailing = (EditText) findViewById(R.id.editTextPrevailing);
EditText editTextRecommended = (EditText) findViewById(R.id.editTextReccomended);
int p = Integer.parseInt(editTextPrevailing.getText().toString());
int r = Integer.parseInt(editTextRecommended.getText().toString());
prevailingSetting.setText(r+p);
}
});
}
I've looked at several similar questions on here but I haven't been able to find anything that I can implement further than what I have already done. But I am probably looking in the wrong places.
Thanks!
You can't use setText() with an Integer since it expects a CharSequence, you have to use
prevailingSetting.setText(String.valueOf(r+p));
or just
prevailingSetting.setText(""+(r+p));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prevailing_torque);
Button calculatePrevailing = (Button) findViewById(R.id.button_calculatePrevailing);
TextView prevailingSetting = (TextView) findViewById(R.id.textViewSetting);
EditText editTextPrevailing = (EditText) findViewById(R.id.editTextPrevailing);
EditText editTextRecommended =(EditText)findViewById(R.id.editTextReccomended);
calculatePrevailing.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int p = Integer.parseInt(editTextPrevailing.getText().toString());
int r = Integer.parseInt(editTextRecommended.getText().toString());
int s = r+p;
prevailingSetting.setText(Integer.toString(s));
}
});
Ok ! if this is your problem then first of all convert your editText value to string and after that convert it into integer like !
String str1=editTextPrevailing.getText().toString();
String str2=editTextRecommended.getText().toString();
int p = Integer.parseInt(str1);
int r = Integer.parseInt(str2);
int s = r+p;
prevailingSetting.setText(""+s);
Related
I know this questions already has been asked by some peoples but Still I am getting issue fro last 1 day. I have chat application and My emoji is sent successfully and displaying perfect in Website but In TextView its returning strange characters. I am using this Library for Emoji in TextView
This is Response from Web
data
:
"{"type":"text","message":"\ud83d\ude29","to":"264","from":"175","group_id":"0","groupFlag":0}"
but My TextView showing this Value
ðÿ˜ðÿ˜¡ðÿ˜¡ðÿ˜¡
**My ScreenShot is Here **
Please guide me How can I show Emoji in TextView. ? My complete code is
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootView = findViewById(R.id.root_view);
emojiButton = (ImageView) findViewById(R.id.emoji_btn);
submitButton = (ImageView) findViewById(R.id.submit_btn);
emojiconEditText = (EmojiconEditText) findViewById(R.id.emojicon_edit_text);
textView = (EmojiconTextView) findViewById(R.id.textView);
emojIcon = new EmojIconActions(this, rootView, emojiconEditText, emojiButton);
emojIcon.ShowEmojIcon();
emojIcon.setKeyboardListener(new EmojIconActions.KeyboardListener() {
#Override
public void onKeyboardOpen() {
Log.e("Keyboard", "open");
}
#Override
public void onKeyboardClose() {
Log.e("Keyboard", "close");
}
});
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newText = emojiconEditText.getText().toString();
textView.setText(newText);
}
});
}
can you try this may be worked:
emojIcon.setUseSystemEmoji(true);
Perhaps you need you need to convert the Html request to string
Html.fromHtml((String) YOUR_STRING_HERE).toString()
This question already has answers here:
OnClickListener in Android Studio
(5 answers)
Closed 6 years ago.
I am new to android studio and tried a tutorial where the presenter demonstrated his program but when I type out the code I am persistently getting an error like Cannot resolve Symbol setOnClickListener where the setOnClickListener is highlighted in red.
TextView Resultant = (TextView) findViewById(R.id.Resultant);
EditText Percentage_Input = (EditText) findViewById(R.id.Percentage_Input);
EditText Number_Input = (EditText) findViewById(R.id.Percentage_Input);
#Override
public View findViewById(#IdRes int id) {
return super.findViewById(R.id.Percentage_Input);
}
Button calc_btn = (Button) findViewById(R.id.calc_btn);
calc_btn.setOnClickListener(new View.OnClickListener(){
public void onClick((View) view); {
float percent = Float.parseFloat(Percentage_Input.getText().toString());
float dec = percent/100;
float Qty = Float.parseFloat(Number_Input.getText().toString());
float total = dec*Qty;
Resultant.setText(Float.toString(total));
}
})
I think your code is not in the right place. I see you are overriding findViewById (I wonder why) and rest of your code is also at the same level. You need to put all this code within a lifecycle method.
Maybe in onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView Resultant = (TextView) findViewById(R.id.Resultant);
EditText Percentage_Input = (EditText) findViewById(R.id.Percentage_Input);
EditText Number_Input = (EditText) findViewById(R.id.Percentage_Input);
Button calc_btn = (Button) findViewById(R.id.calc_btn);
calc_btn.setOnClickListener(new View.OnClickListener() {
public void onClick((View) view); {
float percent = Float.parseFloat(Percentage_Input.getText().toString());
float dec = percent / 100;
float Qty = Float.parseFloat(Number_Input.getText().toString());
float total = dec * Qty;
Resultant.setText(Float.toString(total));
}
});
}
Also you are missing a semicolon ; at the end of setting OnClickListener.
Move ; and put at the end, this:
TextView Resultant = (TextView) findViewById(R.id.Resultant);
EditText Percentage_Input = (EditText) findViewById(R.id.Percentage_Input);
EditText Number_Input = (EditText) findViewById(R.id.Percentage_Input);
#Override
public View findViewById(#IdRes int id) {
return super.findViewById(R.id.Percentage_Input);
}
Button calc_btn = (Button) findViewById(R.id.calc_btn);
calc_btn.setOnClickListener(new View.OnClickListener(){
public void onClick((View) view) {
float percent = Float.parseFloat(Percentage_Input.getText().toString());
float dec = percent/100;
float Qty = Float.parseFloat(Number_Input.getText().toString());
float total = dec*Qty;
Resultant.setText(Float.toString(total));
}
});
Remove you method findViewById, this is some weird stuff, returning always the same view.
Put the rest of your code inside onCreate and add the missing ; at the end.
So I'm in a basic part of my application I'm wanting to make. I've never gotten this error before, and I don't know what's going on. My .setText is throwing an error saying "setText cannot be resolved or is not a field" I've looked around and haven't been able to find my problem. I believe I'm doing it correctly. If anyone could help me out that'd be great!
MainActivity.java:
public class MainActivity extends Activity {
final TextView loading_Text = (TextView)findViewById(R.id.textView4);
final EditText name_Edit = (EditText)findViewById(R.id.editText1);
//String Values
String Age="";
String Name = name_Edit.getText().toString();
//Int Values
int Gender = 0; //1 male | 2 female
int Group = 0; //Different groups for ages and genders
int save_Info = 0; //save info to phone
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button male_Button= (Button)findViewById(R.id.button1);
Button female_Button = (Button)findViewById(R.id.button2);
male_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Gender++;//Adds one to show this user is a male.
loading_Text.setText=(Name);
}
});
}
I saw two problems:
First:
loading_Text.setText=(Name);
Should be
loading_Text.setText("The text you want to set");
You'll need to take a look at the API document to see how to call the method.
Second:
Move these part:
final TextView loading_Text = (TextView)findViewById(R.id.textView4);
final EditText name_Edit = (EditText)findViewById(R.id.editText1);
//String Values
String Age="";
String Name = name_Edit.getText().toString();
inside your onCreate, like this:
public class MainActivity extends Activity {
TextView loading_Text;
EditText name_Edit;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loading_Text = (TextView)findViewById(R.id.textView4);
name_Edit = (EditText)findViewById(R.id.editText1);
Or you'll get NullPointerException.
This is because you were trying to reach the View's property before the view is being initialized. View will be initialized after setContentView, and what you were intend to do was findViewById from R.layout.activity_main before it had been loaded.
Similarly, you'll need to move this call of method:
String Name = name_Edit.getText().toString();
somewhere after setContentView.
setText is a function. So you would need to pass name as a argument.
like loading_Text.setText(Name);
Change
loading_Text.setText=(Name);
to this:
loading_Text.setText(Name);
Also, if you don't see anything in the textview, it is because you are getting the edittext's text before you even create your views, I use an on edittext listener like this to refresh the String when the edit text is changed:
name_Edit.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Name = name_Edit.getText().toString();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
I hope this works for you :)
It's hard to explain in the title. Basically as far as I can tell the submit button is taking the name and placing it in the array like I want. What I need now is for the Play(done) Button to transfer the user and the data to the next class (which I believe is coded correctly) and I need it to start a game. The game which you will see in the second class (get the data from the previous) I need it to display the names from the names array 1 at a time and randomly assign them a task to do from the tasks array.
Currently the app is force closing after I click the play button. I'm linking both classes cause I'm pretty sure the problem is in the second class. Thanks for your help ahead of time.
Class1
public class Class1 extends Activity
{
int players=0;
String names[];
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.class1);
final EditText input = (EditText) findViewById(R.id.nameinput);
final ArrayList<String> names = new ArrayList<String>();
Button submitButton = (Button) findViewById(R.id.submit_btn);
submitButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View submit1)
{
players++;
for(int i=0; i < 6; i++)
{
names.add(input.getText().toString());
input.setText("");
}
}
});
Button doneButton = (Button) findViewById(R.id.done_btn);
doneButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View done1)
{
Intent done = new Intent(Class1.this, Game.class);
Bundle bundle = new Bundle();
bundle.putStringArrayList("arrayKey", names);
done.putExtra("players", players);
//done.putExtra("names", names[players]);
startActivity(done);
}
});
}
Game Class
public class Game extends Activity
{
int players, counter=0, score, ptasks,rindex;
String[] names, tasks;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Bundle bundle = this.getIntent().getExtras();
String[] names = bundle.getStringArray("arrayKey");
Intent game = getIntent();
players = game.getIntExtra("players", 1);
//names = game.getStringArrayExtra("names");
Random generator = new Random();
tasks[0]= "";
tasks[1]= "";
tasks[2]= "";
tasks[3]= "";
tasks[4]= "";
tasks[5]= "";
tasks[6]= "";
tasks[7]= "";
tasks[8]= "";
tasks[9]= "";
while (counter <5)
{
for (int i = 0; i < players; i++)
{
TextView name1 = (TextView) findViewById(R.id.pname);
name1.setText( names[i]+":");
ptasks = 10;
rindex = generator.nextInt(ptasks);
TextView task = (TextView) findViewById(R.id.task);
task.setText( tasks[rindex]);
}
Button failButton = (Button) findViewById(R.id.fail_btn);
failButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View failed)
{
}
});
Button notButton = (Button) findViewById(R.id.notbad_btn);
notButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View notbad)
{
}
});
Button champButton = (Button) findViewById(R.id.champ_btn);
champButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View champp)
{
}
});
counter++;
}
}
Thought I should also mention that these buttons on the 2nd page I would like to assign a score to whichever name array person is up and keep track until the final round where it will display the winner. If anyone has any idea how to make that work.
Have you made sure to include the Game activity as an Application Node in your AndroidManifest?
If not, open your manifest to the application tab, on the bottom hit Add, and add an Activity of the name .Game
Without this, that intent never gets received by the other class.
You've already been told that you use non-initialized arrays here: EditText and using buttons to submit them. But also you're trying to get array extra, however you don't put it inside intent. And you're using uninitialized tasks array in Game class.
I'm a beginner in application development. My problem is, that when I run my app and I click on the Calculate button, the program stops. The code:
public class screen1 extends Activity {
private EditText name;
private CheckBox box1;
private Spinner spinner;
private TextView text1, text2, text3, text4, text5, text6;
private Button calcbutton, closebutton;
String strength;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner hubSpinner = (Spinner) findViewById(R.id.myspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.military_ranks , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hubSpinner.setAdapter(adapter);
name = (EditText)findViewById(R.id.editText1);
strength = name.getText().toString();
box1 = (CheckBox)findViewById(R.id.checkBox1);
text1 = (TextView)findViewById(R.id.textView4);
text2 = (TextView)findViewById(R.id.textView6);
text3 = (TextView)findViewById(R.id.textView8);
text4 = (TextView)findViewById(R.id.textView10);
text5 = (TextView)findViewById(R.id.textView12);
text6 = (TextView)findViewById(R.id.textView14);
final Button calcbutton = (Button) findViewById(R.id.button1);
calcbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int str = Integer.valueOf(strength);
int rank = spinner.getSelectedItemPosition()+1;
double sebzes;
if(box1.isChecked()){
sebzes = (((rank-1)/20+0.3)*((str/10)+40))*1*(1+1/100);
text1.setText(Double.toString(sebzes));
}
else{
sebzes = (((rank-1)/20+0.3)*((str/10)+40))*1;
text1.setText(Double.toString(sebzes));
}
}
});
final Button closebutton = (Button) findViewById(R.id.button2);
closebutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
In the edittext component you should be able to write numbers only. I don't know why it's not working.
The problem are these two lines:
int str = Integer.valueOf(strength);
int rank = spinner.getSelectedItemPosition()+1;
The first won't fail if you use only numbers in your EditText but it would be better to ensure that or at least catch the exception that is thrown when you try to convert a character to a numberical value. Additionally you could also use Integer.valueOf(strength).intValue(); even so it is normally not really necessary.
The real problem is the second line. You declared the variable spinner but you never instantiate it. That's why you will get a NullPointerException there.
On an unrelated note: You also should start your class name with a capital letter to follow the Java naming conventions.
You're not instantiating spinner anywhere, but you're referencing it in the second line of your button click method. Probably a null reference problem.