dimanche 19 avril 2015

when I click button 4 I get a big run time error

This is a small android code to implement an exammple of actionlistener


To test this plz install this in your phone.


then first click button 4


then click button 10 and you will get the error.


This program has been written in adt.


enter code here



package com.example.test;
import java.util.Random;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
Button b[];

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b=new Button[11]; //declared an array of buttons and defined each button.

b[0] = (Button) findViewById(R.id.btn1);
b[1] = (Button) findViewById(R.id.btn2);
b[2] = (Button) findViewById(R.id.btn3);
b[3] = (Button) findViewById(R.id.btn4);
b[4] = (Button) findViewById(R.id.btn5);
b[5] = (Button) findViewById(R.id.btn6);
b[6] = (Button) findViewById(R.id.btn7);
b[7] = (Button) findViewById(R.id.btn8);
b[8] = (Button) findViewById(R.id.btn9);
b[9] = (Button) findViewById(R.id.btn10);
b[10] = (Button) findViewById(R.id.btn11);

for(int i=0;i<10;i++)
{
b[i].setOnClickListener(this);//set onClickListener on all buttons (here is where I think the execution is going wrong)
}

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@SuppressLint("ShowToast") @Override
public void onClick(View v){

if(v.getId() == R.id.btn10)
{
Toast.makeText(getApplicationContext(), "Click a button to change its color", 3).show(); //displaying a message
if(v.getId() == R.id.btn1) //checks which button is clicked
{
b[0].setBackgroundColor(Color.GREEN); //on clicking this command will get executed
}
else if(v.getId() == R.id.btn2)
{
b[1].setBackgroundColor(Color.GREEN);
}
else if(v.getId() == R.id.btn3)
{
b[2].setBackgroundColor(Color.GREEN);
}
else if(v.getId() == R.id.btn4)
{
b[3].setBackgroundColor(Color.GREEN);
}
else if(v.getId() == R.id.btn5)
{
b[4].setBackgroundColor(Color.GREEN);
}
else if(v.getId() == R.id.btn6)
{
b[5].setBackgroundColor(Color.GREEN);
}
else if(v.getId() == R.id.btn7)
{
b[6].setBackgroundColor(Color.GREEN);
}
else if(v.getId() == R.id.btn8)
{
b[7].setBackgroundColor(Color.GREEN);
}
else if(v.getId() == R.id.btn9)
{
b[8].setBackgroundColor(Color.GREEN);
}
else
{
Toast.makeText(getApplicationContext(), "You clicked a wrong button", 3).show();
}
}
else if(v.getId() == R.id.btn11)
{
Toast.makeText(getApplicationContext(), "Randomly a button will change color", 3).show();
Random r = new Random();

for(int i = 0 ; i < 10 ; i++)
{
int i1 = r.nextInt(9);
b[i1].setBackgroundColor(Color.GREEN);
}

}
else
{
Toast.makeText(getApplicationContext(), "Select only btn10 and btn11", 3).show();
}
}
}

Aucun commentaire:

Enregistrer un commentaire