Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

java - how to set text an integer and get int without getting error

I tried to get the intent from integer. The String get intent works fine and displays well but when i put the integer i get a force close error.

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

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


    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
TextView text1 = (TextView) findViewById(R.id.calories);
text1.setText(""+productCalories);

Or

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...