0

在我的应用程序中,我有三个编辑文本,并且我使用共享首选项来存储这些数字。我想从共享偏好中获取数字并显示在编辑文本中,我也想编辑文本。当我运行下面的代码时,我得到了 NullPointerException。有人可以帮助我吗?

     public void onCreate(Bundle savedInstanceState)
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_cell_number);
        init();
        /*SharedPreferences sharedPref=this.getSharedPreferences("MY_PREF_2",MODE_WORLD_READABLE);
        SharedPreferences.Editor shredPref_Editor=sharedPref.edit();
        shredPref_Editor.putString(MY_ACTIVITY,"changeSMSnumber");
        shredPref_Editor.commit();


        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ChangeSmsNumber_Activity.this);
        String servername = settings.getString("sharedPreferencesKey", "defaultValue");
        //server.setText(servername);  
        */      



        btnSubmit.setOnClickListener(new OnClickListener() 
        {

            @SuppressWarnings("unused")
            @Override
            public void onClick(View arg0) 
            {
                // TODO Auto-generated method stub
                String numberISD=edtxt_ISD_Code1.getText().toString();


                if(edtxt_ISD_Code1.getText().toString().isEmpty()) 
                {
                    Toast.makeText(getApplicationContext(),"Enter ISD 1st Number * ", Toast.LENGTH_SHORT).show();
                }
                else if(edtxt_Cell_Number1.getText().toString().isEmpty())
                {
                    Toast.makeText(getApplicationContext(),"Enter 1st Number *", Toast.LENGTH_SHORT).show();
                }

                else

                {


                    String strSign1 = edtxt_PlusSign1.getText().toString();
                    String strSign2 = edtxt_PlusSign2.getText().toString();
                    String strSign3 = edtxt_PlusSign3.getText().toString();
                    String strSign4 = edtxt_PlusSign4.getText().toString();
                    String strSign5 = edtxt_PlusSign5.getText().toString();


                    String strIsd1 = edtxt_ISD_Code1.getText().toString();
                    String strIsd2 = edtxt_ISD_Code2.getText().toString();
                    String strIsd3 = edtxt_ISD_Code3.getText().toString();
                    String strIsd4 = edtxt_ISD_Code4.getText().toString();
                    String strIsd5 = edtxt_ISD_Code5.getText().toString();

                    String strNum1 = edtxt_Cell_Number1.getText().toString();
                    String strNum2 = edtxt_Cell_Number2.getText().toString();
                    String strNum3 = edtxt_Cell_Number3.getText().toString();
                    String strNum4 = edtxt_Cell_Number4.getText().toString();
                    String strNum5 = edtxt_Cell_Number5.getText().toString();


                    String final_Num_1 =  strSign1 + strIsd1 + strNum1;
                    String final_Num_2 =  strSign2 + strIsd2 + strNum2;
                    String final_Num_3 =  strSign3 + strIsd3 + strNum3;
                    String final_Num_4 =  strSign4 + strIsd4 + strNum4;
                    String final_Num_5 =  strSign5 + strIsd5 + strNum5;

                    SharedPreferences settings = ChangeSmsNumber_Activity.this.getSharedPreferences("MyPref_CellNumber",0);
                    strGetCellNum = settings.getString("num1", "n/a"); 
                    final_Num_1=strGetCellNum;


                }

            }
        });
    }

请指导我。

4

1 回答 1

0
sharedPref=this.getSharedPreferences("MY_PREF_2",MODE_PRIVATE);
String number= sharedPref.getString("KeyValue","defaultValue")//you can use defaultValue=""

用于在 Edittext 中设置

editText.setText(number);
于 2013-04-10T10:04:50.317 回答