-3

Iv been stuck on this bug, in my android app, for about 3 weeks now. Its killing me... :(. Can someone please fix the code for me (its a small error), and then I can learn from it. Basically, I have the MainActivity class that holds a variable "dblCountValue". I want to access the variable in the Withdraw class. Here is my code:

CLASS: MainActivity

package com.mycash.borgjake.mycash;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.Button;
import android.widget.TextView;

import android.view.View;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

import static com.mycash.borgjake.mycash.R.styleable.View;

public class MainActivity extends AppCompatActivity {

private InterstitialAd mInterstitial;


Button btnClick;
Button btnWithdraw;

TextView txtBalance;

@Override
protected void onCreate(Bundle savedInstanceState) {

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

    AdView adView = (AdView)findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    adView.loadAd(adRequest);

    btnClick = (Button) findViewById(R.id.button);

    txtBalance = (TextView) findViewById(R.id.textView);

    btnClick.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v){
                String countValue = txtBalance.getText().toString();
                int dblCountValue = Integer.parseInt(countValue);

                if (mInterstitial.isLoaded()) {
                    mInterstitial.show();
                }

                //mInterstitial.loadAd(request);

                dblCountValue++;
                txtBalance.setText(String.valueOf(dblCountValue));

        }
    });

    mInterstitial = new InterstitialAd(this);
    mInterstitial.setAdUnitId("...");
    AdRequest request = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    mInterstitial.loadAd(request);

}

public void onButtonClick(View v) {
    if(v.getId() == R.id.button2) {
        Intent i = new Intent(MainActivity.this, Withdraw.class);
        startActivity(i);
    }
  }
}

CLASS: Withdraw

package com.mycash.borgjake.mycash;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class Withdraw extends Activity {

MainActivity mainActivityObject = new MainActivity();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.withdraw);

    AdView adView = (AdView) findViewById(R.id.adView2);
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    adView.loadAd(adRequest);
}

public void showAlert(View view) {
    AlertDialog.Builder myAlert = new AlertDialog.Builder(this);

    String messageWithdraw;
    String aWithdraw;
    String dWithdraw;
    aWithdraw = "Congradulations! Your payment is being processed";
    dWithdraw = "Sorry! Please try again when you reach 100 points ($10)";



    dblCountValue = mainActivityObject.dblCountValue;      // <-- Error

    if (dblCountValue > 100) {                           // <-- Error
        myAlert.setMessage(aWithdraw)                   
                .create();                             
        myAlert.show();                               
    } else {                                         
        myAlert.setMessage(dWithdraw)               
                .create();                         
        myAlert.show();                           
    }                                            

  }
}

Thanks a lot in advance :)

user3704212
  • 1
  • 1
  • 6

3 Answers3

3

To send a variable from one activity to other Activity you should use Intent object.In your case you have to create an Intent object in your MainActivity and with that Intent object you can send your data to Withdraw class.

you should write this code in MainActivity.

public void onClick(View v){
                String countValue = txtBalance.getText().toString();
                int dblCountValue = Integer.parseInt(countValue);

                if (mInterstitial.isLoaded()) {
                    mInterstitial.show();
                }

                //mInterstitial.loadAd(request);

                dblCountValue++;
                txtBalance.setText(String.valueOf(dblCountValue));

        Intent intent = new Intent(MainActivity.this, Withdraw.class);
        intent.putExtra( "key" , dblCountValue);
               startActivity(intent);

        }
    });

Now this section of code take place in your Withdraw class in side onCreate method .Here we will extract dblCountValue from Intet object using key .

In Withdraw class replace dblCountValue = mainActivityObject.dblCountValue;

with following code section

Bundle bundle = getIntent().getExtras();
int dblCountValue =Bundle.getInt("key",0);
pcrafter
  • 31
  • 3
0

Just declare the dblCountValue at location below

public class MainActivity extends AppCompatActivity {

private InterstitialAd mInterstitial;

Button btnClick; Button btnWithdraw;

TextView txtBalance;

public int dblCountValue;

@Override protected void onCreate(

Sahil Shaikh
  • 161
  • 8
0

First of all declare your variable dblCountValue as a global variable then
1. Pass this variable as via intent.
2. If the variable has lot of usage in many activities then better save it in sharedPreference and get data from it.Don't forget to clear sharedpreference while leaving your app.
3. Create a singleton class with variable dblCountValue and its getters and setters.So set its value in MainActivity and retrieve in WithDrawActivity.
4. Keep it as a static variable and access it via Class.
Good idea is to pass the variable via intent or use sharedPreference.

Rajesh Gosemath
  • 1,812
  • 1
  • 17
  • 31