user1072908 user1072908 - 4 months ago 9
Android Question

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

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

}





}

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

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