Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.app.AlertDialog.Builder;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.app.AlertDialog;
  11. import android.widget.EditText;
  12.  
  13. public class userPreferences extends Activity {
  14. EditText editText;
  15. Button save;
  16. public static final String NAME="UserName";
  17. SharedPreferences sharedPreferences;
  18. SharedPreferences.Editor editor;
  19. String n;
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.userpreferences);
  25. editText=(EditText)findViewById(R.id.editText);
  26. save=(Button)findViewById(R.id.button);
  27. sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
  28. save.setOnClickListener(new View.OnClickListener() {
  29. final AlertDialog.Builder alertDialog = new AlertDialog.Builder(userPreferences.this);
  30.  
  31. @Override
  32. public void onClick(View arg0) {
  33.  
  34. alertDialog.setTitle("Alert Dialog");
  35. alertDialog.setMessage("Are you sure you want to save?");
  36. alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  37. @Override
  38. public void onClick(DialogInterface dialog, int which) {
  39. n=editText.getText().toString();
  40. sharedPreferences=getPreferences(MODE_PRIVATE);
  41. editor=sharedPreferences.edit();
  42. editor.putString(NAME,n);
  43. editor.apply();
  44. startActivity(new Intent(userPreferences.this,Welcome.class));
  45.  
  46. }
  47. });
  48. alertDialog.setNegativeButton("No", null);
  49. alertDialog.show();
  50.  
  51.  
  52. }
  53. });
  54.  
  55. }
  56.  
  57.  
  58. }
  59.  
  60. import android.app.Activity;
  61. import android.content.Context;
  62. import android.content.Intent;
  63. import android.content.SharedPreferences;
  64. import android.os.Bundle;
  65. import android.view.Menu;
  66. import android.view.MenuItem;
  67. import android.widget.TextView;
  68.  
  69. public class Welcome extends Activity {
  70.  
  71. TextView textView;
  72. SharedPreferences sharedPreferences;
  73. public static final String NAME="UserName";
  74. SharedPreferences.Editor editor;
  75. String n;
  76.  
  77. @Override
  78. protected void onCreate(Bundle savedInstanceState) {
  79. super.onCreate(savedInstanceState);
  80. setContentView(R.layout.welcome);
  81. textView=(TextView)findViewById(R.id.textView);
  82. textView.setText("Welcome," + editor.putString(NAME, n));
  83. sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
  84. editor.apply();
  85.  
  86. }
  87.  
  88. @Override
  89. public void onBackPressed() {
  90. super.onBackPressed();
  91. startActivity(new Intent(Welcome.this, MainActivity.class));
  92. return;
  93.  
  94.  
  95. }
  96.  
  97. Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference
  98.  
  99. sharedPreferences=getPreferences(MODE_PRIVATE);
  100.  
  101. sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
  102. if(!sharedPreferences.getString(NAME,"Default value").equals("Default value")){
  103. startActivity(new Intent(userPreferences.this, Welcome.class));
  104. finish();
  105. }
  106.  
  107. sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
  108. textView=(TextView)findViewById(R.id.textView);
  109. textView.setText("Welcome," + sharedPreferences.getString(NAME,"Default value"));
  110. editor = = sharedPreferences.edit();
  111. editor.putString(NAME, sharedPreferences.getString(NAME,"Default value"));
  112. editor.apply();
  113.  
  114. // open your preferences using the name you originally used to create it
  115. sharedPreferences = getSharedPreferences(NAME, MODE_PRIVATE);
  116.  
  117. // open for editing
  118. editor = sharedPreferences.edit();
  119.  
  120. // you probably don't want to use the same name here as preferences name
  121. editor.putString(NAME,n);
  122. editor.apply();
  123.  
  124. sharedPreferences = getSharedPreferences(NAME, MODE_PRIVATE);
  125. String name = sharedPreferences.getString(NAME);
  126.  
  127. textView.setText("Welcome," + name);
  128.  
  129. @Override
  130. protected void onCreate(Bundle savedInstanceState) {
  131. super.onCreate(savedInstanceState);
  132. setContentView(R.layout.welcome);
  133. textView=(TextView)findViewById(R.id.textView);
  134.  
  135. sharedPreferences = getSharedPreferences(NAME, MODE_PRIVATE);
  136. String name = sharedPreferences.getString(NAME);
  137.  
  138. textView.setText("Welcome," + name);
  139. editor = sharedPreferences.edit();
  140. editor.putString(NAME, name);
  141. editor.apply();
  142.  
  143. }
  144.  
  145. TextView textView;
  146. SharedPreferences sharedPreferences;
  147. public static final String NAME = "UserName";
  148. public static final String defaultName = "";
  149.  
  150. @Override
  151. protected void onCreate(Bundle savedInstanceState) {
  152. super.onCreate(savedInstanceState);
  153. setContentView(R.layout.welcome);
  154. textView = (TextView) findViewById(R.id.textView);
  155.  
  156. sharedPreferences = getSharedPreferences(NAME, Context.MODE_PRIVATE);
  157. textView.setText("Welcome," + sharedPreferences.getString(NAME, defaultName));
  158. }
  159.  
  160. EditText editText;
  161. Button save;
  162. public static final String NAME="UserName";
  163. public static final String default = "";
  164. SharedPreferences sharedPreferences;
  165. SharedPreferences.Editor editor;
  166. String n;
  167.  
  168. @Override
  169. protected void onCreate(Bundle savedInstanceState) {
  170. super.onCreate(savedInstanceState);
  171. setContentView(R.layout.userpreferences);
  172. editText = (EditText) findViewById(R.id.editText);
  173. save = (Button) findViewById(R.id.button);
  174. sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
  175. if (sharedPreferences.getString(NAME, default).length() > 0) {
  176. // The user has already entered a username, so start the Welcome activity
  177. startActivity(new Intent(userPreferences.this, Welcome.class));
  178. finish();
  179. }
  180. // put the rest of your onCreate code here
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement