program has stopped in AVD - java

i make a button to start a game. whenever i click on that button from AVD, unfortunately the program has stopped and i am new to android programming. i try googling my problem and it seems there is something wrong with the code. can anyone help me? i also share a link to my code, visit https://www.dropbox.com/sh/hyitjbgda69rkd4/3Iz8WuM5-5
the main activity
public class MainActivity extends Activity
{
private Game game1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.startbutton);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// Perform action on click
game1 = new Game(this);
setContentView(game1);
}
});
}
}
Game class :
public Game(OnClickListener onClickListener)
{
super((Context) onClickListener);
caneta = new Paint();
this.caneta.setARGB(255, 0, 0, 0);
this.caneta.setAntiAlias(true);
this.caneta.setStyle(Style.STROKE);
this.caneta.setStrokeWidth(5);
l = this.getWidth();
a = this.getHeight();
singlesquare = new Cell[x][y];
int xss = l / x;
int yss = a / y;
for (int z = 0; z < y; z++)
{
for (int i = 0; i < x; i++)
{
singlesquare[z][i] = new Empty(xss * i, z * yss);
}
}
}

You really shouldn't be calling setContentView() more than once. Is that the line that's causing the error? Further, the object type of game1 doesn't appear to be a View.

Related

Android Studio - Close Icon of a Chip does not do anything (Java)

I created some Chips and they have an X symbol on the right side.
But when I click the X (intending to dismiss or remove the Chip), nothing happens.
I tried to use the method setOnCloseIconClickListener but it did not have an effect.
I click the X icon and the color of the icon changes and a clicking sound appears, but the Chip View remains on the screen.
And I also don't know what to write in the callback method of the click listener.
for(int i = 0; i<products.length; i++) {
//the chip component requires your app theme to be Theme.MaterialComponents (or a
//descendant)
chips[i] = new Chip(this);
//ScrollView can only host one direct child
ll1.addView(chips[i]);
chips[i].setText(products[i]);
chips[i].setCloseIconVisible(true);
}
I tried this, but it said the variable i has to be final which is not possible cause i is incrementing.
chips[i].setOnCloseIconClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chips[i].close();
}
});
I found the solution. This is the code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et1;
private ScrollView sv1;
private LinearLayout ll1;
private Chip[] chips;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = findViewById(R.id.editText);
sv1 = findViewById(R.id.scrollView);
ll1 = new LinearLayout(this);
ll1.setOrientation(LinearLayout.VERTICAL);
sv1.addView(ll1);
}
public void splitToChips(View v) {
String content = et1.getText().toString();
String[] products = content.split(";");
chips = new Chip[products.length];
for(int i = 0; i<products.length; i++) {
chips[i] = new Chip(this);
ll1.addView(chips[i]);
chips[i].setText(products[i]);
chips[i].setCloseIconVisible(true);
chips[i].setOnCloseIconClickListener(this);
}
}
#Override
public void onClick(View v) {
Chip chip = (Chip) v;
ll1.removeView(chip);
}
}

tic tac toe andrdoid studio

I am making a tic tac toe game, i just created a array buttons. now i want to set a button with O and X then put in into a array but i don't know how to do that. I am new to android studio.
public class MainActivity extends AppCompatActivity {
int turn;
int[]myButton={R.id.button1,R.id.button2,R.id.button3,R.id.button4,
R.id.button5,R.id.button6,R.id.button7,R.id.button8,R.id.button9,};
int[]j= new int[10];
Button button[]=new Button[myButton.length];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
turn = 1;
for (int i = 0; i < myButton.length; i++) {
final Button button = (Button) findViewById(myButton[i]);
String Btn = String.valueOf(button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (button.getText().toString().equals("")) {
if (turn == 1) {
turn = 2;
button.setText("X");
}
else if (turn == 2) {
turn = 1;
button.setText("O");
}
}
endGame();
}
});
}
}
public void endGame() {
}
}
Now the thing is you have nine buttons but you dont know how to assign X or O for the players. I would suggest a solution like this. You have two players (A,B) and if A starts first, for the entire game he's going to go with label 'X' for his turns. For the other it's 'O'. You will have to mark your 9 buttons according to which player clicked it. Ex:- If 'A' clicked 'button5' then the button label should be turned to 'X'. likewise until all the buttons get clicked or someone wins the game continues. I only gave you the guidance. Come up with this implementation and let's resolve any new problems you have.

Multiple OnClick associated to WebViews inside loop

I want to link my Buttons to the associated WebViews. I have 2 lists, one of Button the other one of WebViews. What I'm trying to do is, showing or hiding the webView[x] associated to button[x]
nbObjects = 2;
Button[] buttons = null;
buttons = new Button[nbObjects];
buttons[0] = findViewById(R.id.button0);
buttons[1] = findViewById(R.id.button1);
WeView[] webViews = null;
webViews = new WebView[nbObjects];
webViews[0] = findViewById(R.id.webView0);
webViews[1] = findViewById(R.id.webView1);
for (int i = 0; i < nbObjects; i++) {
webViews[i].setVisibility(View.GONE);
final int j = i;
buttons[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (webViews[j].getVisibility() == View.VISIBLE) {
webViews[j].setVisibility(View.GONE);
} else {
webViews[j].setVisibility(View.VISIBLE);
}
}
});
}
Result: when I click on Button0, everything works. WebView0 shows and hides. But not for WebView1 when I click on Button1.
Solution/Update
My WebViews are actually showing up. I added a background color to spot them when no URL was loaded. Now time to loadURL

How to modify a global variable in a method

I have an animation for android with 10 buttons, and I need to trigger this buttons with an order, so I have a variable named order, if order=1 button1 can get activated, if order=2 button 2 can get activated and so on. When I open the program, an animation starts, then a second animation repeats itself after the first one ends and in between I need to change the variable order to 1. I have the next code:
public class Juego extends Activity
{
private AnimationDrawable animacion, loop;
private MediaPlayer miPlayer;
private int order = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_juego);
ImageView video = (ImageView) findViewById(R.id.secuencia);
video.setBackgroundResource(R.drawable.animation_drawable_start);
animacion = (AnimationDrawable) video.getBackground();
animacion.start();
checkIfAnimationDone0(animacion);
}
public void checkIfAnimationDone0(AnimationDrawable anim)
{
final AnimationDrawable a = anim;
int timeBetweenChecks = 20;
android.os.Handler h = new android.os.Handler();
h.postDelayed(new Runnable()
{
public void run()
{
if (a.getCurrent() != a.getFrame(a.getNumberOfFrames() - 1))
{
checkIfAnimationDone1(a);
}
else
{
ImageView video = (ImageView) findViewById(R.id.secuencia);
video.setBackgroundResource(R.drawable.animation_drawable_loop_inicio);
loop = (AnimationDrawable) video.getBackground();
loop.start();
order=1;
}
}
}, timeBetweenChecks);
};
public void onClickButton2(View any)
{
if (order == 1)
{
ImageView video = (ImageView) findViewById(R.id.secuencia);
video.setBackgroundResource(R.drawable.animation_drawable_boton_1);
animacion = (AnimationDrawable) video.getBackground();
miPlayer = MediaPlayer.create(Juego.this, R.raw.sonido_boton_1);
animacion.start();
miPlayer.start();
checkIfAnimationDone1(animacion);
order=2;
}
}
etc..
The problem is that the value of the global variable order does not get changed in the line order=1, and the method onClickButton() cannot start. How do I solve this?
In checkIfAnimationDone0(), when
a.getCurrent() != a.getFrame(a.getNumberOfFrames() - 1)
you go to checkIfAnimationDone1(), so order is never set as 1.
Change checkIfAnimationDone1() in checkIfAnimationDone0() to checkIfAnimationDone0().

How to create a scoreboard with a textview, 2 variables and a button in java for Android

I know this is a simpleton question, but I am not going to school for java, just learning it online.
how to a have a textview with an initial value of 0. and then everytime you press a button it ads 25 points to the score board.
At first I wanted the button press to add a random number between 42-57 to the score board.
And then how to do convert that int or long to a string to make it fit into a textview and keep the current score, and then add a new score.
EDIT: ok so someone said I should post the code so here it is.. where do i put this..
TextView txv182 = (TextView) findViewById(R.id.welcome);
txv182.setText(toString(finalScore));
Because when I do it, I get an error: The method toString() in the type Object is not applicable for the arguments (int)
public class MainActivity extends Activity {
// Create the Chartboost object
private Chartboost cb;
MediaPlayer mp = new MediaPlayer();
SoundPool sp;
int counter;
int db1 = 0;
Button bdub1;
TextView txv182;
int finalScore;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txv182 = (TextView) findViewById(R.id.welcome);
finalScore = 100;
sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
db1 = sp.load(this, R.raw.snd1, 1);
bdub1 = (Button) findViewById(R.id.b4DUB1);
bdub1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (db1 != 0)
sp.play(db1, 1, 1, 0, 0, 1);
txv182.setText(finalScore);
}
});
First you need to store your score to certain integer variable say score and set it to any initial value you want and use.
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setText(toString(score));
you do not need to initialize the textview with value just in onclick() of button do score+=25and add text to your textview as above.
hope this helps
It's actually like this..
pernts+=25;
txv182.setText(String.valueOf(pernts));
in android, after the
public class MainActivity extends Activity {
but before..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i wrote..
int pernts;
String strI;
so then after the above #Override majigy, i wrote..
strI = "" + pernts;
pernts = 0;
because when i wrote it like.. int pernts = 0; it never worked, forcing me to add final and what not..
any way.. How to convert an integer value to string? anSwered the question..
and so the end was like this
pernts+=25;
txv182.setText(String.valueOf(pernts));
i figured out you have to add a value to the int variable not the string.. i kept wanting to add 25 and i was getting 25252525252525.., instead of 25 50 75 100.. kinda cool actually.. separating the logic of how to "add" 25 .. anyway thanks.. SOF!
30 minutes later... found this.. How can I generate random number in specific range in Android?
..
import android.app.Activity;
public class InsMenu extends Activity {
static TextView txv182;
static int pernts;
Button bdub1;
public static void addPernts() {
int min = 37;
int max = 77;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
pernts+=i1;
txv182.setText(String.valueOf(pernts));
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.BLAHBLAH);
bdub1 = (Button) findViewById(R.id.b4DUB1);
txv182 = (TextView) findViewById(R.id.welcome);
bdub1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addPernts();
}
--- i created my first objekt thanks to this too.. http://www.tutorialspoint.com/java/java_methods.htm ..

Categories