0

I have created a code that worked when i had a regular button called "@+id/buttonSave_character" however, when i remove it and add a image button and try to save the values i have stored in edittext, it wont do it. what is wrong with the code?

another odd thing is that i can see that i havent defined the button in the code yet it still will save it (with the old regular button).

package com.daniel.darkheresy.character;

import com.daniel.darkheresy.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Character_Activity extends Activity {

   TextView name ;
   TextView home_world;
   TextView weapon_skill;
   TextView ballistic_skill;
   TextView strength;
   TextView toughness;
   TextView agility;
   TextView intelligence;
   TextView perception;
   TextView will_power;
   TextView fellowship;



   public static final String MyPREFERENCES = "MyPrefs" ;
   public static final String Name = "nameKey"; 
   public static final String Home_world = "home_worldKey"; 
   public static final String Weapon_skill = "weapon_skillKey"; 
   public static final String Ballistic_skill = "ballistic_skillKey"; 
   public static final String Strength = "strengthKey";
   public static final String Toughness = "toughnessKey";
   public static final String Agility = "agilityKey";
   public static final String Intelligence = "intelligenceKey";
   public static final String Perception = "perceptionKey";
   public static final String Will_power = "will_powerKey";
   public static final String Fellowship = "fellowshipKey";

   SharedPreferences sharedpreferences;

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



      name = (TextView) findViewById(R.id.editTextName);
      home_world = (TextView) findViewById(R.id.editTextHome_world);
      weapon_skill = (TextView) findViewById(R.id.editTextWeapon_skill);
      ballistic_skill = (TextView) findViewById(R.id.editTextBallistic_skill);
      strength = (TextView) findViewById(R.id.editTextStrength);
      toughness = (TextView) findViewById(R.id.editTextToughness);
      agility = (TextView) findViewById(R.id.editTextAgility);
      intelligence = (TextView) findViewById(R.id.editTextIntelligence);
      perception = (TextView) findViewById(R.id.editTextPerception);
      will_power = (TextView) findViewById(R.id.editTextWill_power);
      fellowship = (TextView) findViewById(R.id.editTextFellowship);

      sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

      if (sharedpreferences.contains(Name))
      {
         name.setText(sharedpreferences.getString(Name, ""));

      }
      if (sharedpreferences.contains(Home_world))
      {
         home_world.setText(sharedpreferences.getString(Home_world, ""));

      }
      if (sharedpreferences.contains(Weapon_skill))
      {
         weapon_skill.setText(sharedpreferences.getString(Weapon_skill, ""));

      }
      if (sharedpreferences.contains(Ballistic_skill))
      {
         ballistic_skill.setText(sharedpreferences.getString(Ballistic_skill, ""));

      }
      if (sharedpreferences.contains(Strength))
      {
         strength.setText(sharedpreferences.getString(Strength,""));

      }
      if (sharedpreferences.contains(Toughness))
      {
         toughness.setText(sharedpreferences.getString(Toughness,""));

      }
      if (sharedpreferences.contains(Agility))
      {
         agility.setText(sharedpreferences.getString(Agility,""));

      }
      if (sharedpreferences.contains(Intelligence))
      {
         intelligence.setText(sharedpreferences.getString(Intelligence,""));

      }
      if (sharedpreferences.contains(Perception))
      {
         perception.setText(sharedpreferences.getString(Perception,""));

      }
      if (sharedpreferences.contains(Will_power))
      {
         will_power.setText(sharedpreferences.getString(Will_power,""));

      }
      if (sharedpreferences.contains(Fellowship))
      {
         fellowship.setText(sharedpreferences.getString(Fellowship,""));

      }

   }



   public void run(View view){
      String n  = name.getText().toString();
      String hw  = home_world.getText().toString();
      String ws  = weapon_skill.getText().toString();
      String bs  = ballistic_skill.getText().toString();
      String s  = strength.getText().toString();
      String t  = toughness.getText().toString();
      String a  = agility.getText().toString();
      String i  = intelligence.getText().toString();
      String p  = perception.getText().toString();
      String wp  = will_power.getText().toString();
      String f  = fellowship.getText().toString();
      Editor editor = sharedpreferences.edit();
      editor.putString(Name, n);
      editor.putString(Home_world, hw);
      editor.putString(Weapon_skill, ws);
      editor.putString(Ballistic_skill, bs);
      editor.putString(Strength, s);
      editor.putString(Toughness, t);
      editor.putString(Agility, a);
      editor.putString(Intelligence, i);
      editor.putString(Perception, p);
      editor.putString(Will_power, wp);
      editor.putString(Fellowship, f);

      editor.commit(); 

   }

}

the code i have used comes from: http://www.tutorialspoint.com/android/android_shared_preferences.htm

PuchuKing33
  • 381
  • 3
  • 7
  • 19

1 Answers1

1

Perhaps you missed the line in your xml to set the on click handler.

Ensure your image button has the android:onClick property set to run like so:

android:onClick="run"
idunnololz
  • 8,058
  • 5
  • 30
  • 46