Caused by: java.lang.NullPointerException: Error message [duplicate] - java

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)

Related

Doesn't save the result of previous 2 values if i put 3rd value for addtion

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("");
}
});
}
}

How to change text in BaseAdapter

I am trying to change the text of button inside onClick() but it's not changing. I have tried the following -
viewHoder.btn_select_qq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHoder.btn_select.setText("xxxxx");
notifyDataSetChanged();
}
});
How to set it?
Try this -
viewHoder.btn_select_qq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v;
b.setText("xxxxx");
}
});

Unable to use another layout element in another activity - android studio

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.

how do you add two or more numbers selected randomly by the user in a single EditText using android studio?

I am making a mobile calculator and I am aware there are many that have been created. I tried using others as an example to build mine but I have been stuck for days. I was able to display numbers on the EditText when user selects random numbers, but I cannot figure how to add, sub, multi, divide, the numbers that were selected by the user. Please show an example of how I can solve it. This is my first mobile application.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView editText = (EditText) findViewById(R.id.edit_text);
final Button Button0 = (Button) findViewById(R.id.Button_0);
final Button Button1 = (Button) findViewById(R.id.Button_1);
Button Button2 = (Button) findViewById(R.id.Button_2);
Button Button3 = (Button) findViewById(R.id.Button_3);
Button Button4 = (Button) findViewById(R.id.Button_4);
Button Button5 = (Button) findViewById(R.id.Button_5);
Button Button6 = (Button) findViewById(R.id.Button_6);
Button Button7 = (Button) findViewById(R.id.Button_7);
Button Button8 = (Button) findViewById(R.id.Button_8);
Button Button9 = (Button) findViewById(R.id.Button_9);
Button clear_Btn = (Button) findViewById(R.id.C_Button);
Button decimal_Btn = (Button) findViewById(R.id.Decimal_Button);
Button equal_Btn = (Button) findViewById(R.id.equal_Button);
final Button add_Btn = (Button) findViewById(R.id.Addition_Button);
/////////////////// NUMBER BUTTONS ///////////////////////
Button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 0);
}
});
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editText.setText(editText.getText().toString() + 1);
}
});
Button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 2);
}
});
Button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 3);
}
});
Button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 4);
}
});
Button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 5);
}
});
Button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 6);
}
});
Button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 7);
}
});
Button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 8);
}
});
Button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + 9);
}
});
///////////////////// Sign Buttons /////////////////////
// Clear Button
clear_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText("");
}
});
// Decimal Button
decimal_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + ".");
}
});
// Equal button
equal_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
// Add button
add_Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText(editText.getText().toString() + "+");
}
});
}
}
A couple ways to achieve this, you just need a way to retrieve the current number in the calculator and perform an operation against the new selected number
So store the main number in its own variable, and then perform the desired operation against a new number, before you set the new edittext value

Music stops after 20 seconds (android soundboard) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have made my own android soundboard.
Now i have only 1 problem
I have a song Intro , but it is about 38 seconds.
When i am trying to play intro i only hear for about 20 seconds.
can someone help me ?
SoundBoard.java
package com.soundboard;
import com.soundboard.SoundManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Soundboard extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.sound1);
mSoundManager.addSound(2, R.raw.sound2);
mSoundManager.addSound(3, R.raw.sound3);
mSoundManager.addSound(4, R.raw.sound4);
mSoundManager.addSound(5, R.raw.sound5);
mSoundManager.addSound(6, R.raw.sound6);
mSoundManager.addSound(7, R.raw.sound7);
mSoundManager.addSound(8, R.raw.sound8);
mSoundManager.addSound(9, R.raw.sound9);
mSoundManager.addSound(10, R.raw.sound10);
mSoundManager.addSound(11, R.raw.sound11);
mSoundManager.addSound(12, R.raw.sound12);
mSoundManager.addSound(13, R.raw.sound13);
mSoundManager.addSound(14, R.raw.sound14);
mSoundManager.addSound(15, R.raw.sound15);
mSoundManager.addSound(16, R.raw.sound16);
mSoundManager.addSound(17, R.raw.sound17);
mSoundManager.addSound(18, R.raw.sound18);
mSoundManager.addSound(19, R.raw.sound19);
mSoundManager.addSound(20, R.raw.sound20);
mSoundManager.addSound(21, R.raw.sound21);
mSoundManager.addSound(22, R.raw.sound22);
mSoundManager.addSound(23, R.raw.sound23);
mSoundManager.addSound(24, R.raw.sound24);
mSoundManager.addSound(25, R.raw.sound25);
mSoundManager.addSound(26, R.raw.sound26);
mSoundManager.addSound(27, R.raw.sound27);
mSoundManager.addSound(28, R.raw.sound28);
mSoundManager.addSound(29, R.raw.sound29);
mSoundManager.addSound(30, R.raw.sound30);
mSoundManager.addSound(31, R.raw.sound31);
mSoundManager.addSound(32, R.raw.sound32);
mSoundManager.addSound(33, R.raw.sound33);
mSoundManager.addSound(34, R.raw.sound34);
mSoundManager.addSound(35, R.raw.sound35);
mSoundManager.addSound(36, R.raw.sound36);
Button SoundButton1 = (Button)findViewById(R.id.sound1);
SoundButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button SoundButton2 = (Button)findViewById(R.id.sound2);
SoundButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
Button SoundButton3 = (Button)findViewById(R.id.sound3);
SoundButton3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(3);
}
});
Button SoundButton4 = (Button)findViewById(R.id.sound4);
SoundButton4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(4);
}
});
Button SoundButton5 = (Button)findViewById(R.id.sound5);
SoundButton5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(5);
}
});
Button SoundButton6 = (Button)findViewById(R.id.sound6);
SoundButton6.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(6);
}
});
Button SoundButton7 = (Button)findViewById(R.id.sound7);
SoundButton7.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(7);
}
});
Button SoundButton8 = (Button)findViewById(R.id.sound8);
SoundButton8.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(8);
}
});
Button SoundButton9 = (Button)findViewById(R.id.sound9);
SoundButton9.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(9);
}
});
Button SoundButton10 = (Button)findViewById(R.id.sound10);
SoundButton10.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(10);
}
});
Button SoundButton11 = (Button)findViewById(R.id.sound11);
SoundButton11.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(11);
}
});
Button SoundButton12 = (Button)findViewById(R.id.sound12);
SoundButton12.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(12);
}
});
Button SoundButton13 = (Button)findViewById(R.id.sound13);
SoundButton13.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(13);
}
});
Button SoundButton14 = (Button)findViewById(R.id.sound14);
SoundButton14.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(14);
}
});
Button SoundButton15 = (Button)findViewById(R.id.sound15);
SoundButton15.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(15);
}
});
Button SoundButton16 = (Button)findViewById(R.id.sound16);
SoundButton16.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(16);
}
});
Button SoundButton17 = (Button)findViewById(R.id.sound17);
SoundButton17.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(17);
}
});
Button SoundButton18 = (Button)findViewById(R.id.sound18);
SoundButton18.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(18);
}
});
Button SoundButton19 = (Button)findViewById(R.id.sound19);
SoundButton19.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(19);
}
});
Button SoundButton20 = (Button)findViewById(R.id.sound20);
SoundButton20.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(20);
}
});
Button SoundButton21 = (Button)findViewById(R.id.sound21);
SoundButton21.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(21);
}
});
Button SoundButton22 = (Button)findViewById(R.id.sound22);
SoundButton22.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(22);
}
});
Button SoundButton23 = (Button)findViewById(R.id.sound23);
SoundButton23.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(23);
}
});
Button SoundButton24 = (Button)findViewById(R.id.sound24);
SoundButton24.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(24);
}
});
Button SoundButton25 = (Button)findViewById(R.id.sound25);
SoundButton25.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(25);
}
});
Button SoundButton26 = (Button)findViewById(R.id.sound26);
SoundButton26.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(26);
}
});
Button SoundButton27 = (Button)findViewById(R.id.sound27);
SoundButton27.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(27);
}
});
Button SoundButton28 = (Button)findViewById(R.id.sound28);
SoundButton28.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(28);
}
});
Button SoundButton29 = (Button)findViewById(R.id.sound29);
SoundButton29.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(29);
}
});
Button SoundButton30 = (Button)findViewById(R.id.sound30);
SoundButton30.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(30);
}
});
Button SoundButton31 = (Button)findViewById(R.id.sound31);
SoundButton31.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(31);
}
});
Button SoundButton32 = (Button)findViewById(R.id.sound32);
SoundButton32.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(32);
}
});
Button SoundButton33 = (Button)findViewById(R.id.sound33);
SoundButton33.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(33);
}
});
Button SoundButton34 = (Button)findViewById(R.id.sound34);
SoundButton34.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(34);
}
});
Button SoundButton35 = (Button)findViewById(R.id.sound35);
SoundButton35.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(35);
}
});
Button SoundButton36 = (Button)findViewById(R.id.sound36);
SoundButton36.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(36);
}
});
}
}
SoundManager.java
package com.soundboard;
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager {
private SoundPool mSoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public SoundManager()
{
}
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
public void playSound(int index) {
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
}
Thank you
Is there any specific reason why you're using Soundpool instead of MediaPlayer?
MediaPlayer is much more elegant for this type of media playback.
If you're doing that on the main thread and not in a service (recommended for music playback of that length) or a background thread you could be locking up the event thread for too long. Just a thought.

Categories

Resources