Checklist log error - android

There is an error in my code, that I don't understand, where when a checkbox is ticked the application closes.
A brief summary of what I'm trying to do. When the check button is pressed, it tries to see what checklists have been checked and than add 1 to score if it is checked. I than to output text depending on the score received.
If there are any other mistakes I've made please point them out. Also include code example(Makes it easier to understand).
04-06 06:15:44.601 20298-20298/xyz.ashraf.whoisdelasalle E/AndroidRuntime: FATAL EXCEPTION: main
Process: xyz.ashraf.whoisdelasalle, PID: 20298
java.lang.IllegalStateException: Could not find method onCheckboxClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.CheckBox with id 'concern'
at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4485)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4449)
at android.view.View.performClick(View.java:5204)
at android.widget.CompoundButton.performClick(CompoundButton.java:122)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Error code^^
package xyz.ashraf.whoisdelasalle;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
* Created by Ashraf on 3/2/2016.
*/
public class check_Button extends Pop_sallian {
// Connects The variable to an xml id
TextView output = (TextView) findViewById(R.id.output);
//sets the variable to 0
int score = 0;
public void onCheckboxClicked(View v){
boolean checked = ((CheckBox) v).isChecked();
// checks what checklists have been checked
switch (v.getId()) {
case R.id.concern:
if (checked) {
score += 1;
}
break;
case R.id.faith:
if (checked) {
score += 1;
}
break;
case R.id.respect:
if (checked) {
score += 1;
}
break;
case R.id.education:
if (checked) {
score += 1;
}
break;
case R.id.community:
if (checked) {
score += 1;
}
break;
}
// adds the variables together to form a score
if (score == 0) {
output.setText("Come on! Get involved, your la sallian community needs you.");
} else if (score == 1) {
output.setText("Good start, keep going!");
} else if (score == 2) {
output.setText("Room to improve but doing good!");
} else if (score == 3) {
output.setText("Very good, others look up to you!");
} else if (score == 4) {
output.setText("Wow, you really are an inspiration");
} else if (score == 5) {
output.setText("Excellent! You're a leader in your la sallian community");
} else {
output.setText("Unknown");
}
// changes the output text based on score value
}
}
^^Code that checks the checklist and than outputs text^^
package xyz.ashraf.whoisdelasalle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
/**
* Created by Ashraf on 1/27/2016.
*/
public class Pop_sallian extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow_sallian);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.6));
//Creates a pop up window where the checklists are stored
Button checkButton = (Button) findViewById(R.id.check);
checkButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Pop_sallian.this, check_Button.class));
}
}); // runs the check_Button.java to check what checklists have been checked.
Button okButton = (Button) findViewById(R.id.okButton_sallian);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});// closes the pop up window
}
}
^^ Code that contains the check button(To check if the checklists are checked and output text depending)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you a Sallian?"
android:id="#+id/textView7"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you meet the following prerequisites, if you do you may be a Sallian"
android:id="#+id/textView8"
android:layout_below="#+id/textView7"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you concerened for the poor and Social Justice?"
android:id="#+id/concern"
android:textSize="18sp"
android:layout_below="#+id/textView8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have faith in the presence of God?"
android:id="#+id/faith"
android:textSize="15sp"
android:layout_below="#+id/concern"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have Respect for all people?"
android:id="#+id/respect"
android:layout_below="#+id/faith"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:textSize="15sp"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you value education?"
android:id="#+id/education"
android:textSize="15sp"
android:layout_below="#+id/respect"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you inclusive in your community?"
android:id="#+id/community"
android:layout_below="#+id/education"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="15sp"
android:checked="false"
android:onClick="onCheckboxClicked"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="#+id/okButton_sallian"
android:layout_below="#+id/community"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:layout_marginTop="90dp"
android:layout_marginBottom="20dp"
android:background="#FAFAFA"
android:textColor="#00E676"
android:elevation="2dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:id="#+id/check"
android:textColor="#00E676"
android:elevation="2dp"
android:background="#FAFAFA"
android:layout_alignTop="#+id/okButton_sallian"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/output"
android:textColor="#1eff00"
android:textSize="20sp"
android:layout_below="#+id/community"
android:layout_centerHorizontal="true"
android:layout_above="#+id/check"
android:textIsSelectable="false" />
</RelativeLayout>
</ScrollView>
^^ pop up xml code^^

You should set your launcher in your manifest as check_Button instead of the default, which is probably Pop_sallian.
<activity
android:name=".check_Button"
<!--stuff -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Related

How to indicate an error if an user inputs letters into the edit text for age? (Android Studio)

I am trying to make an app which intakes student details. Part of that detail is the students age which is supposed to be only a number input. If letters are entered into the edit text field, the app must indicate an error.
I have the coding below, however it seems like there are no changes to the app as it still crashes whenever I enter a letter for age.
//xml content
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="40dp"
android:background="#F0F8FF"
>
<TableLayout
android:id="#+id/add_table"
android:layout_width="match_parent"
android:layout_height="606dp"
android:paddingTop="40dp">
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:padding="3dip"
android:text="Student ID:" />
<EditText
android:id="#+id/sid"
android:layout_width="190dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:padding="3dip"
android:text="First Name:" />
<EditText
android:id="#+id/fn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="150dip" />
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:padding="3dip"
android:text="Last Name:" />
<EditText
android:id="#+id/ln"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="150dip" />
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:padding="3dip"
android:layout_marginTop="5dp"
android:text="Gender:" />
<RadioGroup
android:id="#+id/ge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.9"
android:scaleY="0.9"
android:text="Male" />
<RadioButton
android:id="#+id/female"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleX="0.9"
android:scaleY="0.9"
android:text="Female" />
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:padding="3dip"
android:text="Course Study:" />
<EditText
android:id="#+id/cs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="150dip" />
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:inputType="number"
android:digits="0123456789"
android:padding="3dip"
android:text="Age:" />
<EditText
android:id="#+id/ag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="150dip" />
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="25dp"
android:padding="3dip"
android:text="Address:" />
<EditText
android:id="#+id/ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="150dip" />
</TableRow>
<Button
android:id="#+id/add_button"
android:layout_width="207dp"
android:layout_height="wrap_content"
android:layout_marginLeft="118dp"
android:layout_marginRight="52dp"
android:layout_marginTop="14dp"
android:padding="6dip"
android:text="Add Student" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
app:srcCompat="#mipmap/man" />
</TableLayout>
//Java
package com.user.project3;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
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.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Addrecord extends AppCompatActivity {
DatabaseManager myDb;
EditText sid, fn, ln, cs, ag, ad;
RadioGroup radioGenderGroup;
RadioButton radioGenderButton;
Button btnAddStudent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
myDb = new DatabaseManager(this);
sid = (EditText)findViewById(R.id.sid);
fn = (EditText)findViewById(R.id.fn);
ln = (EditText)findViewById(R.id.ln);
cs = (EditText)findViewById(R.id.cs);
ag = (EditText)findViewById(R.id.ag);
ad = (EditText)findViewById(R.id.ad);
btnAddStudent = (Button)findViewById(R.id.add_button);
AddStudentRecord();
}
public void AddStudentRecord() {
btnAddStudent.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
radioGenderGroup = (RadioGroup) findViewById(R.id.ge);
int selectedid = radioGenderGroup.getCheckedRadioButtonId();
radioGenderButton = (RadioButton) findViewById(selectedid);
boolean isInserted = myDb.insertDataStudent(
Integer.parseInt(sid.getText().toString()),
fn.getText().toString(),
ln.getText().toString(),
radioGenderButton.getText().toString(),
cs.getText().toString(),
Integer.parseInt(ag.getText().toString()),
ad.getText().toString()
);
String strNumber=ag.getText().toString().trim();
if(TextUtils.isEmpty(strNumber) || Integer.parseInt(strNumber)>100){
Toast.makeText(Addrecord.this,"Please input a number",Toast.LENGTH_LONG).show();
}
if(isInserted == true)
Toast.makeText(Addrecord.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(Addrecord.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.screen2_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.home2) {
Intent intent = new Intent(Addrecord.this, Home.class);
startActivity(intent);
return true;
}
if (id == R.id.viewarecord) {
Intent intent = new Intent(Addrecord.this, Viewstudent.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
//crash log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.supriya.project3, PID: 2274
java.lang.NumberFormatException: For input string: "j"
at java.lang.Integer.parseInt(Integer.java:608)
at java.lang.Integer.parseInt(Integer.java:643)
at
com.supriya.project3.Addrecord$1.onClick(Addrecord.java:72)
at android.view.View.performClick(View.java:6891)
at
android.widget.TextView.performClick(TextView.java:12651)
at
android.view.View$PerformClick.run(View.java:26083)
at
android.os.Handler.handleCallback(Handler.java:789)
at
android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at
android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Application terminated.
Try this
youredittext.addTextChangedListener(new TextWatcher()
{
#Override
public void afterTextChanged(Editable mEdit)
{
String regexStr = "^[0-9]*$";
if(your_editText.getText().toString().trim().matches(regexStr))
{
//write code here for success
}
else{
// write code here on failure
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
let me know if this is working or not
You can use android:inputType="number" . If you also want to filter the number that user input, you can addTextChangeListener.
String strNumber = ag.getText().toString().trim();
if(TextUtils.isEmpty(strNumber)){
Toast.makeText(Addrecord.this,"Please input a number",Toast.LENGTH_LONG).show();
}else{
String regexStr = "^[0-9]*$";
if(your_editText.getText().toString().trim().matches(regexStr))
{
//write code here for success
your_editText.setError(null) // to clear any errors
}
else{
your_editText.setError(“please insert numbers only”);
}
}
}
Use this regex to find if a string has numbers or not:
Pattern pattern = Pattern.compile(".*[^0-9].*");
pattern.matcher(you input in edittext).matches());

Lots of errors in my app

Sorry for the unspecific title. So, I was working on an app called JustJava and I just finished installing the whipped cream checkbox and the corresponding code. I had it logged and for some reason it didn’t show up. Instead, it gave me errors like:
Could not load memetrack module
Failed to get memory consumption info: -1
No default activity
Also, the app would crash before once every time I ran it, right at the start, and I'd have to run the app again. Obviously the whipped cream checkbox did not update anything. I have looked through the code multiple times and searched on the internet a while, but I could not find anything. Could someone please take a look at my code and see if they can see the issue?
Thanks so much!
XML layout code
<EditText
android:id="#+id/name_input"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="Enter Your Name Here"
android:layout_marginBottom="16dp"
android:inputType="text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toppings"
android:layout_marginBottom="16dp"
android:textAllCaps="true"
android:textSize="20sp"/>
<CheckBox
android:id="#+id/whipped_cream_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Whipped cream"
android:paddingLeft="24dp"
android:textSize="16sp"
android:textAppearance="?android:textAppearanceMedium" />
<CheckBox
android:id="#+id/chocolate_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chocolate"
android:paddingLeft="24dp"
android:textSize="16sp"
android:textAppearance="?android:textAppearanceMedium"
android:layout_marginBottom="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Quantity"
android:textAllCaps="true"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="decrement"
android:text="-" />
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="0"
android:textColor="#android:color/black"
android:textSize="16sp" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="increment"
android:text="+" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Order summary"
android:textSize="20sp"
android:textAllCaps="true" />
<TextView
android:id="#+id/order_summary_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Free"
android:textColor="#android:color/black"
android:textSize="16sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="submitOrder"
android:text="Order" />
MainActivity Code
package com.example.android.justjava3;
import android.icu.text.NumberFormat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import com.example.android.justjava.R;
/**
This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
CheckBox whippedCreamCheckBox = (CheckBox)
findViewById(R.id.whipped_cream_checkbox);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
Log.v(“MainActivity”, “Has whipped cream?”+hasWhippedCream);
int price=calculatePrice();
String priceMessage=createOrderSummary(price, hasWhippedCream);
displayMessage(priceMessage);
}
/**
Calculates the price of the order.
#total price
*/
private int calculatePrice() {
int price = quantity * 5;
return price;
}
private String createOrderSummary(int price, boolean addWhippedCream){
String priceMessage=“The total price for " + quantity + " cups of coffee is
$” + price;
priceMessage +="\n Customer name: Kaptain Kunal";
priceMessage +="\n Whipped cream topping: " + addWhippedCream;
String freeMessage=“Free”;
displayMessage(freeMessage);
priceMessage+="\n Number of coffees ordered: "+quantity;
return priceMessage;
}
/**
This method displays the given quantity value on the screen.
/
private void displayQuantity(int number) {
TextView quantityTextView = (TextView)
findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
/*
This method displays the given price on the screen.
*/
private void displayMessage(String message) {
TextView orderSummaryTextView = (TextView)
findViewById(R.id.order_summary_text_view);
orderSummaryTextView.setText
(NumberFormat.getCurrencyInstance().format(message));
}
public void increment (View view){
quantity=(quantity+1);
displayQuantity(quantity);
}
public void decrement (View view){
quantity=(quantity-1);
displayQuantity(quantity);
}
}
It seems like you forgot to define the activity in Manifest.xml. Try it once like this
<application>
<activity
android:name="ActivityName"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
Due Limited details about the issue in question, if solution won't work then add Error stackTrace along with question.

Setting an event for a button

I've asked a question similar to this. The point of this code is to check if a checklist is checked, if it is than it adds 1 to the score.
I've been told that the problem is that I haven't set an event. Please explain what I need to do to make it work, I'm new to android development and have been stuck on this for a few days. Please give a code example with your answer.
package xyz.ashraf.whoisdelasalle;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
* Created by Ashraf on 3/2/2016.
*/
public class check_Button extends Pop_sallian{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow_sallian);
// Connects The variable to an xml id
TextView output = (TextView) findViewById(R.id.output);
final int[] score = {0};
//sets the variable to 0
OnCheckedChangeListener checkedListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.concern:
score[0]++;
break;
case R.id.faith:
score[0]++;
break;
case R.id.respect:
score[0]++;
break;
case R.id.education:
score[0]++;
break;
case R.id.community:
score[0]++;
break;
}
}
};
// adds the variables together to form a score
if(score[0] == 0){
output.setText("Come on! Get involved, your la sallian community needs you.");
} else if(score[0] == 1){
output.setText("Good start, keep going!");
} else if(score[0] == 2){
output.setText("Room to improve but doing good!");
} else if(score[0] == 3){
output.setText("Very good, others look up to you!");
} else if(score[0] == 4){
output.setText("Wow, you really are an inspiration");
} else if(score[0] == 5){
output.setText("Excellent! You're a leader in your la sallian community. Nice work!");
} else{
output.setText("Unknown");
}
// changes the output text based on score value
}
}
^^checklist code^^
package xyz.ashraf.whoisdelasalle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.who);
Button today = (Button) findViewById(R.id.today);
Button sallian = (Button) findViewById(R.id.sallian);
Button how = (Button) findViewById(R.id.toBe);
Button moreInfo = (Button) findViewById(R.id.info);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop.class));
}
});
today.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_today.class));
}
});
sallian.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_sallian.class));
}
});
how.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_how.class));
}
});
moreInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Pop_info.class));
}
});
}
}
^^Code where the button is and, when pressed calls on the checklist code, The Ok button is to close the pop up when pressed^^
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent" android:layout_height="match_parent">
android:elevation="8dp"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you a Sallian?"
android:id="#+id/textView7"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you meet the following prerequisites, if you do you may be a Sallian"
android:id="#+id/textView8"
android:layout_below="#+id/textView7"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you concerened for the poor and Social Justice?"
android:id="#+id/concern"
android:textSize="18sp"
android:layout_below="#+id/textView8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have faith in the presence of God?"
android:id="#+id/faith"
android:textSize="15sp"
android:layout_below="#+id/concern"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have Respect for all people?"
android:id="#+id/respect"
android:layout_below="#+id/faith"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:textSize="15sp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you value education?"
android:id="#+id/education"
android:textSize="15sp"
android:layout_below="#+id/respect"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you inclusive in your community?"
android:id="#+id/community"
android:layout_below="#+id/education"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="15sp"
android:checked="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="#+id/okButton_sallian"
android:layout_below="#+id/community"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:layout_marginTop="90dp"
android:layout_marginBottom="20dp"
android:background="#FAFAFA"
android:textColor="#00E676"
android:elevation="2dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:id="#+id/check"
android:textColor="#00E676"
android:elevation="2dp"
android:background="#FAFAFA"
android:layout_alignTop="#+id/okButton_sallian"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/output"
android:textColor="#1eff00"
android:textSize="23sp"
android:layout_below="#+id/community"
android:layout_centerHorizontal="true"
android:layout_above="#+id/check" />
</RelativeLayout>
</ScrollView>
^^ xml code the code takes for^^
You are defining a checked listener, but doing nothing with it. You need to set it to your checkbox or checkboxes.
mCheckBox = (CheckBox) findViewById(R.id.concern);
mCheckBox.setOnCheckedChangedListener(checkedListener);
Just like you do on your buttons.

Error with the if and else statement

I am getting 3 different errors: identifier expected, unexpected token, unknown class: 'score'. These error's are in line 57-69.
The point of this code is to check if a checklist is checked and, if so, add 1 to score. It than changes the output text to a different string depending on score.
package xyz.ashraf.whoisdelasalle;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
* Created by Ashraf on 3/2/2016.
*/
public class check_Button extends Pop_sallian{
// Connects The variable to an xml id
TextView output = (TextView) findViewById(R.id.output);
//sets the variable to 0
int score = 0;
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch(view.getId()){
case R.id.concern:
if(checked) {
score += 1;
}
break;
case R.id.faith:
if(checked){
score+=1;
}
break;
case R.id.respect:
if(checked){
score+=1;
}
break;
case R.id.education:
if(checked){
score+=1;
}
break;
case R.id.community:
if(checked){
score+=1;
}
break;
}
}
// adds the variables together to form a score
if(score == 0){
output.setText("Come on! Get involved, your la sallian community needs you.");
} else if( score == 1){
output.setText("Good start, keep going!");
} else if( score == 2){
output.setText("Room to improve but doing good!");
} else if(score == 3){
output.setText("Very good, others look up to you!");
} else if(score == 4){
output.setText("Wow, you really are an inspiration");
} else if(score == 5){
output.setText("Excellent! You're a leader in your la sallian community");
} else{
output.setText("Unknown");
}
// changes the output text based on score value
}
^^ Code where the error is located^^
package xyz.ashraf.whoisdelasalle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
/**
* Created by Ashraf on 1/27/2016.
*/
public class Pop_sallian extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow_sallian);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.6));
Button checkButton = (Button) findViewById(R.id.check);
checkButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Pop_sallian.this, check_Button.class));
}
});
Button okButton = (Button) findViewById(R.id.okButton_sallian);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
^^ Code where the pop up screen is launched and where the check button is^^
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you a Sallian?"
android:id="#+id/textView7"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you meet the following prerequisites, if you do you may be a Sallian"
android:id="#+id/textView8"
android:layout_below="#+id/textView7"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:textColor="#000000" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you concerened for the poor and Social Justice?"
android:id="#+id/concern"
android:textSize="18sp"
android:layout_below="#+id/textView8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have faith in the presence of God?"
android:id="#+id/faith"
android:textSize="15sp"
android:layout_below="#+id/concern"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you have Respect for all people?"
android:id="#+id/respect"
android:layout_below="#+id/faith"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:textSize="15sp"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you value education?"
android:id="#+id/education"
android:textSize="15sp"
android:layout_below="#+id/respect"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you inclusive in your community?"
android:id="#+id/community"
android:layout_below="#+id/education"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="15sp"
android:checked="false"
android:onClick="onCheckboxClicked"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="#+id/okButton_sallian"
android:layout_below="#+id/community"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:layout_marginTop="90dp"
android:layout_marginBottom="20dp"
android:background="#FAFAFA"
android:textColor="#00E676"
android:elevation="2dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:id="#+id/check"
android:textColor="#00E676"
android:elevation="2dp"
android:background="#FAFAFA"
android:layout_alignTop="#+id/okButton_sallian"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/output"
android:textColor="#1eff00"
android:textSize="20sp"
android:layout_below="#+id/community"
android:layout_centerHorizontal="true"
android:layout_above="#+id/check"
android:textIsSelectable="false" />
</RelativeLayout>
</ScrollView>
^^XML code^^
You have the closing curly bracket } in the wrong place. The function was ending before the if.
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch(view.getId()){
case R.id.concern:
if(checked) {
score += 1;
}
break;
case R.id.faith:
if(checked){
score+=1;
}
break;
case R.id.respect:
if(checked){
score+=1;
}
break;
case R.id.education:
if(checked){
score+=1;
}
break;
case R.id.community:
if(checked){
score+=1;
}
break;
}
// adds the variables together to form a score
//} <-- REMOVE THIS CURLY BRACKET
if(score == 0){
output.setText("Come on! Get involved, your la sallian community needs you.");
} else if( score == 1){
output.setText("Good start, keep going!");
} else if( score == 2){
output.setText("Room to improve but doing good!");
} else if(score == 3){
output.setText("Very good, others look up to you!");
} else if(score == 4){
output.setText("Wow, you really are an inspiration");
} else if(score == 5){
output.setText("Excellent! You're a leader in your la sallian community");
} else{
output.setText("Unknown");
}
// changes the output text based on score value
} // <-- MOVE THE CURLY BRACKET HERE

Android : Managing two buttons

I have a problem concerning setting a listener that would execute different action based on the clicked button.
The compiler doesn't detect any error on the syntax, but the problem is that when I simulate my app on the emulator, it pops up the window asking for "Force Close".
Here is my Java file :
package tp.imc.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class IMCCalculatorActivity extends Activity {
EditText poids,taille;
Button boutton1,boutton2;
TextView text1;
Boolean k=true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
poids=(EditText) findViewById(R.id.poids);
taille=(EditText) findViewById(R.id.taille);
boutton1 =(Button) findViewById(R.id.boutton1);
text1=(TextView) findViewById(R.id.text1);
boutton1.setOnClickListener(clicker);
boutton2.setOnClickListener(clicker);
}
// declare a OnClickListener that will execute different actions
// depending on the view that was clicked
View.OnClickListener clicker = new View.OnClickListener(){
public void onClick (View v){
if( v == boutton1 )
{
String a,b;
Double r;
a=poids.getText().toString();
b=taille.getText().toString();
Double zero=Double.parseDouble(b);
a=poids.getText().toString();
b=taille.getText().toString();
if (zero==0)
{
text1.setText("Erreur ! Division par 0.");
}
else {
r=(Double.parseDouble(a))/(Double.parseDouble(b)*Double.parseDouble(b));
if (r<16.5) text1.setText("Famine.");
else if (r>16.5 && r<18.5) text1.setText("Maigreur.");
else if (r>=18.5 && r<25) text1.setText("Coplulence nomrale.");
else if (r>=25 && r<30) text1.setText("Surpoids.");
else if (r>=30 && r<35) text1.setText("Obésité modérée.");
else if (r>=35 && r<40) text1.setText("Obésité sévère.");
else if (r>=40) text1.setText("Obésité morbide ou massive.");
}
}
else if( v == boutton2 )
{
poids.setText("");
taille.setText("");
text1.setText("");
}
}
};
}
And here is my XML file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/poids"
android:textColor="#FF0000"
android:gravity="center"
/>
<EditText
android:id="#+id/poids"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/poids"
android:lines="1"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/taille"
android:textColor="#FF0000"
android:gravity="center"
/>
<EditText
android:id="#+id/taille"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/taille"
android:lines="1"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/metre"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/centimetre"
/>
</RadioGroup>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mega"
android:checked="false"
/>
<Button
android:id="#+id/boutton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/compute" />
<Button
android:id="#+id/boutton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/raz" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/resultat"
/>
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/text"
/>
</LinearLayout>
You don't define boutton2, it is null. Add this:
boutton2 = (Button) findViewById(R.id.boutton2);
boutton2.setOnClickListener(clicker);
Since boutton1 and boutton2 don't have common code between them you could simply create two different listeners.
Lastly, this line doesn't look right.
if( v == boutton1 )
You are trying to compare a View to a Button which may not work. Try this:
if( v.getId() == boutton1.getId() )

Categories

Resources