Why doesn't my "SharedPreferences" work? - java

The Problem is, that the Text field which is in "Wizard2Activity" just shows "nothing" and not what the user has typed in the "EditText" in "Wizard1".
Wizard1.java:
package com.CENSORED.CENSORED;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Wizard1 extends Activity {
public static final String PREFS ="examplePrefs";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wizard1);
final EditText et = (EditText)findViewById(R.id.editText1);
Button nextAct = (Button)findViewById(R.id.button1);
nextAct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
String message = et.getText().toString();
SharedPreferences examplePrefs = getSharedPreferences(PREFS, 0);
Editor editor = examplePrefs.edit();
editor.putString("usermessage", message);
editor.commit();
Intent i = new Intent(getApplicationContext(),
Wizard2Activity.class);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.wizard1, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And Wizard 2 Activity:
package com.CENSORED.CENSORED;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class Wizard2Activity extends Activity {
public static final String PREFS ="examplePrefs";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wizard2);
TextView tv = (TextView)findViewById(R.id.textView1);
SharedPreferences example = getSharedPreferences(PREFS, 0);
String userString = example.getString("userMessage", "nothing");
tv.setText(userString);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.wizard2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thank you for your help! :)

You should change
String userString = example.getString("userMessage", "nothing");
to
String userString = example.getString("usermessage", "nothing");
key is usermessage. It's just typo.

Because the method
editor.putString("usermessage", message);
and
example.getString("userMessage", "nothing");
have different key.

Related

Simple Android searchbar into a string

MY GOAL:take whatever was placed into the search bar and then turn it into a string which can be a variable used across multiple activities. I am wondering on line 92 , would i need a if statement of some sort to see if there are any integers passed. That then would need to be converted into a string. Or Would the code work fine the way it is. Thank you for reading!
package com.karanvir.search;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Intent intentGoogle;
Random rn;
SharedPreferences urls;
AutoCompleteTextView searchBar;
public static String urlGlobal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
searchBar=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
Button button=(Button) findViewById(R.id.button);
Intent intentGoogle= new Intent(getApplicationContext(),Main2Activity.class);
rn= new Random();
urls=this.getSharedPreferences("com.karanvir.search", Context.MODE_PRIVATE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings4) {
startActivity(intentGoogle);
return true;
} else if(id ==R.id.action_settings2){
return true;
}else if (id==R.id.action_settings3){
return true;
}else if(id==R.id.action_settings1){
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.alert_dark_frame)
.setTitle("About")
.setMessage("stuff");
return true;
}
return super.onOptionsItemSelected(item);
}
public void jump(View view){
//intnet changing target of our code
urlGlobal=searchBar.getText().toString();
if
//public static String urlGlobal=
/* urls.edit().putString("url",searchBar.getText().toString()).apply();
String Stringurls=urls.getString("url","");*/
int pageJump = rn.nextInt(3)+1;
if (pageJump==1){
startActivity(intentGoogle);
} else if (pageJump==2){
} else if(pageJump==3){
}
}
}
Turns out it does convert it into a string so there is no need for further complications.

Passing an drawable image from 1st activity to 2nd does not work (only from 2nd to 1ast does) Android

enter code hereThis is my First Activity::> I need:
1) select an option
2) click send an image (from drawable) should go to the second activity but it does not only from 2nd to 1st works fine all my xml and manifest files are correct (I double checked)
package com.example.damianlopez.myassignment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button toBob;
private ImageView burgerImage;
private RadioGroup radioGroupChoice;
private RadioButton radBurger;
private RadioButton radScare;
private RadioButton radSick;
private final int MY_SELECTION = 111;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.activity_main);
//get radio Buttons
//radioGroupChoice = (RadioGroup) findViewById(R.id.radGroupChoice);
radBurger = (RadioButton)findViewById(R.id.radBurger);
radScare = (RadioButton)findViewById(R.id.radScare);
radSick = (RadioButton)findViewById(R.id.radSick);
//display app icon
ActionBar ab = getSupportActionBar();
ab.setDisplayShowHomeEnabled(true);
ab.setIcon(R.mipmap.ic_launcher);
//getting Intent
Intent getIntent = getIntent();
Bundle myBundle2 = getIntent.getExtras();
setResult(Activity.RESULT_OK, getIntent);
//
}catch(Exception e ){
}
}//End onCreate()
public void onClickButton(View v){
try {
//get radio gruop id's
radioGroupChoice = (RadioGroup) findViewById(R.id.radGroupChoice);
int mySelection = radioGroupChoice.getCheckedRadioButtonId();
if(radioGroupChoice.getCheckedRadioButtonId() == -1){
Toast.makeText(getApplicationContext(), "Please make a selection", Toast.LENGTH_LONG).show();
}else {
//creating new Intent
Intent msgToBob = new Intent(MainActivity.this, Main22Activity.class);
Bundle myBundle = new Bundle();
myBundle.putInt("imageChoice", mySelection);
msgToBob.putExtras(myBundle);
startActivityForResult(msgToBob, MY_SELECTION);
}
}catch(Exception e ){
}
}//End of onClick()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent myData) {
super.onActivityResult(requestCode, resultCode, myData);
Log.i("OnActivityResult() call", "***********");
radioGroupChoice = (RadioGroup) findViewById(R.id.radGroupChoice);
int selected = radioGroupChoice.getCheckedRadioButtonId();
//get Images
burgerImage = (ImageView) findViewById(R.id.pumpkin_burger);
if(radioGroupChoice.getCheckedRadioButtonId() == -1){
Toast.makeText(getApplicationContext(), "Please make a selection", Toast.LENGTH_LONG).show();
}
else {
switch (requestCode) {
case MY_SELECTION: {
if (resultCode == Activity.RESULT_OK) {
if (radBurger.isChecked()) {
burgerImage.setImageResource(R.drawable.pumpkin_burger);
} else if (radScare.isChecked()) {
burgerImage.setImageResource(R.drawable.pumpkin_scared);
} else if (radSick.isChecked()) {
burgerImage.setImageResource(R.drawable.pumpkin_sick);
};
}
break;
}//end case
}//end switch
}//end else if
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();``
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is my Second Activity===>
package com.example.damianlopez.myassignment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Main22Activity extends AppCompatActivity {
private RadioGroup radioGroupChoice2;
private RadioButton radBurger;
private RadioButton radScare;
private RadioButton radSick;
private Button toAlice;
private ImageView currentImage;
private ImageView scareImage;
private ImageView sickImage;
private final int MY_SELECTION2 = 222;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main22);
//display app icon
ActionBar ab = getSupportActionBar();
ab.setDisplayShowHomeEnabled(true);
ab.setIcon(R.mipmap.ic_launcher);
//Buttons
radioGroupChoice2 = (RadioGroup)findViewById(R.id.radGroupChoice);
//toAlice = (Button) findViewById(R.id.sndToAlice);
//get the intent called
Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
myLocalIntent.putExtras(myBundle);
setResult(Activity.RESULT_OK, myLocalIntent);
}
public void onClickButton2(View v){
try {
radioGroupChoice2 = (RadioGroup) findViewById(R.id.radGroupChoice);
if (radioGroupChoice2.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please make a selection", Toast.LENGTH_LONG).show();
} else {
Intent myIntent2 = new Intent();
Bundle myBundle2 = new Bundle();
int myImage = radioGroupChoice2.getCheckedRadioButtonId();
myBundle2.putInt("selectedImage", myImage);
myIntent2.putExtras(myBundle2);
setResult(Activity.RESULT_OK, myIntent2);
}
}catch(Exception e) {
}
finish();
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main22, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

how to direct to another activity after successful login, Android Developer Studio

as i said, how do i direct to the next activity after successfully login? im kinda stuck for a while now, and it is necessary for this to run. im practicing my android skills awhile back. so i am using android developer studio for my practices of developing android. this is my code. hope you guys can help.
`package com.mikesaclolo.pinasarap;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends Activity {
Button b1,b2;
EditText ed1,ed2;
TextView tx1;
int counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
b2=(Button)findViewById(R.id.button2);
tx1=(TextView)findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}`
start activity using Intent :
if(ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(MainActivity.this,LoginActivity.class); // redirecting to LoginActivity.
startActivity(intent);
}

error: class, interface or enum expected at void

I've been stuck on this problem for a while, I get error:
class, interface or enum expected
Any help will be appreciated? I've been stuck on this problem and my friends cant figure it out.
here is the code:
package com.example.vengelen.knitting;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
public class Rectangular extends ActionBarActivity {
EditText amount1;
EditText amount2;
EditText amount3;
TextView tt;
Button calculate;
double w=0;
double x=0;
double y=0;
double z=0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rectangular);
initControls();
}
private void initControls()
{
amount1=(EditText)findViewById(R.id.editText);
amount2=(EditText)findViewById(R.id.editText3);
amount3=(EditText)findViewById(R.id.editText2);
tt=(TextView)findViewById(R.id.editText4);
calculate=(Button)findViewById(R.id.button9);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
EditText input;
EditText output;
Button one;
private void calculate()
{
w=Double.parseDouble(amount3.getText().toString());
x=Double.parseDouble(amount1.getText().toString());
y=Double.parseDouble(amount2.getText().toString());
z=(x+y)*(y+w);
tt.setText(Double.toString(z));
}
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rectangular);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_selector, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You have unnecesarry end brace just before your method onCreate:
}
^^
#Override
protected void onCreate(Bundle savedInstanceState)
This means that compiler thinks that by that brace, your class definition has completed and then sees a new method definition which should be within another class and hence complains about the same. As a best practice, you should always format your code.

Android Studio App Unfortunately has stopped

I'm new to Android and I was practicing, when i finished this practice app i ran it but it says "unfortunately has stopped" where is the problem?My app looks like a simple log in screen and i don't think there's a problem with the XML file.
package com.mycompany.testbutton;
import android.media.Image;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
Button buttonLog = (Button)findViewById(R.id.button1);
EditText userName = (EditText)findViewById(R.id.usernameText);
EditText passWord = (EditText)findViewById(R.id.passwordText);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonLog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String username = String.valueOf(userName.getText());
String password = String.valueOf(passWord.getText());
String allTogetherToast = "Your Email is " + username + " and your Password is " + password;
Toast.makeText(MainActivity.this, allTogetherToast, Toast.LENGTH_LONG).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Initialize these
Button buttonLog = (Button)findViewById(R.id.button1);
EditText userName = (EditText)findViewById(R.id.usernameText);
EditText passWord = (EditText)findViewById(R.id.passwordText);
after your
setContentView(R.layout.activity_main)
so your code will be like this
package com.mycompany.testbutton;
import android.media.Image;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
Button buttonLog;
EditText userName;
EditText passWord;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonLog = (Button)findViewById(R.id.button1);
userName = (EditText)findViewById(R.id.usernameText);
passWord = (EditText)findViewById(R.id.passwordText);
buttonLog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String username = String.valueOf(userName.getText());
String password = String.valueOf(passWord.getText());
String allTogetherToast = "Your Email is " + username + " and your Password is " + password;
Toast.makeText(MainActivity.this, allTogetherToast, Toast.LENGTH_LONG).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
Move
Button buttonLog = (Button)findViewById(R.id.button1);
EditText userName = (EditText)findViewById(R.id.usernameText);
EditText passWord = (EditText)findViewById(R.id.passwordText);
after setContentView(....) inside onCreate(...)
Corrected:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonLog = (Button)findViewById(R.id.button1);
EditText userName = (EditText)findViewById(R.id.usernameText);
EditText passWord = (EditText)findViewById(R.id.passwordText);
Views available only after setContentView(....)

Categories

Resources