I am a newbie to eclipse, and I want to write an app.
Here are what i want to do:
I've created 3 strings: x y and z
each have intergers with format like ("a,b,c")
With 3 toggle button(btn1,btn2,btn3), I want to get a string "assigned" to the button and put them in order.
Public class MainActivity extends{
String a=String("2,7,3")
String b=String("2,6,8,1")
String c=String("6,8,9")
String d=String()
String e=String()
private Button Btn1
private Button Btn2
private Button Btn3
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Btn1=(Button)findViewById(R.id.Btn1);
Btn2=(Button)findViewById(R.id.Btn2);
Btn3=(Button)findViewById(R.id.Btn3);
Btn1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
d="a"+","
}
}
}
}
So that's the case.....
I don't know if I make myself clear enough....but it would be awesome if you can help me out....
Thanks So Much
The problem you have is here:
public void onClick(View arg0) {
d="a"+","
}
You looks like you want String d set to String a not a string a value of "a" ... the correct code is to set String d like this:
public void onClick(View arg0) {
d = a +","
}
If several buttons are pressed, then you should do the following:
public void onClick(View arg0) {
d = d + a +","
}
to keep them in order.
Related
where is error? how can I fix it? layout is true error in 24th line that
kaleciKayit.setOnClickListener(new View.OnClickListener() {
here is my all codes
public class oyuncuAdlariActivity extends AppCompatActivity {
Button kaleciKayit;
Button oyuncuKayit;
EditText isimGiris;
String isimGirisString;
int kaleciSayisi = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oyuncu_adlari);
kaleciKayit = (Button) findViewById(R.id.kaleciButton);
oyuncuKayit = (Button) findViewById(R.id.oyuncuButton);
isimGiris = (EditText) findViewById(R.id.isimGir);
kaleciKayit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
kaleciSayisi++;
isimGirisString = isimGiris.getText().toString();
if (isimGirisString.isEmpty()){
Toast toast1 = Toast.makeText(getApplicationContext(),getApplicationContext().getString(R.string.isimBos), Toast.LENGTH_SHORT);
toast1.show();
}
else if (kaleciSayisi >2)
kaleciKayit.setEnabled(false);
}
});
}
}
First of all, I will recommend setting the objects in a class to private. I think the problem is that maybe you are assigning the id of the wrong widget? check the FindViewByID again.
The other 3 possibilities that I can think about are-
The problem is in the first activity(In this case I ask you to post here the content of the first activity)
The setContentView method points at a wrong XML file.
Your AVD ran out of memory and you need to add more to it in the AVD Manager tool.
Change MainActivity to this,
public class MainActivity extends AppCompatActivity {
Button devamButton;
EditText takim1, takim2;
String takimTextA;
String takimTextB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
devamButton = findViewById(R.id.devamButton);
takim1 = findViewById(R.id.takimB);
takim2 = findViewById(R.id.takimA);
devamButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takimTextA = takim1.getText().toString();
takimTextB = takim2.getText().toString();
if (takimTextA.equals(takimTextB)) {
Toast toast1 = Toast.makeText(getApplicationContext(),getApplicationContext().getString(takimAyniOlmaz), Toast.LENGTH_SHORT);
toast1.show();
} else if (takimTextA.isEmpty() || takimTextB.isEmpty()) {
Toast toast2 = Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.takimBosBirakma), Toast.LENGTH_SHORT);
toast2.show();
} else {
activityGecis1(v);
}
}
});
}
private void activityGecis1(View v) {
Intent gecis1 = new Intent(v.getContext(), oyuncuAdlariActivity.class);
startActivity(gecis1);
}
Since you are calling inside onclicklistener maybe this in intent may point to that functions class.
I made app that shows you random number after you select minimum and maximum number. But after rotating result disappears, so i implemented onSaveInstanceState and customized it for my need, and after that my app crashes all the time.
public class MainActivity extends AppCompatActivity {
EditText et_min,et_max;
Button button;
TextView tv_output;
Random r;
int min, max, output;
private static final String STATE_TV_OUTPUT = "tv_output";
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
String onSave = savedInstanceState.getString("onSave");
}
#Override
protected void onSaveInstanceState(Bundle SavedInstanceState) {
super.onSaveInstanceState(SavedInstanceState);
SavedInstanceState.putString("onSave", "1");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r = new Random();
String onSave = tv_output.getText().toString();
et_max = (EditText) findViewById(R.id.et_max);
et_min = (EditText) findViewById(R.id.et_min);
button = (Button) findViewById(R.id.button);
tv_output = (TextView) findViewById(R.id.tv_output);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tempMin, tempMax;
tempMin = et_min.getText().toString();
tempMax = et_max.getText().toString();
if (!tempMin.equals("") && !tempMax.equals(""))//ovde stavljam ih u poziciju gde su slobodni za pisanje
min = Integer.parseInt(tempMin);
max = Integer.parseInt(tempMax);//ova dva sluze kako bi pretvorili stringove u intove za potrebe dole methoda
if (max > min) {
output = r.nextInt((max - min) + 1) + min;//nextInt sluzi kako bi dao random broj izmedju dva broja u ovom slucaju min i max
tv_output.setText("" + output);
}
}
});
}
}
you need to change the order, else the changed bundle won't be passed:
#Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("onSave", "1");
super.onSaveInstanceState(savedInstanceState);
}
the call to the super class' method is not always the topmost one.
What's the logcat output?
You can read more about the Android activity lifecycle here: https://developer.android.com/guide/components/activities/activity-lifecycle#java
Your code to save and restore state will be like this:
static final String STATE_KEY = "onSave";
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
String onSave = savedInstanceState.getString(STATE_KEY);
}
#Override
protected void onSaveInstanceState(Bundle SavedInstanceState) {
SavedInstanceState.putString(STATE_KEY, "1");
super.onSaveInstanceState(SavedInstanceState);//call this at the end
}
Put this line
String onSave = tv_output.getText().toString();
after tv_output is initialized, otherwise you will get NullPointerException.
UPDATE:
To save the text in the TextView during rotation, add android:freezesText="true" to xml (What is (s/g)etFreezesText in TextView?), no need to use onSaveInstanceState.
I am trying to make a app that has a switch a button and a text and if you turn the switch on and press the button; the number displayed on the text will be added by 1. But if the switch is turned off the number will be subtracted by 1.
but when i run my app and press the button, the app crashes...
i do not have much experience at programming and i do not know what im doing wrong. and i have only tried this code.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
Switch mySwitch = (Switch)findViewById(R.id.mySwitch);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked== true){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(text_int);
}
});
}
if (isChecked == false) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(text_int);
}
});
}
}
});
}
}
so this should behave as i described earlier but it doesn't.
Your app crashes because you are trying to set an int to a textview.setText()and when you pass an int to this method it expects it to be a resource id and which could not be found in your case that's why it will throw ResourceNotFoundException and crashes.
You should set text as following:
text.setText(String.valueOf(text_int));
You’re nesting listeners but that logic doesn’t work sequentially. You should declare your listeners separately. I suggest you create a boolean that holds the state of the switch and one button listener. Within the listener check if switch is enabled then run your calculations and do the same if the switch is disabled.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mySwitch.isChecked(){
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(String.valueOf(text_int));
} else {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(String.valueOf(text_int));
}
}
});
You don't need a listener for the Switch, but only 1 listener for the Button:
final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
final Switch mySwitch = (Switch)findViewById(R.id.mySwitch);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = 0;
try {
text_int = Integer.parseInt(text_string);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (mySwitch.isChecked())
text_int++;
else
text_int--;
text.setText("" + text_int);
}
});
Every time you click the Button, in its listener the value in the TextView is increased or decreased depending on whether the Switch is checked or not.
I am the beginner of the Android SDK programming.
What I have is,a calculator that doesn't show decimals when i press calculate button and is there if there is any way to do it?Also crashes if I didn't fill the EditText.
Button cal;
EditText n1,n2,n3;
TextView answer;
int x,y,r,z;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
answer = (TextView)findViewById(R.id.res);
n1 = (EditText)findViewById(R.id.n1);
n2 = (EditText)findViewById(R.id.n2);
n3 = (EditText)findViewById(R.id.n3);
cal = (Button)findViewById(R.id.calc);
cal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
x=Integer.parseInt(n1.getText().toString());
y=Integer.parseInt(n2.getText().toString());
z=Integer.parseInt(n3.getText().toString());
r=(x+y+z)/3;
answer.setText("" + r);
}
});
You are using Integer.parseInt , to get decimal use Double.parseDouble
set this for edit texts -
n1.setInputType(InputType.TYPE_CLASS_NUMBER);
n2.setInputType(InputType.TYPE_CLASS_NUMBER);
n3.setInputType(InputType.TYPE_CLASS_NUMBER);
for crashing put checks that user can calculate only when edit texts have values
cal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!n1.getText().toString().isEmpty()&&!n2.getText().toString().isEmpty()&&!n3.getText().toString().isEmpty())
calculateResult();
else{ //show error message
}
}
});
public void calculateResult()
{
x=Double.parseDouble(n1.getText().toString());
y=Double.parseDouble(n2.getText().toString());
z=Double.parseDouble(n3.getText().toString());
r=(x+y+z)/3;
answer.setText("" + r);
}
and change data types of x,y,z,r to double
I am making this beer/drink app as a fun project. I checked around the site and couldn't find anything specific to my problem. I would greatly appreciate help at two things;
I would like a button to reset whatever the count gets to in this code, I can't seem to figure out what I need..
The countButton is for beer and the drinkButton is for, well yeah, drinks.
public class Activity2 extends AppCompatActivity {
// Private member field to keep track of the count
private int mCount = 0;
private int mSum = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
final TextView countTextView = (TextView) findViewById(R.id.TextViewCount);
final TextView sumTextView = (TextView) findViewById(R.id.textSum);
final ImageButton countButton = (ImageButton) findViewById(R.id.beerCount);
final ImageButton drinkButton = (ImageButton) findViewById(R.id.drinkCount);
countButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCount++;
mSum += 72;
countTextView.setText("You have been drinking " + mCount + " units!");
sumTextView.setText("Sum:" + mSum + "!");
}
});
drinkButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCount++;
mSum += 96;
countTextView.setText("You have been drinking " + mCount + " units!");
sumTextView.setText("Sum:" + mSum + "!");
}
});
Also, the count does remove itself when I toggle through tabs in the app, any ideas to make it stay, and only removing by the reset button?
Greatly appreciate any help!
For what you require, simply add a third button to your activity and in its onCLickListener, set the values to 0.
Something like this.
resetButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
this.mSum = 0;
}
});