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

时间:2016-05-16 22:07:17

标签: java android

我正在制作一个Android应用程序,我需要在2个活动中使用一个整数值。我尝试使用此代码,但它没有工作。

//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);

它表示无法解析符号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.
    }
}

错误代码:

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.
    }
}

以上都是答案! -------------------------------------------------- -------

现在我对这部分代码有疑问:

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

当我点击按钮时,它说我没有足够的钱,但我有超过300.请帮助我。 我发现问题是什么,但我不知道如何解决它。我需要将余额发回MainActivity。任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

您在发送和接收Int时使用不同的密钥。 改变这一行:

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

对此:

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

答案 1 :(得分:0)

发送这样的数据 -

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

并在onCreate() - {/ p>的UpgradesActivity内抓取

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