从位于另一个类中的一个类访问变量

时间:2016-11-08 15:08:47

标签: java android oop android-studio error-handling

我已经在我的Android应用程序中停留了这个错误,现在已经持续了大约3个星期。它杀了我...... :(。有人可以为我修复代码(这是一个小错误),然后我可以从中学习。基本上,我有一个MainActivity类,它包含一个变量" dblCountValue&#34我希望在Withdraw类中访问变量。这是我的代码:

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:退出

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

  }
}

提前多多感谢:)

3 个答案:

答案 0 :(得分:3)

要将变量从一个活动发送到其他活动,您应该使用Intent对象。在您的情况下,您必须创建一个 MainActivity中的Intent对象和Intent对象可以将数据发送到Withdraw类。

您应该在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);

        }
    });

现在这段代码发生在onCreate方法的Withdraw类中。这里我们将使用key从Intet对象中提取dblCountValue。

在Withdraw类中,替换dblCountValue = mainActivityObject.dblCountValue;

以下代码部分

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

答案 1 :(得分:0)

只需在

下面的位置声明dblCountValue

公共类MainActivity扩展了AppCompatActivity {

私人InterstitialAd mInterstitial;

按钮btnClick; 按钮btnWithdraw;

TextView txtBalance;

public int dblCountValue;

@覆盖 protected void onCreate(

答案 2 :(得分:0)

首先将变量dblCountValue声明为全局变量
1。通过意图传递此变量。
2。如果变量在许多活动中有很多用法,那么最好将它保存在sharedPreference中并从中获取数据。不要忘记在离开应用程序时清除共享首选项。
3。创建一个带有变量dblCountValue及其getter和setter的单例类。在MainActivity中设置其值并在WithDrawActivity中检索。将它保存为静态变量并通过Class访问它。
好的想法是通过intent传递变量或使用sharedPreference。