简体   繁体   English

Android Studio在2个活动中使用相同的整数

[英]Android studio use same integer in 2 activities

i was making an android app and i need to use one integer value in 2 activities. 我正在制作一个android应用程序,因此我需要在2个活动中使用一个整数值。 I tried using this code but it didn't work. 我尝试使用此代码,但是没有用。

//Integer Sender
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("MyIntNameGoesHere", intValue);
startActivity(myIntent);

//Integer receiver
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);

It says Cannot resolve symbol intValue and Cannot resolve symbol A and the same for B. Here's the whole code. 它说无法解析符号intValue和无法解析符号A,而对于B则相同。这是完整的代码。 MainActivity: 主要活动:

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    int balance;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Hide notification bar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //Click counter
        final TextView text = (TextView) findViewById(R.id.balance_text);
        assert text != null;
        // to retreuve the values from the shared preference
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        balance = preferences.getInt("balance", 0);
        text.setText(balance + " $");
        final ImageButton button = (ImageButton) findViewById(R.id.click_button);
        assert button != null;
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                balance++;
                text.setText("" + balance + " $");
                SharedPreferences.Editor editor = preferences.edit();
                editor.putInt("balance", balance);
                editor.apply();
            }
        });
        final Button UpgradesButton = (Button) findViewById(R.id.upgrades_button);
        assert UpgradesButton != null;
        UpgradesButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, UpgradesActivity.class));
            }
        });
        //Balance Integer Sender
    }
}

UpgradesActivity: 升级活动:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class UpgradesActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upgrades);
        //Hide notification bar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        final Button e2u_button = (Button) findViewById(R.id.e2u_button);
        assert e2u_button != null;
        e2u_button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            }
        });
        final Button back_button = (Button) findViewById(R.id.back_button);
        assert back_button != null;
        back_button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(UpgradesActivity.this, MainActivity.class));
            }
        });
        //TODO: Pass balance integer from MainActivity to here.
    }
}

Error code: 错误代码:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class UpgradesActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upgrades);
        //Receive balance from MainActivity
        Intent mIntent = getIntent();
        int intValue = mIntent.getIntExtra("key_int", 0);
        //Hide notification bar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        final Button e2u_button = (Button) findViewById(R.id.e2u_button);
        assert e2u_button != null;
        e2u_button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(balance >= 300){ //ERROR ON THIS LINE
                }
            }
        });
        final Button back_button = (Button) findViewById(R.id.back_button);
        assert back_button != null;
        back_button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(UpgradesActivity.this, MainActivity.class));
            }
        });
        //TODO: Pass balance integer from MainActivity to here.
    }
}

ALL ABOVE IS ANSWERED! 上面所有内容都已答复! --------------------------------------------------------- -------------------------------------------------- -------

Now i have problems with this part of the code: 现在我对这部分代码有疑问:

    @Override
    public void onClick(View v) {
        if(balance >= 300){
            balance -= 300;
        }
        if(balance < 300){
            final TextView text = (TextView) findViewById(R.id.not_enough_money_text);
            assert text != null;
            text.setText("You do not have enough money.");
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    text.setText("");
                }
            }, 2000);
        }
    }

When i click the button it says i do not have enough money but i have over 300. Please help me. 当我单击按钮时,它说我没有足够的钱,但是我有300多个。请帮助我。 I found out what the problem was but I'm not sure how to fix it. 我发现了问题所在,但不确定如何解决。 I need to send balance back to MainActivity. 我需要将余额发送回MainActivity。 Can anyone help with that? 有人可以帮忙吗?

You're using different key when sending and receiving the Int. 在发送和接收Int时,您正在使用其他密钥。 Change this line: 更改此行:

int intValue = mIntent.getIntExtra("intVariableName", 0);

To this: 对此:

int intValue = mIntent.getIntExtra("MyIntNameGoesHere", 0);

Send the data like this - 发送这样的数据-

   UpgradesButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                     Intent intent = new Intent(MainActivity.this, UpgradesActivity.class);
                    intent.putExtra("key_int", balance);
                    startActivity(intent);
                }
            });

And fetch it in onCreate() of UpgradesActivity - 并在UpgradesActivity onCreate()中获取它-

Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("key_int", 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM