如何int变量传递给其他的活动? [英] How to pass int variable to another activity?

查看:114
本文介绍了如何int变量传递给其他的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想的意图,另一个活动传递一个int值,但我总是得到0。而且这不是0,我检查。我试图通过从int变量brojPoena值。我想这样的:

 意向书I =新意图(游戏。这,Popup_opis.class);
i.putExtra(brojPoenaPrimljeno,brojPoena);

在我接受的活动:

 意图mIntent = getIntent();
        如果(mIntent!= NULL){
           INT brojPoena = mIntent.getIntExtra(brojPoenaPrimljeno,0);
        }
tvBrojPoena.setText(你赢了+ brojPoenaPrimljeno +点);

我也尝试过这样的:

 意向书I =新意图(getApplicationContext(),Popup_opis.class);
i.putExtra(brojPoenaPrimljeno,brojPoena);

在我recieving活动:

 捆绑extrasPoe​​ni = getIntent()getExtras()。
        如果(extrasPoe​​ni!= NULL){
           brojPoenaPrimljeno = extras.getInt(brojPoena);
        }

我的接收活动:

 公共类Popup_opis扩展活动实现OnClickListener {    TextView的tvOpis,tvNaslov,tvBrojPoena;
    串poslatOpis,primljenOpis;
    INT brojPoenaPrimljeno;
    OK按钮;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow()setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN)。        的setContentView(R.layout.popup_opis);        捆绑额外= getIntent()getExtras()。
        如果(临时演员!= NULL){
           primljenOpis = extras.getString(poslatOpis);
        }        捆绑extrasPoe​​ni = getIntent()getExtras()。
        如果(extrasPoe​​ni!= NULL){
           brojPoenaPrimljeno = extras.getInt(brojPoena);
        }        initVariables();    }    私人无效initVariables(){
        字体电视= Typeface.createFromAsset(getAssets(),ARIALN.TTF);
        OK =(按钮)findViewById(R.id.bOK);
        tvOpis =(的TextView)findViewById(R.id.tvOpis);
        tvBrojPoena =(的TextView)findViewById(R.id.tvBrojPoena);
        tvBrojPoena.setTypeface(电视);
        tvNaslov =(的TextView)findViewById(R.id.tvNaslov);
        tvNaslov.setTypeface(电视);
        tvOpis.setTypeface(电视);
        tvOpis.setText(primljenOpis);
        tvBrojPoena.setText(Osvojili STE+ brojPoenaPrimljeno +无明文不üovoj igri。);    OK.setOnClickListener(新OnClickListener(){        公共无效的onClick(视图v){
            完();        }
    });
    }    公共无效的onClick(视图v){
        // TODO自动生成方法存根    }}


解决方案

这哪里是变量 brojPoenaPrimljeno ?你有这样的

  INT brojPoena = mIntent.getIntExtra(brojPoenaPrimljeno,0);

但使用的是当你调用一个不同的变量名称的setText()

您尝试接收使用不是值的值。当您创建意图

  i.putExtra(brojPoenaPrimljeno,brojPoena); // brojPoenaPrimljeno的关键是试图使用

尝试

  brojPoenaPrimljeno = getIntent()getIntExtra(brojPoenaPrimljeno,0);

此外,这是次要的,而不是你的问题,但效率不高,可能会造成问题。你得到两个不同的地方`意图。在这里,

 捆绑额外= getIntent()getExtras()。

在这里

 捆绑extrasPoe​​ni = getIntent()getExtras()。

处理来自不同意图活动

在情况下,这会有所帮助,如果我有接收活动意图来自多个地方,我将使用一个字符串额外来告诉接收活动它是从哪里来的。例如:

意向意图=新意图(SendingActivity.this,ReceivingActivity.class);
intent.putExtra(源,activityName); //这将被用来知道那里的意图是从哪里来的。

  //接收活动
意向recIntent = getIntent();
如果(recIntent.getStringExtra(源)!= NULL)
{
     字符串源= recIntent.getStringExtra(源);
    如果(source.equals(activityName))
    {
         //做的东西
    }
    如果(source.equals(differentActivityName))
    {
         //做其他的东西
    }
}

I'm trying to pass an int value with an intent to another activity, but I always get 0. And it's not 0, I checked. I'm trying to pass value from int variable brojPoena. I tried this:

Intent i = new Intent(Game.this, Popup_opis.class);
i.putExtra("brojPoenaPrimljeno", brojPoena);

and in my receiving activity:

Intent mIntent = getIntent(); 
        if(mIntent !=null) {
           int brojPoena = mIntent.getIntExtra("brojPoenaPrimljeno", 0);
        }
tvBrojPoena.setText("You won " + brojPoenaPrimljeno + " points");

Also I tried this:

Intent i = new Intent(getApplicationContext(), Popup_opis.class);
i.putExtra("brojPoenaPrimljeno", brojPoena);

and in my recieving activity:

Bundle extrasPoeni = getIntent().getExtras(); 
        if(extrasPoeni !=null) {
           brojPoenaPrimljeno = extras.getInt("brojPoena");
        }

My receiving activity:

public class Popup_opis extends Activity implements OnClickListener{

    TextView tvOpis,tvNaslov,tvBrojPoena;
    String poslatOpis, primljenOpis;
    int brojPoenaPrimljeno;
    Button OK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.popup_opis);

        Bundle extras = getIntent().getExtras(); 
        if(extras !=null) {
           primljenOpis = extras.getString("poslatOpis");
        }

        Bundle extrasPoeni = getIntent().getExtras(); 
        if(extrasPoeni !=null) {
           brojPoenaPrimljeno = extras.getInt("brojPoena");
        }

        initVariables();

    }

    private void initVariables() {


        Typeface tv = Typeface.createFromAsset(getAssets(), "ARIALN.TTF");
        OK = (Button) findViewById(R.id.bOK);
        tvOpis = (TextView) findViewById(R.id.tvOpis);
        tvBrojPoena = (TextView) findViewById(R.id.tvBrojPoena);
        tvBrojPoena.setTypeface(tv);
        tvNaslov = (TextView) findViewById(R.id.tvNaslov);
        tvNaslov.setTypeface(tv);
        tvOpis.setTypeface(tv);
        tvOpis.setText(primljenOpis);
        tvBrojPoena.setText("Osvojili ste " + brojPoenaPrimljeno + " poena u ovoj igri.");



    OK.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            finish();

        }
    });
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}

解决方案

Where is this variable brojPoenaPrimljeno? You have this

int brojPoena = mIntent.getIntExtra("brojPoenaPrimljeno", 0);

but are using a different variable name when you call setText()

You are trying to receive the value using the value instead of the key. When you create the Intent

i.putExtra("brojPoenaPrimljeno", brojPoena);  // brojPoenaPrimljeno is the key be trying to use to 

Try

 brojPoenaPrimljeno = getIntent().getIntExtra("brojPoenaPrimljeno", 0);

Also, this is minor and not your problem but is inefficient and could cause problems. You are getting the `Intent in two different places. Here

Bundle extras = getIntent().getExtras();

and here

Bundle extrasPoeni = getIntent().getExtras(); 

Handling Intent from different Activities

In case this helps, if I have an Activity receiving Intents from multiple places, I will use a String extra to tell the receiving Activity where it came from. For example:

Intent intent = new Intent(SendingActivity.this, ReceivingActivity.class); intent.putExtra("source", "activityName"); // this will be used to know where the intent came from

//receiving activity
Intent recIntent = getIntent();
if (recIntent.getStringExtra("source") != null)
{
     String source = recIntent.getStringExtra("source");
    if (source.equals("activityName"))
    {
         // do stuff
    }
    if (source.equals("differentActivityName"))
    {
         // do other stuff
    }
}

这篇关于如何int变量传递给其他的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆