2016-04-05 219 views
0

在edittext中为setText值引发错误。 Id给定是正确的,如果我给set尝试在空对象引用上调用虚拟方法'void android.widget.EditText.setText(java.lang.CharSequence)'导致空指针异常

下面给出的代码我不能setText Otp代码“ otpcode.setText( “12345”);”在创造它完美的作品。 当我给它的方法“recivedSms”,它没有工作。

public class Change_Password_Activity extends AppCompatActivity { 
EditText user_name,pass_wd; 
public EditText otpcode; 
private Button btn_submit; 
private String username,otp,password; 
private ProgressDialog prgDialog; 
private Typeface typeface; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_change__password_); 
    typeface = GlobalVariables.getTypeface(Change_Password_Activity.this); 
    prgDialog = new ProgressDialog(this); 
    // Set Progress Dialog Text 
    prgDialog.setMessage("Please wait..."); 
    // Set Cancelable as False 
    prgDialog.setCancelable(false); 

    otpcode = (EditText)findViewById(R.id.otpedittext); 
    user_name = (EditText) findViewById(R.id.edittext_ch_user); 
    pass_wd = (EditText) findViewById(R.id.edittext_ch_passwd); 
    btn_submit = (Button) findViewById(R.id.button_changepswd); 
    otpcode.setTypeface(typeface); 
    user_name.setTypeface(typeface); 
    pass_wd.setTypeface(typeface); 
    btn_submit.setTypeface(typeface); 



} 
public void recivedSms(String message) 
{ 
    try 
    { 
     int smsnumbr= Integer.parseInt(message); 
     otpcode.setText(smsnumbr); 

    } 
    catch (Exception e) 
    { 
     Log.e("error", String.valueOf(e)); 
    } 
+1

是否onCreate总是在receivedSms之前调用? –

+1

何时何地receivedSms叫? – zgc7009

+0

收到短信方法onCreate后调用 – susaine

回答

0

,如果你想设置smsnumbr到EDITTEXT然后它会给空指针异常如的setText(整数)机器人试图找到R.java资源与给定的整数作为标识文件。为了实现你想要的,你应该使用String.valueOf(..)来代替。

int smsnumbr= Integer.parseInt(message); 
otpcode.setText(String.valueOf(smsnumb)); 
+0

造成相同的异常 – susaine

+0

设置编辑文本中的整数不会导致空指针异常 – Tony

+0

你可以告诉我什么时候recivedSms(...)被调用吗? –