Question

I've got a editText field. In this field, I've defined: android:inputType="numberDecimal"

In my code I've a button that has to be set the value of the editText. The value is an integer. I am doing this by: text.setText(saved);

But when I press the button, my app crashes. But I cant figure out why. It has to be something simple, but I can't figure it out. Someone who understands why the program crashes and what I have to do to fix it?

I have already tried it to convert it to a String, or using ""+saved, and some other simple basic tricks.

This is my log:

This is my code (All in the main activity):

public void onFetchOpenTimesheetsClick(View v) {
    newSavedData();
}

public void onFetchNewTimesheetsClick(View v) {
    addTableRow();
}

public void addAddTimesheetTableRow() {
    TableLayout table = (TableLayout)MainActivity.this.findViewById(R.id.mainTable);
    table.removeAllViews();
    TableRow row =     (TableRow)LayoutInflater.from(MainActivity.this).inflate(R.layout.additem, null);
    table.addView(row);
    table.requestLayout();
    restoreSavedData();
}

public void restoreSavedData() {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    int saved = settings.getInt("savedData", 0);
    EditText text = (EditText)MainActivity.this.findViewById(R.id.editText);
    text.setText(saved);
}

 public void newSavedData() {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    EditText text = (EditText)MainActivity.this.findViewById(R.id.editText;
    int toSave = Integer.valueOf(text.getText().toString());
    editor.putInt("savedData", toSave);
    editor.commit();
}

The relevant XML data in my XML file:

<EditText
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="7dp"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_gravity="center" >
</EditText>
Was it helpful?

Solution 3

Solution was simple. I've added a simple loop in my two save methods. This is to check each row for save data and load data. The problem I had before was because I had multiple rows with one xml layout. Therefor you need some extra coding to know each editText. You can see my solution below:

public void restoreSavedData() {
    // Restore preferences
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    TableLayout table = (TableLayout)MainActivity.this.findViewById(R.id.mainTable);
    for(int n = 0, s = table.getChildCount(); n < s; ++n) {
        TableRow row = (TableRow)table.getChildAt(n);
        EditText text = (EditText)row.findViewById(R.id.editText);
        int saved = settings.getInt(String.valueOf(n), 0);
        try {
            text.setText(String.valueOf(saved));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public void newSavedData() {
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    TableLayout table = (TableLayout)MainActivity.this.findViewById(R.id.mainTable);
    int toSave = 0;
    for(int n = 0, s = table.getChildCount(); n < s; ++n) {
        TableRow row = (TableRow)table.getChildAt(n);
        EditText text = (EditText)row.findViewById(R.id.editText);
        if(!text.getText().toString().equals(""))
        {
            toSave = Integer.valueOf(text.getText().toString());
            editor.putInt(String.valueOf(n), toSave);
            // Commit the edits!
            editor.commit();
        }
    }
}

Now the data of each row is saved and loaded when needed.

OTHER TIPS

In my code I've a button that has to be set the value of the editText. The value is an integer

use

text.setText(String.valueOf(saved));

setText() looks for a resource with the id if not found you get ResourceNotFoundException. Resource id is an int. So you are looking for setText(CharacterSequence);

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#valueOf(int)

findViewById looks for a view with the current inflated inflated layout. If not you end up with NullPointerException. EditText is in a different layout than the one set to activity. So if you inflate that layout you need to use the inflated view object to initialize your edittext.

I guess you want add edittext to table row. I guess you ahve a table layout to which you want to add rows with edittext

Declare EditText ed as a instance variable

    TableLayout table = (TableLayout)MainActivity.this.findViewById(R.id.mainTable);
    table.removeAllViews();
    TableRow row = new TableRow(this);
    ed = new EditText(this); 
    row.addView(ed);
    table.addView(row);
    table.requestLayout();

try this way text.settext(""+saved); where saved is your int.

Here is a test code i tried now which works for me

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" >
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="38dp"
    android:text="Button" /></LinearLayout>

And the class file

public class MainActivity extends Activity {

private Button btn;
private EditText edit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn = (Button) findViewById(R.id.button1);
    edit = (EditText) findViewById(R.id.editText1);

    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (edit.getText().toString().length() > 0)
                btn.setText("" + edit.getText().toString());
        }
    });
}}

please do like this

int value;//whatever the value is
TextView data = (TextView)findViewById(R.id.textView1);
data .setText(Integer.toString(value));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top