Edit starts
Sorry for messing up my code. All of you pointed out various mistakes and you all are correct. But what really happened is my original code was correct up to certain extend. Before posting the code here I changed my original code to check something and I used that edited code here forgetting that I changed, that's the reason you saw some wrong textviews, capitalization etc. Apologize for that.
I checked my code replacing the textview that located on the other activity with a text view on the same activity where the button is and it worked fine. So I guess my Intent to start another activity caused the error. It's the first time I tried using Intent. All I used is
Intent intent=new Intent(this, ViewAccount.class);
startActivity(intent);
But it seems that doesn't work. To cut it short, I have three activities activity1 contain editText1 editText2 and a save button. When save button clicked data from the edtiText1 and editText2 stored using sharedPreferences. activity2 contain a button called show. When button show is clicked activity3 should be opened up with saved values on it.
I hope I didn't make it more complicated.
Edit Ends.
Old post
Can somebody help me out to find what am I doing wrong. I am just a newbie to the Java and Android programming world. I am just trying to make a sample app where I will save and retrieve data using SharedPreferences. Saving part is working fine, I also put a Toast at saving. But when I click the button to retrieve data app get crashed. Please help me out. While saving I used Name and Age as keys.
Following is the java code I used for retrieve data:
package com.xyz.abc;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class Card extends Activity{
public static final String DEFAULT="N/A";
TextView showsname,showage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card);
showname=(TextView)findViewById(R.id.showname);
showage=(TextView) findViewById(R.id.showage);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.card, 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);
}
public void ViewAC(View view) {
SharedPreferences sharedPreferences=getSharedPreferences("data", Context.MODE_PRIVATE);
String SName=sharedPreferences.getString("Name",DEFAULT);
String sAge=sharedPreferences.getString("Age",DEFAULT);
if(SName.equals(DEFAULT)|| sAge.equals(DEFAULT))
{
Toast.makeText(this,"There is no account exist",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this,"Account loaded successfully",Toast.LENGTH_LONG).show();
showsitename.setText(sName);
showurl.setText(sAge);
}
Intent intent=new Intent(this, ViewAccount.class);
startActivity(intent);
}
}
Following is the xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.xyz.abc.ViewAccount"
android:orientation="vertical">
<TextView
android:text="Website Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showname"/>
<TextView
android:text="URL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showage"/>
</LinearLayout>