In my Code I have an Intent to an other Activity but when I use my Phone to test it nothing appears. The program doesn't crashes or something like that. It simply does nothing. I have another Intent and this works perfectly. I don't know what the problem is.
I am using the onClick feature on the xml file
On the Main Activity:
public class MainActivity extends AppCompatActivity {
private Object TextView;
int eggcounter;
Button b1;
android.widget.TextView textClicks;
private Object SafeBrowsingResponse;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button b1 = findViewById(R.id.b1);
eggcounter = 100;
final ImageButton ImgButton = findViewById(R.id.eggBtn);
ImgButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
eggcounter = eggcounter - 1;
updateEgg();
if (eggcounter < 80) {
ImgButton.setImageResource(R.drawable.egg_2);
if (eggcounter < 60){
ImgButton.setImageResource(R.drawable.egg_3);
if (eggcounter < 40) {
ImgButton.setImageResource(R.drawable.egg_4);
if (eggcounter < 15) { ImgButton.setImageResource(R.drawable.egg_5);
if (eggcounter <= 0) {
b1.setVisibility(View.VISIBLE);
ImgButton.setImageResource(R.drawable.egg_ende);
b1.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
}
}
);
}
}
}
}
}
}
}
);
}
public void updateEgg() {
textClicks = (TextView) findViewById(R.id.textScore);
textClicks.setText(eggcounter + " ");
}
public void backstartseite(View view) {
Intent back = new Intent(this, Startseite.class);
startActivity(back);
}
public void ende (View view) {
Intent e = new Intent(this, Ende.class);
startActivity(e);
}
}
You are never calling backStartSeite or ende therefore no Intent fires.
Also don't set the onClickListener of b1 inside another onClickListener (your listener for b1 will only be able to handle click events once the other button has been clicked already - this would confuse users).
If you want your Intent to work, call either startSeite or ende in the outer onClickListener.
Related
Main Activity
i want to check the amount is greater than 100 , after clicking button
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buyItemButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (amount_checker >= 100){
Intent intent = new Intent(CartActivity.this,AddressActivity.class);
intent.putExtra("itemList", (Serializable) itemsList);
startActivity(intent);
}else {
Toast.makeText(getApplicationContext(), ""+amount_checker, Toast.LENGTH_SHORT).show();
}
}
});
}
i want to get the amount checker value to go to oncreate function
private void calculateAmount() {
amount_checker = 1223.00023223;
}
I want to get the amount checker value to go to onCreate function
Since you need the amount_checker value inside a click listener of a button (and not when the onCreate method is called) you can make amount_checker a field of your CartActivity class.
Then you are just referencing amount_checker as this.amount_checker instead anywhere you need to read or assign the value (maybe in some scopes you might have to access it with CartActivity.this.amount_checker).
class CartActivity {
// this stores the latest amount_checker value
private double amount_checker = 0;
private void calculateAmount() {
this.amount_checker = 1223.00023223;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buyItemButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (this.amount_checker >= 100){
Intent intent = new Intent(CartActivity.this,AddressActivity.class);
intent.putExtra("itemList", (Serializable) itemsList);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), ""+this.amount_checker, Toast.LENGTH_SHORT).show();
}
}
});
}
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.
Hello I have an activity which shows some listviews, and I want them not to reload/refresh every time I get into it, as it is programmed to show different items every time.
But I want it not to refresh until a button which is in another activity is pushed.
I've not tried anything yet as I don't know what to start with.
Here I leave you the code of the java.class:
public class Comida extends AppCompatActivity implements Adaptador2.OnRecipeListener {
private RecyclerView recyclerView1;
List<Entidad2> listItems;
Adaptador2 adaptor;
private Entidad2 entidad1,entidad2,entidad3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
String myValue = savedInstanceState.getString("key");
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_comida);
recyclerView1 = findViewById(R.id.lv_1);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView1.setLayoutManager(layoutManager);
listItems = new ArrayList<>();
entidad1 = new Entidad2(R.drawable.calabacines_3, "Solomillo a la plancha", " 10 min.", 4, 20);
entidad2 = new Entidad2(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Entrecot", " 15 min.", 2, 50);
entidad3 = new Entidad2(R.drawable.tomate, "Hamburguesa", " 2 min.", 5, 100);
listItems.add(entidad1);
listItems.add(entidad2);
listItems.add(entidad3);
adaptor = new Adaptador2(listItems, this);
recyclerView1.setAdapter(adaptor);
adaptor.notifyDataSetChanged();
pickEntidad();
}
#Override
public void OnRecipe(int priority) {
if (priority == 20) {
Intent in = new Intent(this, Solomillo.class);
startActivity(in);
}
if (priority == 50) {
Intent in = new Intent(this, Entrecot.class);
startActivity(in);
}
if (priority == 100) {
Intent in = new Intent(this, Hamburguesa.class);
startActivity(in);
}
}
private void pickEntidad(){
final int random = new Random().nextInt(101);
int priority1 = entidad1.getPriority();
int priority2 = entidad2.getPriority();
int priority3 = entidad3.getPriority();
listItems.clear();
if(random < priority1){
listItems.add(entidad1);
}else if(random < priority2){
listItems.add(entidad2);
}else if (random <= priority3){
listItems.add(entidad3);
}
adaptor.notifyDataSetChanged();
}
}
And then here there is the java.class of the other activity(the one which contains the button that has to refresh the other activity):
The button which I want to use to refresh the activity is the boton_prueba.
public class Menu extends AppCompatActivity {
Button boton_start;
Button boton_refresh;
Button boton_prueba;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
boton_start=(Button) findViewById(R.id.boton_platos);
boton_refresh = (Button) findViewById(R.id.boton_cambiarmenu);
boton_prueba=(Button) findViewById(R.id.boton_menu);
boton_start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(Menu.this,Dishes.class);
startActivity(in);
}
});
boton_prueba.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(Menu.this,Comida.class);
startActivity(in);
}
});
boton_refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//What to do?
}
});
}
}
Please if anyone has any idea of how to do it help me and in case you need more code or information just tell me.
Thank you.
If I really understand you then you want to execute pickEntidad() method when pressing in refresh button and go to this activity so you can do that using send any number or data with the intent like:
Intent in = new Intent(currentActivity.this,targetActivity.class);
in.putExtra("number",2);
startActivity(in);
and in target activity use somthing like this:
if(getIntent().getIntExtra("number",-1) ==2)
{
// what do you want to do...
}
I'm beginner in android dev and i'm creating a pizza clicker game, just like cookie clicker. I created an activity for upgrades and for upgrading you need some amount of pizza like if you have 10 pizzas you can upgrade. If the amount of pizzas is equals the price the button is enabled, if not, the button is not enabled. When I click the button, the amount of pizza is decreased and the button should disable again, but it's not disabling.
Here's the first activity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public static int pizza = 0;
public static TextView pizzaContText, helpers;
public static Button add, upgrades, exit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
add = (Button) findViewById(R.id.makePizza);
exit = (Button) findViewById(R.id.exitButton);
upgrades = (Button) findViewById(R.id.upgrades);
pizzaContText = (TextView) findViewById(R.id.pizzas);
helpers = (TextView) findViewById(R.id.helpers);
pizzaContText.setText("Pizzas: " + pizza);
pizzaContText.setTextColor(Color.BLACK);
pizzaContText.setTextSize(40);
helpers.setText("Helpers: " + Upgrades.contHelper);
helpers.setTextSize(20);
helpers.setTextColor(Color.BLACK);
add.setOnClickListener(this);
upgrades.setOnClickListener(this);
exit.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.makePizza:
pizza++;
pizzaContText.setText("Pizzas: " + pizza);
pizzaContText.setTextColor(Color.BLACK);
pizzaContText.setTextSize(40);
break;
case R.id.upgrades:
Intent i = new Intent(getApplicationContext(), Upgrades.class);
startActivity(i);
break;
case R.id.exitButton:
finish();
System.exit(0);
break;
}
}
}
And here's the second activity (the upgrades):
public class Upgrades extends AppCompatActivity implements View.OnClickListener{
public static int contHelper = 0, priceHelper = 10;
Button addHelper, back;
Handler h = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upgrades);
initialize();
if (MainActivity.pizza >= priceHelper){
//ENABLES THE BUTTON
addHelper.setEnabled(true);
} else{
//DISABLE THE BUTTON
addHelper.setEnabled(false);
}
}
private void initialize() {
addHelper = (Button) findViewById(R.id.addHelper);
addHelper.setText("Helper: " + priceHelper + " pizzas");
back = (Button) findViewById(R.id.back);
addHelper.setOnClickListener(this);
back.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.addHelper:
MainActivity.pizza-=priceHelper;
addHelper.setText("Helper: " + priceHelper + " pizzas");
priceHelper+=4;
contHelper++;
//Auto clicks the make pizza button every 1 sec
final Runnable r = new Runnable() {
#Override
public void run() {
MainActivity.add.performClick();
h.postDelayed(this, 1000);
}
};
h.postDelayed(r, 1000);
break;
case R.id.back:
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
break;
}
}
}
Don't use static objects.. Is the worst thing you could do.. Use bundle to send your variable
Intent activity = new Intent(this, Upgrades.class);
activity.putExtra("pizza", pizza);
startActivity(intent);
And in your Upgrades activity use
Bundle extras = getIntent().getExtras();
int pizza = extras.getInt("pizza");
And check for nulls and that you send the correct things.
I want to add a button in my application that turns off the music but I don't know how to approach it, I have an idea but I'm sure it's far from best so I want to consult with you. The situation is as follows:
public class MainActivity extends Activity implements OnClickListener {
MediaPlayer easysong;
MediaPlayer normalsong;
MediaPlayer hardsong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.land_main);
mContext = this;
restartButton = (Button)findViewById(R.id.restartButton);
restartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
easysong = MediaPlayer.create(MainActivity.this, R.raw.arideniro);
normalsong = MediaPlayer.create(MainActivity.this, R.raw.junior);
hardsong = MediaPlayer.create(MainActivity.this, R.raw.ketsathis);
counter = 101;
i = 500 - dif;
new Thread(new Runnable() {
public void run() {
if(i==500){
easysong.start();}
else if(i==375){
normalsong.start();
}else if(i==250){
hardsong.start();
}
while (counter > 0) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter--;
runOnUiThread(new Runnable() {
#Override
public void run() {
scoreText.setText(Integer.toString(counter));
}
});
if(i>150){
i/=1.01;}
else if(i>90-(dif/10)){
i-=1;
}
}if (counter==0) {
mChronometer.stop();
if(easysong.isPlaying()) {
easysong.stop();
easysong.release();
easysong = null;
}else if(normalsong.isPlaying()){
normalsong.stop();
normalsong.release();
normalsong = null;
}else if(hardsong.isPlaying()){
hardsong.stop();
hardsong.release();
hardsong = null;
}
This is the main class of my app where the mediaplayer is used, now I deleted much of the code because it was irrelevant to the mediaplayer and the question, so don't look for the missing brackets and such. And this here is the main menu class where the Switch that will turn on and off the music will be located:
public class MainMenu extends Activity{
private Button easy;
private Button normal;
private Button hard;
private Button scores;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
easy = (Button) findViewById(R.id.btn_easy);
scores = (Button) findViewById(R.id.btn_highscores);
easy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dif = 0;
startGame();
}
});
}
public void startGame() {
Intent intent = new Intent(MainMenu.this, MainActivity.class);
startActivity(intent);
}
So my idea is simnple, to add a variable in MainActivity like "int p;" and from the MainMenu class to change it's state between 0 and 1, then I will add around each line that starts music an if(p==1) but is this a good approach ? Also I would like the value of the int to be saved when the app is closed