2016-04-05 23 views
0

setTextをedittextの値にするとエラーが発生します。私が設定した場合、与えられたIDは正しいですnullのオブジェクト参照で仮想メソッド 'void android.widget.EditText.setText(java.lang.CharSequence)'を呼び出そうとしましたヌルポインタ例外を発生させます。

ここでは、私はcant setText Otpコード " otpcode.setText( "12345"); " oncreateでそれは完璧に動作します。 私はそれを "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

いつ、どこでreceiveSmsが呼び出されますか? – zgc7009

+0

が受け取ったsmsメソッドonCreateの後での呼び出し – susaine

答えて

0

あなたはそれはのsetTextのようにnullポインタ例外を与えるEDITTEXTするsmsnumbrを設定しようとしている場合(整数)アンドロイドは、IDとして指定された整数でR.javaファイルからリソースを見つけようとします。代わりにString.valueOf(..)を使用する必要があります。

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

同じ例外が発生しました – susaine

+0

編集テキストの整数を設定してもNULLポインタ例外が発生しません – Tony

+0

recivedSms(...)が呼び出されたときに教えてください。 –