【问题标题】:Accessing a variable from one class that is located in another class从一个类中访问另一个类中的变量
【发布时间】:2017-03-22 06:43:29
【问题描述】:

我在我的 android 应用程序中被这个错误困扰了大约 3 周。它杀了我...... :(。有人可以为我修复代码(这是一个小错误),然后我可以从中学习。基本上,我有一个包含变量“dblCountValue”的 MainActivity 类。我想访问 Withdraw 类中的变量。这是我的代码:

类: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);
    }
  }
}

类:退出

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

  }
}

提前非常感谢:)

【问题讨论】:

  • 这是一个本地字段。没有可能的方法来访问它。除此之外,这是完全错误的dblCountValue = mainActivityObject.dblCountValue;
  • 您的 dblCountValue 整数在 OnClick 方法中的本地范围。您无法在该方法之外访问它。如果您想使用 mainActivity 对象访问它,请将该变量声明为类变量。
  • 愿上帝赦免你的罪。看看:stackoverflow.com/a/3510771/1944896
  • 那么如何从 Withdraw 类中获取 dblCountValue?
  • 开始学习 Java。

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


【解决方案1】:

只需在下面的位置声明 dblCountValue

公共类 MainActivity 扩展 AppCompatActivity {

私有InterstitialAd mInterstitial;

按钮 btnClick; 按钮 btnWithdraw;

TextView txtBalance;

public int dblCountValue;

@覆盖 受保护的无效 onCreate(

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案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);
      
              }
          });
      

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

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

      以下代码部分

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

      【讨论】:

        猜你喜欢
        • 2023-04-07
        • 2020-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-18
        • 1970-01-01
        • 2011-10-22
        相关资源
        最近更新 更多