i have 6 layouts and each layout having two button back and next..without button back and next application runs properly but when i put code in back and next button having name button 7 is used for back and button 8 is used for next layout it gives me a error null exception.
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
ImageButton btn6;
ImageButton btn7;
ImageButton btn8;
ImageView view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton7();
//addListenerOnButton8();
addListenerOnButton();
addListenerOnButton2();
addListenerOnButton3();
addListenerOnButton4();
addListenerOnButton5();
addListenerOnButton6();
public void addListenerOnButton7() {
btn7= (ImageButton) findViewById(R.id.btn7);
btn7.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.show);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton() {
btn1= (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.show);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton2() {
btn2= (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k2);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton3() {
btn3= (ImageButton) findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k3);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton4() {
btn4= (ImageButton) findViewById(R.id.btn4);
btn4.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k4);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton5() {
btn5= (ImageButton) findViewById(R.id.btn5);
btn5.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k5);
Runtime.getRuntime().gc();
}
}
);
}
public void addListenerOnButton6() {
btn6= (ImageButton) findViewById(R.id.btn6);
btn6.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k6);
Runtime.getRuntime().gc();
}
}
);
}
You already use it in there
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Looking at the code, it looks like it comes from inside an activity so you need to call setContentView(R.layout.activity_main); in the onCreate() of your activity before btn7= (ImageButton) findViewById(R.id.btn7); and that should fix the NullPointer.
Related
I have implemented two chronometers in a same activity and so far they are working fine. But when I change activities and then come back to them, they are at 00:00. I need them to keep counting "forever" until I hit the stop button. How can I do that?
And is it possible to make that counting appear also in another activity, so that I can follow up the counting in either activity? It has to be the same counting. Just two different places where I can follow the up from.
I tried a solution with onRestoreInstanceState but I am really confused. I am really new to coding and simple things are still frustrating to me. I am getting something wrong at the set.Base().
Chronometer cronometro_primeira_marca, cronometro_segunda_marca;
Button btn_iniciar_cronometro_primeira_marca, btn_parar_cronometro_primeira_marca, btn_resetar_cronometro_primeira_marca;
Button btn_iniciar_cronometro_segunda_marca, btn_parar_cronometro_segunda_marca, btn_resetar_cronometro_segunda_marca;
String timer1, timer2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cronometros);
cronometro_primeira_marca = (Chronometer) findViewById(R.id.cronometro_primeira_marca);
cronometro_segunda_marca = (Chronometer) findViewById(R.id.cronometro_segunda_marca);
btn_iniciar_cronometro_primeira_marca = (Button) findViewById(R.id.btn_iniciar_cronometro_primeira_marca);
btn_parar_cronometro_primeira_marca = (Button) findViewById(R.id.btn_parar_cronometro_primeira_marca);
btn_resetar_cronometro_primeira_marca = (Button) findViewById(R.id.btn_resetar_cronometro_primeira_marca);
btn_iniciar_cronometro_segunda_marca = (Button) findViewById(R.id.btn_iniciar_cronometro_segunda_marca);
btn_parar_cronometro_segunda_marca = (Button) findViewById(R.id.btn_parar_cronometro_segunda_marca);
btn_resetar_cronometro_segunda_marca = (Button) findViewById(R.id.btn_resetar_cronometro_segunda_marca);
btn_iniciar_cronometro_primeira_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_primeira_marca.setBase(SystemClock.elapsedRealtime());
cronometro_primeira_marca.start();
}
});
btn_iniciar_cronometro_segunda_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_segunda_marca.setBase(SystemClock.elapsedRealtime());
cronometro_segunda_marca.start();
}
});
btn_parar_cronometro_primeira_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_primeira_marca.stop();
}
});
btn_parar_cronometro_segunda_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_segunda_marca.stop();
}
});
btn_resetar_cronometro_primeira_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_primeira_marca.setBase(SystemClock.elapsedRealtime());
}
});
btn_resetar_cronometro_segunda_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_segunda_marca.setBase(SystemClock.elapsedRealtime());
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putLong("Restored_time1", SystemClock.elapsedRealtime());
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if((savedInstanceState !=null)&& savedInstanceState.containsKey("Restored_time1")){
SystemClock.elapsedRealtime().**setBase**(savedInstanceState.getLong("Restored_time1"));
}
super.onRestoreInstanceState(savedInstanceState);
}
}
I have researched similar problems but none is a fit for this problem. Am trying to access the arrays of drawables from the method name called nextQuestion(), but i keep getting the error cannot resolve symbol 'ballArray' any help please. I just feel that this should work but don't know why. Here is my code.
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
The reason you can't access ballArray from within nextQuestion() is because it's declared in a separate method. If you want it accessible from within nextQuestion(), you would need to make it visible at that level:
ImageView mBallDisplay;
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
Do like this:-
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
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
public class saeidactivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.saeid);
Button btn=(Button) findViewById(R.id.saeidbtn1);
TextView str=(TextView) findViewById(R.id.saeidtxtv1);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
str.setTextColor(0xFF00FF00);
}
});
}
}
Try to add final modifier.
final TextView str=(TextView)
instead of
TextView str=(TextView)
Or, you can make the TextView local.
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
TextView str=(TextView) findViewById(R.id.saeidtxtv1);
if(str != null)
str.setTextColor(0xFF00FF00);
}
});
I am having a layout file and 5 buttons -- 1 for next and 2 for previous etc. Application runs fine, when I click first button, it loads the image correctly, then as I click another button, it gives the error -Caused by java lang Out Of Memory Error
public class MainActivity extends ActionBarActivity {
private ShareActionProvider mShareActionProvider;
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
ImageButton btn6;
ImageButton btn7;
ImageButton btn8;
ImageView image1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
addListenerOnButton2();
addListenerOnButton3();
addListenerOnButton4();
addListenerOnButton5();
addListenerOnButton6();
}
public void addListenerOnButton() {
btn1= (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.show);
}
}
);
}
public void addListenerOnButton2() {
btn2= (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k2);
}
}
);
}
public void addListenerOnButton3() {
final Context context =this;
btn3= (ImageButton) findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k3);
}
}
);
}
public void addListenerOnButton4() {
final Context context =this;
btn4= (ImageButton) findViewById(R.id.btn4);
btn4.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k4);
}
}
);
}
public void addListenerOnButton5() {
final Context context =this;
btn5= (ImageButton) findViewById(R.id.btn5);
btn5.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k5);
}
}
);
}
public void addListenerOnButton6() {
final Context context =this;
btn6= (ImageButton) findViewById(R.id.btn6);
btn6.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
setContentView(R.layout.k6);
}
}
);
}
You are changing the whole layout instead of changing the image. When you do that, garbage collector is not called and you should be calling it on your own. Since it's not called, you are going to get that error pretty soon.
Do it like this instead
int[] imageArray = {R.drawable.image1, R.drawable.image2, ... };
and when you click a button, just do
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
someImageView.setImageResource(imgageArray[0])
}
}
Also make sure you don't use too big images. The take up a lot of memory.