Setting an event for a button - android

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.

Related

Checklist log error

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>

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

Simple calculator application in Android

When I click on the add button without entring anything on the edittext,unfortunately calculator has stopped.
package com.example.calculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Calci extends Activity {
TextView t1;
EditText e1,e2;
Button add,sub,mul,div;
Context c;;
String b,a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calci);
e1=(EditText) findViewById(R.id.EditText01);
e2=(EditText) findViewById(R.id.EditText02);
add=(Button) findViewById(R.id.add);
sub=(Button) findViewById(R.id.sub);
mul=(Button) findViewById(R.id.mul);
div=(Button) findViewById(R.id.div);
t1=(TextView) findViewById(R.id.textView1);
a=e1.getText().toString();
b=e2.getText().toString();
}
public void doSomething(View v){
if(v.getId()==R.id.add){
if (a==null & b==null){
Toast.makeText(c,"PLEASE ENTER SOMETHING", Toast.LENGTH_LONG).show();
}
else{
int result=Integer.parseInt(a) + Integer.parseInt(b);
t1.setText(Integer.toString(result));
}
}
}
}
When I click on the add button,unfortunately calculator has stopped.
XML FILE
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Calci" >
<EditText
android:id="#+id/EditText01"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="number" />
<EditText
android:id="#+id/EditText02"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/EditText01"
android:ems="10"
android:inputType="number" />
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/EditText01"
android:layout_marginTop="20dp"
android:text="ADD"
android:onClick="doSomething"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/button4"
android:layout_marginTop="44dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/add"
android:onClick="doSomething"
android:text="SUBTRACT" />
<Button
android:id="#+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/sub"
android:onClick="doSomething"
android:text="MULTIPLY" />
<Button
android:id="#+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/mul"
android:onClick="doSomething"
android:text="DIVIDE" />
</RelativeLayout>
When I click on the add button,unfortunately calculator has stopped.\
what shuold i do now
First of all initialize
t1=(TextView) findViewById(R.id.textView1);
And you can put validation for empty check on button click listener before furthe process like
if(!(t1.getText.toString().equals(""))) to show alerts
I think problem is that you have not declared your text-view, try to declare it like below and then try.
t1=(TextView) findViewById(R.id.textView1);
Edit
if (e1.getText.toString().trim() == "" && e2.getText.toString().trim() == "")
{
// alert
}
else
{
// do your code
}
So your final code would be
public void doSomething(View v){
if(v.getId()==R.id.add){
a=e1.getText().toString();
b=e2.getText().toString();
if (a == "" && b == "")
{
// alert
}
else
{
int result=Integer.parseInt(a) + Integer.parseInt(b);
t1.setText(Integer.toString(result));
}
}
}
At first you have mistake in your function - you should call e1.setText(), not t1, t1 undeclared and not initialized. And next you should use OnClickListener like this:
public class Calci extends Activity {
TextView t1;
EditText e1,e2;
Button add,sub,mul,div;
String b,a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calci);
e1=(EditText) findViewById(R.id.EditText01);
e2=(EditText) findViewById(R.id.EditText02);
add=(Button) findViewById(R.id.add);
sub=(Button) findViewById(R.id.sub);
mul=(Button) findViewById(R.id.mul);
div=(Button) findViewById(R.id.div);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
a=e1.getText().toString();
b=e2.getText().toString();
int result=Integer.parseInt(a) + Integer.parseInt(b);
e1.setText(Integer.toString(result));
}
});
}
}
And yes, check the textfield for not empty String.
if((!a.isEmpty())&(!b.isEmpty())) { your code }
And the best way - use try-catch for catching FormatNumberException
try {
int result=Integer.parseInt(a) + Integer.parseInt(b);
e1.setText(Integer.toString(result));
}
catch (FormatNumberException e) {
Log.d("Exception", e.getMessage());
}
So you can prevent all crashes.

Linear layout not clickable after first layout

Linear Layout
I am trying to create a LinearLayout on the main screen to act as a menu. I didn't want Buttons or have to create images, I like the look I get with the TextViews and Layouts. My problem is I cannot get the onClick() to work past the first layout, its been driving me nuts the past couple days. Is there something small I'm missing or am I doing it all wrong?
Main.Java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout computer = (LinearLayout) findViewById(R.id.main_btn_computer);
LinearLayout networking = (LinearLayout) findViewById(R.id.main_btn_networking);
computer.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
//Call your method here
startActivity (new Intent(getApplicationContext(), ComputerMain.class));
}
});
networking.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
//Call your method here
startActivity (new Intent(getApplicationContext(), NetworkingMain.class));
}
});
}
}
}
Main.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="6dip" >
<LinearLayout
android:id="#+id/main_btn_computer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:background="#drawable/border"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:weightSum="5" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Operating Systems"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/light_blue" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Windows, Linux, Dos, and Mac"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/grey_font" />
</LinearLayout>
<LinearLayout
android:id="#+id/main_btn_networking"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#drawable/border"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:weightSum="10" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Networking"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/light_blue" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cisco, HP Procurve"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/grey_font" />
</LinearLayout>
</LinearLayout>
Why are using onClickFeature(View v) it's onClick(View v).
are setting your onClickListener()?
Call your LinearLayout
LinearLayout computer = (LinearLayout) findViewById(R.id.main_btn_computer);
LinearLayout networking = (LinearLayout) findViewById(R.id.main_btn_networking);
Set the onClick Listeners
computer.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
//Call your method here
startActivity (new Intent(getApplicationContext(), ComputerMain.class));
}
});
networking.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
//Call your method here
startActivity (new Intent(getApplicationContext(), NetworkingMain.class));
}
});
Remove the android:onClick="" attributes

"Source not found" error in Eclipse

Developing my first app for android. Works OK on emulator, but on phone I get a "Source not found error" ViewRoot.handleMessage(Message)line:1757. I get the error when I press button number 4 on the application and try to display my media images on phone.
Code:
package com.example.famiily_connect_v1;
import android.app.Activity;
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;
import android.widget.TextView;
public class family_connect extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button incrementButton, decrementButton, makecallButton,
media_cameraButton;
TextView numberdisplayTextView;
EditText inputEditText;
int A = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
incrementButton = (Button) findViewById(R.id.Button01);
decrementButton = (Button) findViewById(R.id.Button02);
makecallButton = (Button) findViewById(R.id.Button03);
media_cameraButton = (Button) findViewById(R.id.Button04);
numberdisplayTextView = (TextView) findViewById(R.id.TextView01);
inputEditText = (EditText) findViewById(R.id.EditText01);
incrementButton.setOnClickListener(this);
decrementButton.setOnClickListener(this);
makecallButton.setOnClickListener(this);
media_cameraButton.setOnClickListener(this);
numberdisplayTextView.setOnClickListener(this);
inputEditText.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.Button01:
A++;
numberdisplayTextView.setText(String.valueOf(A));
break;
case R.id.Button02:
A--;
numberdisplayTextView.setText(String.valueOf(A));
break;
case R.id.Button03:
break;
case R.id.Button04:
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivity(myIntent);
break;
default:
break;
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#00AAAA">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/hello" />
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<Button android:id="#+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="increment"></Button>
<Button android:id="#+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="decrement"></Button>
<Button android:id="#+id/Button03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="make call"></Button>
<Button android:id="#+id/Button04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="media_camera"></Button>
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="24sp"
android:text="0" android:textStyle="bold" android:textColor="#000000"></TextView>
<EditText android:id="#+id/EditText01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="enter new button text"></EditText>
<RadioButton android:id="#+id/RadioButton01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="camera"></RadioButton>
</LinearLayout>
Enable in your Eclipse the LogCat
Window > Show View > Other > Android >
LogCat
then you can find what is the real description of your Exception.

Categories

Resources