I'm a newbie trying to write an app which input/outputs a number between 1-10 after pressing a button. I'm hoping to have this code throw an exception when the input value is outside the 1-10 boundary.
Caused by: java.lang.reflect.InvocationTargetException
I believe it has to do with the way I put my rand() function inside the onClick() listener. Am I on the right track that it's just written very poorly?
Thanks very much if you can help.
Here is my code:
public Button button;
public TextView textView;
public EditText editText;
Random r;
public int max=0;
public int min=0;
public int temp=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
editText=(EditText)findViewById(R.id.editText);
editText.setText("");
textView=(TextView) findViewById(R.id.output);
}
public void onClick(View view) {
rand(Integer.parseInt(editText.getText().toString()));
}
public void rand(int temp) throws IndexOutOfBoundsException{
temp = Integer.parseInt(editText.getText().toString());
if(temp >10 || temp<0){
throw new IndexOutOfBoundsException("out of bounds... between 1-10");
}
if(!editText.equals("")){
min = 10-temp;
max = r.nextInt(min + 1)+1;
}
String set = String.valueOf(max);
textView.setText(set);
}
Also, here is my XML
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="0"
android:text="Enter a Number Between 1 and 10:"
android:textSize="30sp" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="0"
android:ems="10"
android:inputType="number" />
<Button
android:id="#+id/button"
android:layout_width="369dp"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="0"
android:layout_columnSpan="2"
android:text="Button"
android:onClick="onClick"
android:textSize="30sp" />
<TextView
android:id="#+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="0"
android:text="output"
android:textSize="30sp" />
U have to put your function call into a try/catch block:
public void onClick(View view) {
try{
rand(Integer.parseInt(editText.getText().toString()));
}catch(IndexOutOfBoundsException e){
e.printStackTrace();
}
Related
So I am a begginer & I made a simple CGPA calculator. Everything is working great but I wanna add grades in TextView when each EditText gets a value. So I want to add one or multiple condition to display grades in tv textview when show_grades button is pressed. For Instance if user inputs 4.00, then I want to print grade A+ in tv textview.
As you can see, I have added a simple if statement, but I hade to make 4 individual if statement just to display a single grade in 2 fields. If I want to do it for 8 fields and 12 grades(like A+, A, A-, B+, B, B-....) I have to make 96 statements. Is there a better way to do so.
///import & stuffs
public class MainActivity2 extends AppCompatActivity {
EditText S1, S2;
TextView tv, tv2;
Button show_grade, resetButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
S1 = findViewById(R.id.S1);//edittext assigning section
S2 = findViewById(R.id.S2);
tv = findViewById(R.id.tv);
tv2 = findViewById(R.id.tv2);
show_grade = findViewById(R.id.show_grade);//button assigning section
resetButton = findViewById(R.id.resetButton);
show_grade.setOnClickListener(view -> {
double s1 = Double.parseDouble(S1.getText().toString());
double s2 = Double.parseDouble(S2.getText().toString());
double g1 = 4.00, g2 =3.50;
if(s1==g1 ){
tv.setText("A+");
}
if(s1==g2 ){
tv.setText("A-");
}
if(s2==g1 ){
tv2.setText("A+");
}
if(s2==g2 ) {
tv2.setText("A-");
}
});
resetButton.setOnClickListener(view -> { //executing reset function
S1.setText("");
S2.setText("");
});
}
}
And here is activity's layout for 1 EditText & Textview only
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="HardcodedText">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="start"
android:orientation="horizontal">
<EditText
android:id="#+id/S1"
android:layout_width="130sp"
android:layout_height="wrap_content"
android:autofillHints="creditCardNumber"
android:ems="10"
android:backgroundTint="#color/colorPrimary"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:hint="#string/S1"
android:inputType="numberDecimal"
android:textSize="20sp" />
<TextView
android:id="#+id/tv"
android:layout_width="32dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginStart="10sp"
android:background="#color/color1"
android:hint="Grades"
android:text=""
android:textSize="18sp"
android:textAppearance="#color/colorPrimary2" />
<EditText
android:id="#+id/S2"
android:layout_width="130sp"
android:layout_height="wrap_content"
android:autofillHints="creditCardNumber"
android:ems="10"
android:backgroundTint="#color/colorPrimary"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:hint="#string/S1"
android:inputType="numberDecimal"
android:textSize="20sp" />
<TextView
android:id="#+id/tv2"
android:layout_width="32dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginStart="10sp"
android:background="#color/color1"
android:hint="Grades"
android:text=""
android:textSize="18sp"
android:textAppearance="#color/colorPrimary2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="start"
android:orientation="horizontal">
<Button
android:id="#+id/show_grade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="5dp"
android:backgroundTint="#color/colorPrimary"
android:text="Show grade"
android:textColor="#android:color/white"
tools:ignore="ButtonStyle" />
<Button
android:id="#+id/resetButton"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:text="Reset"
android:textColor="#color/colorPrimary" />
</LinearLayout>
</LinearLayout>
OK. if you want to setText on some event like button click than
TextView tvText;
EditText editText;
button.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
String str = editText.getText().toString().trim();
tvText.setText(str);
}
}
);
But, if you want textview to show/update value as per change in EditText without
an event than use TextWatcher():
editText.addTextChangedListener(new TextWatcher()
{
String inputText;
#Override
public void afterTextChanged(Editable mEdit)
{
inputText = mEdit.toString();
tvText.setText(inputText);
}
public void beforeTextChanged(CharSequence s, int start, int count, int
after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int
count){
}
});
In your code change
tv.setText("Your CGPA is: " + ans);
to
tv.setText("Your CGPA is: " + String.valueOf(ans));
TextView accepts String only
I am writing a simple app which is working fine but the issue I have is that I am using a password condition to trigger a button click if entered correctly.
The issue is that my source code is saying 'onButtonClick' is never used and when I manually press that button in the app, it suddenly force closes and crashes. Anyone know what I am doing wrong. I am extending Activity, at the start of the source code. Should I be extending AppCompatActivity?
public class Gvoice extends Activity implements OnClickListener{
ListView lv1;
static final int check = 1111;
Button b1;
Button b_home;
EditText a1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gvoice);
lv1 = (ListView)findViewById(R.id.LVGVoiceReturn);
b1 = (Button)findViewById(R.id.GVoice);
a1 = (EditText) findViewById(R.id.editTextHome);
b1.setOnClickListener(this);
//This now handles an automatic press of the bVoice button 1 second after the activity is opened
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
b1.callOnClick();
}
}, 1000);
}
public void onButtonClick(View v) {
if (v.getId() == R.id.BHome) {
String str = a1.getText().toString();
//Go to the relevant page if any part of the phrase or word entered in the 'EditText' field contains 'xxx' which is not case sensitive
if (str.toLowerCase().contains("home")) {
Intent userintent = new Intent(Gvoice.this, PocketSphinxActivity.class);
startActivity(userintent);
} else {
Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
}
}
}
public void onClick(View v){
Intent i1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i1.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Repeat Again");
startActivityForResult(i1, check);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == check && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
a1.setText((String) lv1.getItemAtPosition(0)); //Get the first phrase in the first row of list view
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
b_home.performClick();
}
}, 500); //Automatically click the 'Blogin' button after 500ms
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Update: Below is the xml file. Please note that onButtonClick has been added to the xml file but still it force closes the app when the button is clicked using the condition statement:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ececec">
<ImageView
android:layout_width="100dip"
android:layout_height="100dip"
android:background="#drawable/patient_two"
android:id="#+id/pimage"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Patient Name: Joe Blogs"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Cause of Injury: Car crash"
android:id="#+id/textView2"
android:layout_below="#+id/pimage"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Date of Birth:"
android:id="#+id/textView3"
android:layout_below="#+id/textView2"
android:layout_toStartOf="#+id/textView2"
android:layout_marginTop="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Gender:"
android:id="#+id/textView4"
android:layout_below="#+id/textView3"
android:layout_alignStart="#+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Occupation:"
android:id="#+id/textView5"
android:layout_below="#+id/textView4"
android:layout_alignStart="#+id/textView4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Address:"
android:id="#+id/textView6"
android:layout_below="#+id/textView5"
android:layout_alignStart="#+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medical History"
android:id="#+id/textView7"
android:layout_marginTop="15dp"
android:layout_below="#+id/textView6"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Heart attack"
android:id="#+id/textView8"
android:layout_marginTop="15dp"
android:layout_below="#+id/textView7"
android:layout_alignStart="#+id/textView6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Arthritis"
android:id="#+id/textView9"
android:layout_below="#+id/textView8"
android:layout_alignStart="#+id/textView8" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tests Completed"
android:id="#+id/textView10"
android:layout_marginTop="15dp"
android:layout_below="#+id/textView9"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="X-Ray"
android:id="#+id/textView11"
android:layout_below="#+id/textView10"
android:layout_alignStart="#+id/textView9"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="..."
android:id="#+id/textView12"
android:layout_below="#+id/textView11"
android:layout_alignStart="#+id/textView11" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tests Due"
android:id="#+id/textView14"
android:layout_below="#+id/textView12"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="..."
android:id="#+id/textView15"
android:layout_below="#+id/textView14"
android:layout_alignStart="#+id/textView12"
android:layout_marginTop="15dp" />
<ListView
android:layout_width="150dp"
android:layout_height="50dp"
android:id="#+id/lvVoiceReturn1"
android:textColor="#color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter"
android:id="#+id/Blogin1"
android:onClick="onButtonClick"
android:layout_alignParentBottom="true"
android:layout_toStartOf="#+id/bVoice1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak"
android:id="#+id/bVoice1"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/TFusername1"
android:layout_alignParentStart="true"
android:hint="Speech to Text" />
You can 4 method handle button click :
method 1 :
public class Mtest extends Activity {
Button b1;
public void onCreate(Bundle savedInstanceState) {
...
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(myhandler1);
...
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
}
};
}
method 2 :
class MTest extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
...
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(this);
...
}
#Override
public void onClick(View v) {
}
}
method 3 in xml and android:onClick="HandleClick" :
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="HandleClick" />
public class MTest extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void HandleClick(View view) {
}
}
method 4 :
public class MTest extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// do stuff
}
});
}
}
The issue was to do with initially not adding onButtonClick in the xml file and also not correctly assigning the button labels in the java file.
lv1 = (ListView)findViewById(R.id.LVGVoiceReturn);
b1 = (Button)findViewById(R.id.GVoice);
a1 = (EditText) findViewById(R.id.editTextHome);
c1 = (Button)findViewById(R.id.BHome);
b1.setOnClickListener(this);
The following corrects the issues and everything is working fine now. Hope this can help others in the future
The app executed just fine, and the interface loads just fine as well, but when I try to press any of the buttons, nothing that should happen happens. The point is fairly simple: press the '+' button, the quantity increases, and the price immediately updates to match said quantity, vice versa for the '-' button. I don't know what I've done wrong, but any button interaction with the app crashes it.
Here's my Java:
package com.t99sdevelopment.mobile.eyy;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public int quantityValue = 1;
public void increaseQuantity (View view) {
quantityValue = quantityValue + 1;
updateQuantityValue(view);
}
public void decreaseQuantity (View view) {
quantityValue = quantityValue - 1;
updateQuantityValue(view);
}
public void updateQuantityValue(View view) {
updateQuantity(quantityValue);
updatePrice(quantityValue);
}
private void updateQuantity(int number) {
TextView quantity = (TextView) findViewById(
R.id.quantityValue);
quantity.setText(number);
}
private void updatePrice(int number) {
TextView price = (TextView) findViewById(R.id.priceValue);
price.setText(NumberFormat.getCurrencyInstance().format(number * 5));
}
}
Here's my XML:
<TextView
android:text="Quantity"
android:id="#+id/quantityText"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:autoText="false"
android:paddingBottom="20dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal">
<TextView
android:text="0"
android:id="#+id/quantityValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/quantityValue"
android:layout_toStartOf="#+id/quantityValue"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:onClick="decreaseQuantity" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/quantityValue"
android:layout_toEndOf="#+id/quantityValue"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:onClick="increaseQuantity" />
</RelativeLayout>
<TextView
android:text="Price"
android:id="#+id/priceText"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:autoText="false"
android:paddingTop="20dp" />
<TextView
android:text="$0"
android:id="#+id/priceValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:paddingBottom="20dp" />
<Button
android:text="Order"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The formatting of the actual code is just fine, but it got screwy when I pasted in into stack, so I know that's not the problem.
Change you updateQuantity and updatePrice method like :
private void updateQuantity(int number) {
TextView quantity = (TextView) findViewById(
R.id.quantityValue);
quantity.setText(String.valueOf(number));
}
private void updatePrice(int number) {
TextView price = (TextView) findViewById(R.id.priceValue);
price.setText(String.valueOf(NumberFormat.getCurrencyInstance().format(number * 5)));
}
}
public class AddActivity extends Activity implements OnClickListener{
String[] info = new String[11];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView keyString = (TextView)findViewById(R.id.keyString);
TextView site1 = (TextView)findViewById(R.id.site1);
TextView site2 = (TextView)findViewById(R.id.site2);
TextView site3 = (TextView)findViewById(R.id.site3);
ImageButton submit = (ImageButton)findViewById(R.id.submit);
ImageButton add1 = (ImageButton)findViewById(R.id.add1);
ImageButton add2 = (ImageButton)findViewById(R.id.add2);
ImageButton add3 = (ImageButton)findViewById(R.id.add3);
submit.setOnClickListener((OnClickListener) this);
add1.setOnClickListener((OnClickListener) this);
add2.setOnClickListener((OnClickListener) this);
add3.setOnClickListener((OnClickListener) this);
int id = v.getId();
switch(id){
case R.id.submit:{
submitEntry(info);
break;
}
case R.id.add1:{
add2.setVisibility(View.VISIBLE);
site2.setVisibility(View.VISIBLE);
break;
}
}
}
}
This is the code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/key_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<EditText
android:id="#+id/keyString"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="invisible" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/site_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new" />
<EditText
android:id="#+id/site1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/url_hint"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new"
android:visibility="invisible" />
<EditText
android:id="#+id/site2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/url_hint"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="invisible" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new"
android:visibility="invisible" />
<EditText
android:id="#+id/site3"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/url_hint"
android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/submit_buttom" />
</LinearLayout>
</ScrollView>
</LinearLayout>
And this is the XML. The add1, add2, add3 and submit ImageButtons are all in a ScrollView.
When I press the add1 ImageButton, I want the add2 and site2 ImageButtons to become visible but instead, it throws the following error.
Motion event has invalid pointer count 0; value must be between 1 and 16.
What am I doing wrong?
PS: All the findViewById() calls are in the onClick() method because a NullPointerExeption is thrown if I call them in the onCreate().
Those findViewByIdcalls in onClickdon't make sense. Not sure why you are getting a null pointer exception calling them in onCreate.onClick is never called in this instance because nothing in the creation of the Activity is assigning the buttons to look at your onClick method; the buttons will default to having no listener assigned. It also doesn't look like a good idea to use the Activity as the onClickListener as well.
Your code should look something like this:
public class AddActivity extends Activity {
// https://source.android.com/source/code-style.html
// info -> mInfo; non-public, non-static field!
String[] mInfo = new String[11];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
TextView keyString = (TextView)findViewById(R.id.keyString);
TextView site1 = (TextView)findViewById(R.id.site1);
TextView site2 = (TextView)findViewById(R.id.site2);
TextView site3 = (TextView)findViewById(R.id.site3);
Button submit = (Button)findViewById(R.id.submit);
ImageButton add1 = (ImageButton)findViewById(R.id.add1);
ImageButton add2 = (ImageButton)findViewById(R.id.add2);
ImageButton add3 = (ImageButton)findViewById(R.id.add3);
add1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
add2.setVisibility(View.VISIBLE);
site2.setVisibility(View.VISIBLE);
}
});
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
submitEntry(mInfo);
}
});
}
The findViewById() calls and especially the setOnClickListener() calls should have been inside the onCreate(). With setOnClickListener() inside the onClick() i doubt the onClick was ever called.
We would need more logs to find the exact issue.
I have been stuck, regrettably, in a simple math problem.
I am creating a coffee application, as seen below, that takes the "Quantity" and multiplies it by the price to create a sub-total for each drink drink. It will then take all sub-totals from the drinks and add them together for the output at the bottom which would be updated automatically.
I have worked on the code to add or subtract from the 0 (I still can't figure out a way to prevent it from going below 0, if anyone has an idea, help is appreciated).
I have it so that the first 2 buttons (in pink) work right now for Espresso and Macchiato.
Here is my Java file
package com.example.cofeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class DrinkMenu extends Activity {
EditText quantity, quantity2, total;
Button button, plus1, minus1, plus2, minus2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();
// area for the espresso
plus1 = (Button) findViewById(R.id.button6);
minus1 = (Button) findViewById(R.id.button7);
quantity = (EditText) findViewById(R.id.editText2);
// area for the macchiato
plus2 = (Button) findViewById(R.id.button8);
minus2 = (Button) findViewById(R.id.button9);
quantity2 = (EditText) findViewById(R.id.editText4);
//subtotal for espresso
//subtotal for macchiato
total = (EditText) findViewById(R.id.editText9);
plus1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1+1;
quantity.setText(Integer.toString(inum1));
}
});//plus1 button
minus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1-1;
quantity.setText(Integer.toString(inum1));
}
});//minus1 button
plus2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2+1;
quantity2.setText(Integer.toString(inum2));
}
});//plus2 button
minus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
quantity2.setText(Integer.toString(inum2));
}
});//minus2 button
// Here is where I think I should place the sub-total multiplied by the prices
// and will be out put to the total = espresso_sub_total*espress_price +
// macchiato_sub_total*macchiato_price + and so on for the other drinks
}
}
Now, It may be imperative that I set my Total price to a Text View rather than Edit Text also, thoughts?
So after implementing ideas from both users #useruser3249477 and #Shobhit I have gotten the numbers to stop going below 0 and above 10, but then I tried to add the total together in the Total area of '0'. I have updated code below for both java and the xml file. It crashes as I press the '+' button.
updated Java source code:
package com.example.cofeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class DrinkMenu extends Activity {
EditText quantity, quantity2;
//TextView total;
Button button, plus1, minus1, plus2, minus2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();
// area for the espresso
plus1 = (Button) findViewById(R.id.button6);
minus1 = (Button) findViewById(R.id.button7);
quantity = (EditText) findViewById(R.id.editText2);
// area for the macchiato
plus2 = (Button) findViewById(R.id.button8);
minus2 = (Button) findViewById(R.id.button9);
quantity2 = (EditText) findViewById(R.id.editText4);
//espresso-sub-total
//macchiato-sub-total
//total = (TextView) findViewById(R.id.textView7);
plus1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1+1;
if (inum1 > 10) return;
quantity.setText(Integer.toString(inum1));
}
});//plus1 button
minus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1-1;
if (inum1 < 0) return;
quantity.setText(Integer.toString(inum1));
}
});//minus2 button
plus2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2+1;
if (inum2 > 10) return;
quantity2.setText(Integer.toString(inum2));
}
});//plus1 buttons
minus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
if (inum2 < 0) return;
quantity2.setText(Integer.toString(inum2));
}
});
// double subtotal = Double.parseDouble(numb1);
// Here is where I think I should place the sub-total multiplied by the prices
// and will be out put to the total = num1*3;
final TextView total = (TextView) findViewById(R.id.textView7);
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Remove previous price of these items
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal - count*3;
total.setText(newTotal);
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Add the new items price
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal + count*3;
total.setText(newTotal);
}
#Override
public void afterTextChanged(Editable editable) {}
};
quantity.addTextChangedListener(textWatcher);
quantity2.addTextChangedListener(textWatcher);
}
//order button code that is useless to this question.
}
Here is my updated XML file that is updated:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button"
android:layout_marginRight="75dp"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2"
android:background="#FFFFFF"
android:text="Drinks:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/orderbtn"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/orderbtn"
android:background="#FFFFFF"
android:text="Quantity:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="24dp"
android:textStyle="bold" />
<Button
android:id="#+id/button"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Espresso"
android:textSize="24dp" />
<Button
android:id="#+id/button2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Macchiato"
android:textSize="24dp" />
<Button
android:id="#+id/button3"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button2"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Con Panna"
android:textSize="24dp" />
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button4"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Latte"
android:textSize="24dp" />
<Button
android:id="#+id/button6"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/button6"
android:layout_toStartOf="#+id/button6"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<Button
android:id="#+id/button7"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/editText2"
android:layout_toStartOf="#+id/editText2"
android:background="#ffff45df"
android:text="-"
android:textSize="24dp" />
<Button
android:id="#+id/button8"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button2"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<EditText
android:id="#+id/editText3"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8"
android:layout_toStartOf="#+id/button8"
android:background="#FFFFFF"
android:ems="10"
android:inputType="number"
android:text=" 0"
android:textSize="24dp" />
<EditText
android:id="#+id/editText4"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button8"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8"
android:layout_toStartOf="#+id/button8"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24sp" />
<Button
android:id="#+id/button9"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/editText3"
android:layout_toStartOf="#+id/editText3"
android:background="#ffff45df"
android:text="-"
android:textSize="24dp" />
<Button
android:id="#+id/button10"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/button8"
android:layout_alignRight="#+id/button8"
android:layout_alignTop="#+id/button3"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<Button
android:id="#+id/button11"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button4"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<Button
android:id="#+id/button12"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button5"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<EditText
android:id="#+id/editText5"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_above="#+id/button4"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/button10"
android:layout_toStartOf="#+id/button10"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<EditText
android:id="#+id/editText6"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button11"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/button11"
android:layout_toStartOf="#+id/button11"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<EditText
android:id="#+id/editText7"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button12"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/button12"
android:layout_toStartOf="#+id/button12"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<Button
android:id="#+id/button14"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/editText6"
android:layout_toStartOf="#+id/editText6"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />
<Button
android:id="#+id/button15"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/editText7"
android:layout_toStartOf="#+id/editText7"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />
<Button
android:id="#+id/button16"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/editText5"
android:layout_toStartOf="#+id/editText5"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button9"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button16"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button14"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button15"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="#+id/button4"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button3"
android:layout_marginTop="32dp"
android:background="#FFFFFF"
android:text="Americano"
android:textSize="24dp"
android:textColor="#000000" />
<Button
android:id="#+id/orderbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#drawable/buttonround"
android:text="Order"
android:textSize="24sp" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/orderbtn"
android:layout_alignBottom="#+id/orderbtn"
android:layout_alignParentLeft="true"
android:background="#FFFFFF"
android:text="Total: $"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView8"
android:layout_alignBottom="#+id/textView8"
android:layout_alignLeft="#+id/textView"
android:layout_alignRight="#+id/textView6"
android:layout_alignStart="#+id/textView"
android:background="#FFFFFF"
android:text="0"
android:textSize="24sp" />
</RelativeLayout>
Here are some of the errors coming out.
E/AndroidRuntime(369): FATAL EXCEPTION: main
E/AndroidRuntime(369): android.content.res.Resources$NotFoundException: String resource ID #0x0// seems to be here
E/AndroidRuntime(369): at android.content.res.Resources.getText(Resources.java:201)
A simple check will prevent negative values (using the buttons):
minus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
if (inum2 < 0) return;
quantity2.setText(Integer.toString(inum2));
}
});
To prevent manually entering negative values, you can set android:digits="0123456789" as #ShobhitPuri suggested.
Then for the total you'll need to set TextWatchers:
final EditText total = (EditText) findViewById(R.id.editText9);
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Remove previous price of these items
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal - count*3;
total.setText(newTotal);
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Add the new items price
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal + count*3;
total.setText(newTotal);
}
#Override
public void afterTextChanged(Editable editable) {}
};
// Now set the TextWatcher on every count EditText
// If you have different prices, you'll need multiple TextWatchers
quantity1.addTextChangedListener(textWatcher);
quantity2.addTextChangedListener(textWatcher);
...
"I still can't figure out a way to prevent it from going below 0"
One way is you can add android:digits="0123456789" to your EditText's in xml file. This will prevent user from entering anything but these numbers.
One other way is to do a check in the onClickListener. In this you'll need to check for all the EditText's values using ed.getText() and then check if its integer and is in the range acceptable by you.
One more way is to use addTextChangedListener on EditText. You can listen you what is being inputted there. You can make a check for condition when its less than 0 or an invalid character has been put in. (Its good to limit the keyboard so as to prevent garbage entry at the first place).