开发者

Displaying text from another activity on a TextView

I am trying to use putExtra() and getExtra() to send String Data from one activity to another, such that the retrieved string is to be displayed on a TextView and when running. When i run the program i get a classCastException on onCreate() method.

I am new to android so any assistance will be appreciated.

Here is my sample code:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sales); 

    //TextView
    TextView model1 = (TextView)findViewById(R.id.model1);
    TextView model2 = (TextView)findViewById(R.id.model2);
    TextView model3 = (TextView)findViewById(R.id.model3);

    //Bundle
    Bundle bundle = getIntent().getExtras();
    String M开发者_JAVA技巧od1 = bundle.getString("model1");
    String Mod2 = bundle.getString("model2");
    String Mod3 = bundle.getString("model3");

//setting values            
model1.setText(Mod1);               
model2.setText(Mod2);               
model3.setText(Mod3);
   }                


Your widgets model1,model2,model3 are either not TextView or you do not pass strings to the intent that you pass to the new Activity. Also you could try to clean your projects, maybe your R.java file is messed up. You could also paste the LogCat or tell us which is the line that gives you the ClassCastException.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜