I have made an Android Studio app that plays an mp3 file when you click a button. Suddenly it started crashing. I have checked the code myself and I cannot spot any errors. I tried reinstalling the emulator too and using a physical android device to connect, but still the same issue happens. Here is the java file:
public class MainActivity extends AppCompatActivity {
private Button yes;
private Button no;
private ImageButton iwant;
private ImageButton food;
private ImageButton restroom;
private ImageButton water;
private ImageButton playground;
private ImageButton rice;
private ImageButton school;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yes = findViewById(R.id.yes);
no = findViewById(R.id.no);
iwant = findViewById(R.id.iwant);
food = findViewById(R.id.food);
restroom = findViewById(R.id.restroom);
water = findViewById(R.id.water);
playground = findViewById(R.id.playground);
rice = findViewById(R.id.rice);
school = findViewById(R.id.school);
final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.yes);
final MediaPlayer mediaPlayer2 = MediaPlayer.create(this,R.raw.no);
final MediaPlayer mediaPlayer3 = MediaPlayer.create(this,R.raw.iwant);
final MediaPlayer mediaPlayer4 = MediaPlayer.create(this,R.raw.food);
final MediaPlayer mediaPlayer5 = MediaPlayer.create(this,R.raw.restroom);
final MediaPlayer mediaPlayer6 = MediaPlayer.create(this,R.raw.water);
final MediaPlayer mediaPlayer7 = MediaPlayer.create(this,R.raw.playground);
final MediaPlayer mediaPlayer8 = MediaPlayer.create(this,R.raw.rice);
final MediaPlayer mediaPlayer9 = MediaPlayer.create(this,R.raw.school);
yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.start();
}
});
no.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer2.start();
}
});
iwant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer3.start();
}
});
food.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer4.start();
}
});
restroom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer5.start();
}
});
water.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer6.start();
}
});
playground.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer7.start();
}
});
rice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer8.start();
}
});
school.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer9.start();
}
});
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 1 year ago.
Here is the screenshot of Error message
I was creating a calculator app. It seems the error message is in
display.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getString(R.string.display).equals(display.getText().toString().length()));{
display.setText("");
}
}
});
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
Button buttonMult;
Button buttonSub;
Button buttonDec;
Button equal;
Button add;
Button divide;
Button percent;
private EditText display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b0 =findViewById(R.id.button0);
b1 =findViewById(R.id.button1);
b2 =findViewById(R.id.button2);
b3 =findViewById(R.id.button3);
b4 = findViewById(R.id.button4);
b5 =findViewById(R.id.button5);
b6 =findViewById(R.id.button6);
b7 =findViewById(R.id.button7);
b8 =findViewById(R.id.button8);
b9 =findViewById(R.id.button9);
buttonMult =findViewById(R.id.buttonMult);
buttonSub =findViewById(R.id.buttonSub);
buttonDec =findViewById(R.id.buttonDec);
add =findViewById(R.id.add);
divide =findViewById(R.id.divide);
percent =findViewById(R.id.percent);
equal =findViewById(R.id.equal);
display = findViewById(R.id.input);
display.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getString(R.string.display).equals(display.getText().toString().length()));{
display.setText("");
}
}
});
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText("+");
}
});
equal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText("=");
}
});
buttonDec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText(".");
}
});
buttonSub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText("-");
}
});
buttonMult.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {display.setText("*");
}
});
b0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("0");
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("1");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("2");
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("3");
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { display.setText("4");
}
});
b5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("5");
}
});
b6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("6");
}
});
b7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("7");
}
});
b8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("8");
}
});
b9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display.setText("9");
}
});
}
}
This error typically occurs when you are referencing a view that isn't associated with the layout you've loaded.
Your code references view with the ID input.
I'm guessing your EditText View has an ID of Display, try changing the reference ID for the display variable.
display = findViewById(R.id.display)
Sir I want to know, how to put here my condition that it must add previous result with current value by pressing Add Button, instead of this it just add current and previous value, please help me
btn_Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value1 = Float.parseFloat(ed1.getText() + "");
Add = true ;
ed1.setText(null);
}
});
#Tim Biegeleisen, #Henry, #NiVeR 54, # Jeeva here has given up my code
what you would suggest?
package com.example.maher.androidspinnertutorial;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class calculatr extends AppCompatActivity {
Button btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_0,btn_Add,btn_Sub,btn_Mul,btn_Div,btn_calc,btn_dec,btn_clear;
EditText ed1;
float Value1, Value2;
boolean mAddition, mSubtract, mMultiplication, mDivision ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculatr);
btn_0 = (Button) findViewById(R.id.btn_0);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_4 = (Button) findViewById(R.id.btn_4);
btn_5 = (Button) findViewById(R.id.btn_5);
btn_6 = (Button) findViewById(R.id.btn_6);
btn_7 = (Button) findViewById(R.id.btn_7);
btn_8 = (Button) findViewById(R.id.btn_8);
btn_9 = (Button) findViewById(R.id.btn_9);
btn_Add = (Button) findViewById(R.id.btn_Add);
btn_Div = (Button) findViewById(R.id.btn_Div);
btn_Sub = (Button) findViewById(R.id.btn_Sub);
btn_Mul = (Button) findViewById(R.id.btn_Mul);
btn_calc = (Button) findViewById(R.id.btn_calc);
btn_dec = (Button) findViewById(R.id.btn_dec);
btn_clear = (Button) findViewById(R.id.btn_clear);
ed1 = (EditText) findViewById(R.id.edText1);
btn_0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"0");
}
});
btn_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"1");
}
});
btn_2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"2");
}
});
btn_3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"3");
}
});
btn_4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"4");
}
});
btn_5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"5");
}
});
btn_6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"6");
}
});
btn_7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"7");
}
});
btn_8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"8");
}
});
btn_9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+"9");
}
});
btn_dec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText(ed1.getText()+".");
}
});
btn_Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value1 = Float.parseFloat(ed1.getText() + "");
mAddition = true ;
ed1.setText(null);
}
});
btn_Sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value1 = Float.parseFloat(ed1.getText() + "");
mSubtract = true ;
ed1.setText(null);
}
});
btn_Mul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value1 = Float.parseFloat(ed1.getText() + "");
mMultiplication = true ;
ed1.setText(null);
}
});
btn_Div.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value1 = Float.parseFloat(ed1.getText()+"");
mDivision = true ;
ed1.setText(null);
}
});
btn_calc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value2 = Float.parseFloat(ed1.getText() + "");
if (mAddition == true){
ed1.setText(Value1 + Value2 +"");
mAddition=false;
}
if (mSubtract == true){
ed1.setText(Value1 - Value2 +"");
mSubtract=false;
}
if (mMultiplication == true){
ed1.setText(Value1 * Value2 + "");
mMultiplication=false;
}
if (mDivision == true){
ed1.setText(Value1 / Value2+"");
mDivision=false;
}
}
});
btn_clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ed1.setText("");
}
});
}
}
I've 3 activities in my android game: MainActivity, OptionActivity and GameActivity. I'm trying to pull in elements of board_view.xml into another activity, i.e., into OptionActivity.java.
board_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/cell11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="180dp" />
<Button
android:id="#+id/cell10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cell11"
android:layout_alignBottom="#+id/cell11"
android:layout_toLeftOf="#+id/cell11" />
<Button
android:id="#+id/cell12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cell11"
android:layout_alignBottom="#+id/cell11"
android:layout_toRightOf="#+id/cell11" />
<Button
android:id="#+id/cell01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/cell12"
android:layout_alignLeft="#+id/cell11" />
<Button
android:id="#+id/cell21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cell11"
android:layout_below="#+id/cell11" />
<Button
android:id="#+id/cell00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cell01"
android:layout_alignBottom="#+id/cell01"
android:layout_alignLeft="#+id/cell10" />
<Button
android:id="#+id/cell02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cell01"
android:layout_alignLeft="#+id/cell12" />
<Button
android:id="#+id/cell20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cell21"
android:layout_alignBottom="#+id/cell21"
android:layout_alignRight="#+id/cell10" />
<Button
android:id="#+id/cell22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cell21"
android:layout_alignBottom="#+id/cell21"
android:layout_alignLeft="#+id/cell12" />
</RelativeLayout>
I'm using this code to pull elements from board_view.xml into OptionActivity.java:
Button tiles[][];
public Button[][] getTiles() {
tiles[0][0] = (Button) findViewById(R.id.cell00);
tiles[0][1] = (Button) findViewById(R.id.cell01);
tiles[0][2] = (Button) findViewById(R.id.cell02);
tiles[1][0] = (Button) findViewById(R.id.cell10);
tiles[1][1] = (Button) findViewById(R.id.cell11);
tiles[1][2] = (Button) findViewById(R.id.cell12);
tiles[2][0] = (Button) findViewById(R.id.cell20);
tiles[2][1] = (Button) findViewById(R.id.cell21);
tiles[2][2] = (Button) findViewById(R.id.cell22);
return tiles;
}
And I'm using those tiles in spinner click events as given below:
private void spinnerListener() {
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String s = parent.getItemAtPosition(position).toString();
if (s.equalsIgnoreCase("easy")) {
tiles[0][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 0);
}
});
tiles[0][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 1);
}
});
tiles[0][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 2);
}
});
tiles[1][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 0);
}
});
tiles[1][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 1);
}
});
tiles[1][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 2);
}
});
tiles[2][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 0);
}
});
tiles[2][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 1);
}
});
tiles[2][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 2);
}
});
} else if (s.equalsIgnoreCase("intermediate")) {
tiles[0][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 1);
}
});
tiles[0][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 0);
}
});
tiles[0][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 2);
}
});
tiles[1][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 1);
}
});
tiles[1][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 0);
}
});
tiles[1][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 2);
}
});
tiles[2][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 1);
}
});
tiles[2][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 0);
}
});
tiles[2][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 2);
}
});
} else if (s.equalsIgnoreCase("difficult")) {
Toast.makeText(OptionActivity.this, "Difficult", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(OptionActivity.this, "Please select the difficulty level!", Toast.LENGTH_SHORT).show();
}
});
}
But when OptionActivity is loaded in mobile, I get this error:
02-22 00:47:13.369 8383-8383/com.example.ajaykulkarni.tictactoe E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ajaykulkarni.tictactoe, PID: 8383
java.lang.NullPointerException: Attempt to read from null array
at com.example.ajaykulkarni.tictactoe.OptionActivity$1.onItemSelected(OptionActivity.java:106)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
at android.widget.AdapterView.access$300(AdapterView.java:55)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
How can I pull array of buttons from another layout to another activity?
EDIT:
OptionActivity:
public class OptionActivity extends Activity implements Observer {
Model model;
Button start_game;
Button one_player;
RadioGroup group;
RadioButton player1_turn;
RadioButton player2_turn;
EditText player1_name;
EditText player2_name;
Spinner spinner1;
Button tiles[][];
public Button[][] getTiles() {
tiles[0][0] = (Button) findViewById(R.id.cell00);
tiles[0][1] = (Button) findViewById(R.id.cell01);
tiles[0][2] = (Button) findViewById(R.id.cell02);
tiles[1][0] = (Button) findViewById(R.id.cell10);
tiles[1][1] = (Button) findViewById(R.id.cell11);
tiles[1][2] = (Button) findViewById(R.id.cell12);
tiles[2][0] = (Button) findViewById(R.id.cell20);
tiles[2][1] = (Button) findViewById(R.id.cell21);
tiles[2][2] = (Button) findViewById(R.id.cell22);
return tiles;
}
public Model getModel() {
return model;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*setContentView(R.layout.activity_option);*/
setContentView(R.layout.activity_option);
setTitle("Tic-Tac-Toe");
Log.d("Tic-Tac-Toe", "Game Option constructor");
// setup the model
model = new Model();
model.addObserver(this);
// setup the radio buttons and radio group
group = (RadioGroup) findViewById(R.id.select_turn);
player1_turn = (RadioButton) findViewById(R.id.player1_turn);
player2_turn = (RadioButton) findViewById(R.id.player2_turn);
// setup the edit texts
player1_name = (EditText) findViewById(R.id.player1_name);
player2_name = (EditText) findViewById(R.id.player2_name);
// setup the game mode buttons
one_player = (Button) findViewById(R.id.one_player);
// register the controller
this.registerController();
/*spinner functionalities*/
this.addSpinner();
this.spinnerListener();
}
private void addSpinner() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, R.id.spinner1);
arrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
}
private void spinnerListener() {
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String s = parent.getItemAtPosition(position).toString();
if (s.equalsIgnoreCase("easy")) {
tiles[0][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 0);
}
});
tiles[0][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 1);
}
});
tiles[0][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 2);
}
});
tiles[1][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 0);
}
});
tiles[1][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 1);
}
});
tiles[1][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 2);
}
});
tiles[2][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 0);
}
});
tiles[2][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 1);
}
});
tiles[2][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 2);
}
});
} else if (s.equalsIgnoreCase("intermediate")) {
tiles[0][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 1);
}
});
tiles[0][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 0);
}
});
tiles[0][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 2);
}
});
tiles[1][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 1);
}
});
tiles[1][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 0);
}
});
tiles[1][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 2);
}
});
tiles[2][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 1);
}
});
tiles[2][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 0);
}
});
tiles[2][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 2);
}
});
} else if (s.equalsIgnoreCase("difficult")) {
Toast.makeText(OptionActivity.this, "Difficult", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(OptionActivity.this, "Please select the difficulty level!", Toast.LENGTH_SHORT).show();
}
});
}
BoardView.java:
public class BoardView extends RelativeLayout implements Observer {
Model model;
Button tiles[][];
public BoardView(Context context, Model m) {
super(context);
Log.d("Tic-Tac-Toe", "BoardView constructor");
View.inflate(context, R.layout.board_view, this);
// save the model reference
model = m;
// add this view to model's list of observers
model.addObserver(this);
// get a reference to widgets to manipulate on update
tiles = new Button[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
String row = Integer.toString(i);
String col = Integer.toString(j);
String tileId = "cell" + row + col;
int id = getResources().getIdentifier(tileId, "id",
getContext().getPackageName());
tiles[i][j] = (Button) findViewById(id);
tiles[i][j].setFocusable(false);
tiles[i][j].getBackground().setColorFilter(Color.WHITE,
PorterDuff.Mode.MULTIPLY);
}
}
this.registerControllers();
}
/* the controller part */
private void registerControllers() {
tiles[0][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 0);
}
});
tiles[0][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 1);
}
});
tiles[0][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(0, 2);
}
});
tiles[1][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 0);
}
});
tiles[1][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 1);
}
});
tiles[1][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(1, 2);
}
});
tiles[2][0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 0);
}
});
tiles[2][1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 1);
}
});
tiles[2][2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
model.playerMove(2, 2);
}
});
}
The line OptionActivity.java:106 is:
tiles[0][0].setOnClickListener(new OnClickListener() {
The immediate problem and cause of the error message is that you never initialize your array in OptionActivity:
Button tiles[][];
Even though you mention "another activity" in your question, it appears that you have a custom view BoardView and a single activity OptionActivity. The Activity should treat BoardView as a single, complete entity and never access its internal components directly, including individual subviews. Instead, BoardView should provide appropriate methods allow very controlled interaction. If you do this correctly, an activity will have only a single findViewById() call to get the BoardView from the layout.
Recap of comments
OptionActivity should not be aware of BoardView not its internal workings. Instead it should only be responsible for setting preferences and saving them. Most likely you should use SharedPreferences for this. Using PreferenceActivity will help tremendously.
Now your MainActivity will load the options from SharedPreferences and pass them to BoardView. The "appropriate methods" I mentioned earlier will be one or more setter methods.
BoardView contains the code for game operations and is influenced by the options that is selected in OptionActivity.
Note that, with my suggestion, BoardView is influenced by the values of the options but not by where those values come from. This provides a lot of flexibility. Also, OptionActivity is not influenced by BoardView at all, at least not directly.
If there are a lot of options you should consider grouping related ones into one or more class.
I want to develop an app to toggle the device's audio status. If the audio status is silent then my button's text should be "silent", if it's normal the text should be "normal"
Here is my only class:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn1= (Button) findViewById(R.id.btn1);
final AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (audio.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)
{
btn1.setText("Normal");
}
else if (audio.getRingerMode()==AudioManager.RINGER_MODE_SILENT)
{
btn1.setText("Silent");
}
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (audio.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)
{
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
btn1.setText("Silent");
}
else if (audio.getRingerMode()==AudioManager.RINGER_MODE_SILENT)
{
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
btn1.setText("Normal");
}
}
});
}
});
How can I always check this condition in Android ?
If you need to get the mode of current ringer even if it was changed outside from you application context then you should register a BroadcastReceiver for AudioManager.RINGER_MODE_CHANGED_ACTION.
This receiver will be called everytime someone changes the ringer mode.
Do this inside onCreate.
BroadcastReceiver receiver=new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
if (audio.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)
{
btn1.setText("Silent");
}
else if (audio.getRingerMode()==AudioManager.RINGER_MODE_SILENT)
{
btn1.setText("Normal");
}
}
};
IntentFilter filter=new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(receiver,filter);
Just do this
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (audio.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)
{
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
btn1.setText("Silent");
}
else if (audio.getRingerMode()==AudioManager.RINGER_MODE_SILENT)
{
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
btn1.setText("Normal");
}
}
});
what your are doing is
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (audio.getRingerMode()==AudioManager.RINGER_MODE_NORMAL)
{
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
btn1.setText("Silent");
}
else if (audio.getRingerMode()==AudioManager.RINGER_MODE_SILENT)
{
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
btn1.setText("Normal");
}
}
});
}
});