【问题标题】:edtitext default values comes in emulator not on deviceedtitext 默认值来自模拟器而不是设备
【发布时间】:2013-09-19 06:56:46
【问题描述】:

我遇到了一个奇怪的问题,我在 xml 中有几个 EditText 框,我已经给了它们默认值。我也在使用共享首选项,其中我保存用户输入的值。

问题是在模拟器中EditText 工作正常,但在真实的物理设备上值是空的,我错过了什么吗???

  <EditText
       android:id="@+id/etTRQPO"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:ems="10"
       android:inputType="numberDecimal"
       android:singleLine="true"
       android:text="15">
  </EditText>

java代码

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Getacc extends Activity {
    Button save6;

    EditText edtTRQPO, edtTRQGE, edtTRQFW, edtTRQGR, edtTRQBN, edtTRQLT,
            edtTRQPP, edtTRQCG;
    int tV, tW, tX, counterAC;
    String tsTRQPO, tsTRQGE, tsTRQFW, tsTRQGR, tsTRQBN, tsTRQLT, tsTRQPP,
            tsTRQCG;
    public static String FILE1 = "MyPrefsFile";
    SharedPreferences abcPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.getacc);
        save6 = (Button) findViewById(R.id.btGoresult);
        edtTRQPO = (EditText) findViewById(R.id.etTRQPO);
        edtTRQGE = (EditText) findViewById(R.id.etTRQGE);
        edtTRQFW = (EditText) findViewById(R.id.etTRQFW);
        edtTRQGR = (EditText) findViewById(R.id.etTRQGR);
        edtTRQBN = (EditText) findViewById(R.id.etTRQBN);
        edtTRQLT = (EditText) findViewById(R.id.etTRQLT);
        edtTRQPP = (EditText) findViewById(R.id.etTRQPP);
        edtTRQCG = (EditText) findViewById(R.id.etTRQCG);

        abcPref = getSharedPreferences(FILE1, 0);
        edtTRQPO.setText(abcPref.getString("tsTRQPO", ""));
        edtTRQGE.setText(abcPref.getString("tsTRQGE", ""));
        edtTRQFW.setText(abcPref.getString("tsTRQFW", ""));
        edtTRQGR.setText(abcPref.getString("tsTRQGR", ""));
        edtTRQBN.setText(abcPref.getString("tsTRQBN", ""));
        edtTRQLT.setText(abcPref.getString("tsTRQLT", ""));
        edtTRQPP.setText(abcPref.getString("tsTRQPP", ""));
        edtTRQCG.setText(abcPref.getString("tsTRQCG", ""));

        save6.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if ((!edtTRQPO.getText().toString().equals(""))
                        && (!edtTRQGE.getText().toString().equals(""))
                        && (!edtTRQFW.getText().toString().equals(""))
                        && (!edtTRQGR.getText().toString().equals(""))
                        && (!edtTRQBN.getText().toString().equals(""))
                        && (!edtTRQLT.getText().toString().equals(""))
                        && (!edtTRQPP.getText().toString().equals(""))
                        && (!edtTRQCG.getText().toString().equals(""))) {
                    // TODO Auto-generated method stub
                    counterAC =1;
                    abcPref = getSharedPreferences(FILE1, 0);
                    SharedPreferences.Editor editor = abcPref.edit();
                    editor.putString("tsTRQPO", edtTRQPO.getText().toString());
                    editor.putString("tsTRQGE", edtTRQGE.getText().toString());
                    editor.putString("tsTRQFW", edtTRQFW.getText().toString());
                    editor.putString("tsTRQGR", edtTRQGR.getText().toString());
                    editor.putString("tsTRQBN", edtTRQBN.getText().toString());
                    editor.putString("tsTRQLT", edtTRQLT.getText().toString());
                    editor.putString("tsTRQPP", edtTRQPP.getText().toString());
                    editor.putString("tsTRQCG", edtTRQCG.getText().toString());
                    editor.putInt("counterac", counterAC);
                    editor.commit();
                    Toast message = Toast.makeText(Getacc.this,
                            "Values are saved", 2000);
                    message.setGravity(Gravity.BOTTOM, 0, 0);
                    message.show();
                    Intent opentime = new Intent("com.sports.sport.TIME");
                    startActivity(opentime);
                    onPause();
                } else {
                    Toast failz = Toast.makeText(Getacc.this,
                            "Values are not Entered", 2000);
                    failz.setGravity(Gravity.BOTTOM, 0, 0);
                    failz.show();
                }
            };
        });

    }
}

【问题讨论】:

  • 您是否在EditText 中设置SharedPrefrences 值?
  • 将代码发布在您为 EditText 保存价值的位置
  • 您无法获取它,因为在 onCreate 您正在从共享首选项中检索您的数据,因此它将为空..
  • 你的意思是:- edtTRQPO.setText(abcPref.getString("tsTRQPO", ""));
  • 您尝试在真机上运行应用程序有多少次??

标签: android sharedpreferences android-edittext


【解决方案1】:

当您第一次在真实设备上运行应用程序时,它将显示默认值,而不是 SharedPreferences 中的值,因为 SharedPreferences 中的值是空的。如果 SharedPreferences 有空值,当您第二次打开应用程序时,EditText 将显示 SharedPreferences 中的空值。您需要应用一些值检查,例如

 if(user_entered_values_in_edittext){
    //Store values in SharedPreferences otherwise not.
 }

 // On app resume - inside onResume() or onCreate()
 if(SharedPreferences have empty value or no value){
    //Show your EditText default value that you have defined in xml file.
 }

更正 Getacc.java 文件...

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Getacc extends Activity {
   Button save6;

   EditText edtTRQPO, edtTRQGE, edtTRQFW, edtTRQGR, edtTRQBN, edtTRQLT,
        edtTRQPP, edtTRQCG;
   int tV, tW, tX, counterAC;
   String tsTRQPO, tsTRQGE, tsTRQFW, tsTRQGR, tsTRQBN, tsTRQLT, tsTRQPP,
        tsTRQCG;
   public static String FILE1 = "MyPrefsFile";
   SharedPreferences abcPref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.getacc);
    save6 = (Button) findViewById(R.id.btGoresult);
    edtTRQPO = (EditText) findViewById(R.id.etTRQPO);
    edtTRQGE = (EditText) findViewById(R.id.etTRQGE);
    edtTRQFW = (EditText) findViewById(R.id.etTRQFW);
    edtTRQGR = (EditText) findViewById(R.id.etTRQGR);
    edtTRQBN = (EditText) findViewById(R.id.etTRQBN);
    edtTRQLT = (EditText) findViewById(R.id.etTRQLT);
    edtTRQPP = (EditText) findViewById(R.id.etTRQPP);
    edtTRQCG = (EditText) findViewById(R.id.etTRQCG);

    abcPref = getSharedPreferences(FILE1, 0);

    // First get All values stored in SharedPreferences 
      tsTRQPO = abcPref.getString("tsTRQGE", null);
      tsTRQGE = abcPref.getString("tsTRQGE", null);
      tsTRQFW = abcPref.getString("tsTRQFW", null);
      tsTRQGR = abcPref.getString("tsTRQGR", null);
      tsTRQBN = abcPref.getString("tsTRQBN", null);
      tsTRQLT = abcPref.getString("tsTRQLT", null);
      tsTRQPP = abcPref.getString("tsTRQPP", null);
      tsTRQCG = abcPref.getString("tsTRQCG", null);

      // Check if values are not null
      if(tsTRQPO != null && tsTRQGE != null && tsTRQFW != null && tsTRQGR!= null && tsTRQBN != null && tsTRQLT != null && tsTRQPP != null && tsTRQCG != null){
          edtTRQPO.setText(tsTRQPO);
          edtTRQGE.setText(tsTRQGE);
          edtTRQFW.setText(tsTRQFW);
          edtTRQGR.setText(tsTRQGR);
          edtTRQBN.setText(tsTRQBN);
          edtTRQLT.setText(tsTRQLT);
          edtTRQPP.setText(tsTRQPP);
          edtTRQCG.setText(tsTRQCG);

      }else{
        //Do nothing.. EditText will show Default values defined in xml file
      }

    save6.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if ((!edtTRQPO.getText().toString().equals(""))
                    && (!edtTRQGE.getText().toString().equals(""))
                    && (!edtTRQFW.getText().toString().equals(""))
                    && (!edtTRQGR.getText().toString().equals(""))
                    && (!edtTRQBN.getText().toString().equals(""))
                    && (!edtTRQLT.getText().toString().equals(""))
                    && (!edtTRQPP.getText().toString().equals(""))
                    && (!edtTRQCG.getText().toString().equals(""))) {
                // TODO Auto-generated method stub
                counterAC =1;
                abcPref = getSharedPreferences(FILE1, 0);
                SharedPreferences.Editor editor = abcPref.edit();
                editor.putString("tsTRQPO", edtTRQPO.getText().toString());
                editor.putString("tsTRQGE", edtTRQGE.getText().toString());
                editor.putString("tsTRQFW", edtTRQFW.getText().toString());
                editor.putString("tsTRQGR", edtTRQGR.getText().toString());
                editor.putString("tsTRQBN", edtTRQBN.getText().toString());
                editor.putString("tsTRQLT", edtTRQLT.getText().toString());
                editor.putString("tsTRQPP", edtTRQPP.getText().toString());
                editor.putString("tsTRQCG", edtTRQCG.getText().toString());
                editor.putInt("counterac", counterAC);
                editor.commit();
                Toast message = Toast.makeText(Getacc.this,
                        "Values are saved", 2000);
                message.setGravity(Gravity.BOTTOM, 0, 0);
                message.show();
                Intent opentime = new Intent("com.sports.sport.TIME");
                startActivity(opentime);
                onPause();
            } else {
                Toast failz = Toast.makeText(Getacc.this,
                        "Values are not Entered", 2000);
                failz.setGravity(Gravity.BOTTOM, 0, 0);
                failz.show();
            }
        };
    });
  }
}

【讨论】:

    【解决方案2】:

    共享首选项保存在缓存中。如果您从 IDE 重新启动您的应用程序,则不会清除共享首选项。如果你想清除它,你应该

    1. 卸载应用程序
    2. 转到设置 > 程序 > 查找您的程序 > 清除缓存。

    只有这样它是空的。所以也许你的问题是你在模拟器上有旧值,而在设备上它是新值(反之亦然)

    【讨论】:

    • 我在模拟器上擦除了数据,并且默认情况下存在值,因为设备上没有默认值,所以它很奇怪。
    【解决方案3】:

    第一次,应用程序中没有 SharedPreferences,所以abcPref.getString("tsTRQPO", "") 和其他导致这里给出的默认值是空的。通过填写所有编辑文本保存数据后,您将在下一次得到正确的数据。

    为了检查,在从 SharedPreferences 获取数据时尝试设置一些默认值。对于 ex edtTRQPO.setText(abcPref.getString("tsTRQPO", "15")); 并检查它是否第一次加载值 15。

    【讨论】:

      【解决方案4】:

      这是因为您试图从 SharedPreferences 中获取 OnCreate 方法中的值,该方法当前有空白表示 null, 现在看,当您启动您的应用程序时,它将首先执行 OnCreate 方法,您尝试从 sharedPreferences 中获取值,当您将值保存在 save6.setOnClickListener

      这是什么?

      注意:在 sharedPreferences 中保存后获取值

      【讨论】:

        猜你喜欢
        • 2016-10-22
        • 2016-09-16
        • 2011-03-24
        • 2012-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-09
        相关资源
        最近更新 更多