如何设置文本的整数,并得到INT没有得到错误 [英] how to set text an integer and get int without getting error

查看:99
本文介绍了如何设置文本的整数,并得到INT没有得到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code我在得到整数意图使用。字符串GET意图工作正常,并表现不错,但是当我把整数i得到一个强制关闭错误。我可能是错在这里做一些事情。这是code:

 包kfc.project;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.widget.EditText;
进口android.widget.TextView;公共类产品详情延伸活动{    @覆盖
    保护无效的onCreate(捆绑包){
        // TODO自动生成方法存根
        super.onCreate(包);
        的setContentView(R.layout.productdetail);
        //东西拿到意图
        意向receivedIntent = getIntent();
        字符串产品名称= receivedIntent.getStringExtra(名称);
        INT productCalories = receivedIntent.getIntExtra(卡路里,0);            / *意向意图=新意图(ProductListView.this,productdetail.class);
            intent.putExtra(名,product.getName());
            intent.putExtra(卡路里,product.getCalories());
            intent.putExtra(份量,product.getServingSize());
            intent.putExtra(胖,product.getFat());
            intent.putExtra(饱和脂肪,product.getSaturatedFat());
            intent.putExtra(反式脂肪,product.getTransFat());
            intent.putExtra(胆固醇,product.getCholesterol());
            intent.putExtra(钠,product.getSodium());
            intent.putExtra(碳水化合物,product.getCarbs());
            intent.putExtra(纤维,product.getFiber());
            intent.putExtra(糖,product.getSugar());
            intent.putExtra(蛋白质,product.getProtein());
            intent.putExtra(维生素A,product.getVitaminA());
            intent.putExtra(维生素C,product.getVitaminC());
            intent.putExtra(钙,product.getCalcium());
            intent.putExtra(铁,product.getIron()); * /
        捆绑额外= getIntent()getExtras()。        字符串名称= extras.getString(名称);        如果(名字!= NULL){
            TextView的文本1 =(的TextView)findViewById(R.id.servingsize);
            text1.setText(产品名称);        }
        // INT热量= extras.getInt(卡路里);            TextView的文本1 =(的TextView)findViewById(R.id.calories);
            text1.setText(productCalories);
        / *
        意向意图= getIntent();
        字符串str = intent.getStringExtra(名称); * /    }}


解决方案

 的TextView的text1 =(的TextView)findViewById(R.id.calories);
                text1.setText(+ productCalories);
//要么字符串s =将String.valueOf(productCalories);
 text1.setText(多个);

This is the code i used in getting the intent for integer. The String get intent works fine and displays well but when i put the integer i get a force close error. I might be doing something wrong here. This is the code:

package kfc.project;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class productdetail extends Activity{

    @Override
    protected void onCreate(Bundle bundle) {
        // TODO Auto-generated method stub
        super.onCreate(bundle);
        setContentView(R.layout.productdetail);
        //stuff to get intent
        Intent receivedIntent = getIntent();


        String productName = receivedIntent.getStringExtra("name");
        int productCalories = receivedIntent.getIntExtra("calories",0);

            /*Intent intent = new Intent(ProductListView.this, productdetail.class);
            intent.putExtra("name",product.getName());
            intent.putExtra("calories", product.getCalories());
            intent.putExtra("serving size", product.getServingSize());
            intent.putExtra("fat", product.getFat());
            intent.putExtra("saturated fat", product.getSaturatedFat());
            intent.putExtra("trans fat", product.getTransFat());
            intent.putExtra("cholesterol", product.getCholesterol());
            intent.putExtra("sodium", product.getSodium());
            intent.putExtra("carbs", product.getCarbs());
            intent.putExtra("fiber", product.getFiber());
            intent.putExtra("sugar", product.getSugar());
            intent.putExtra("protein", product.getProtein());
            intent.putExtra("vitamina", product.getVitaminA());
            intent.putExtra("vitaminc", product.getVitaminC());
            intent.putExtra("calcium", product.getCalcium());
            intent.putExtra("iron", product.getIron());*/
        Bundle extras = getIntent().getExtras();

        String name = extras.getString("name");

        if (name != null) {
            TextView text1 = (TextView) findViewById(R.id.servingsize);
            text1.setText(productName);

        }
        //int calories = extras.getInt("calories");

            TextView text1 = (TextView) findViewById(R.id.calories);
            text1.setText(productCalories);


        /*
        Intent intent = getIntent();
        String str = intent.getStringExtra("name");*/

    }





}

解决方案

    TextView text1 = (TextView) findViewById(R.id.calories);
                text1.setText(""+productCalories);
//or

String s = String.valueOf(productCalories);
 text1.setText(s);

这篇关于如何设置文本的整数,并得到INT没有得到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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