-2

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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3765415
  • 99
  • 1
  • 5
  • 17
  • showsitename.setText(sName); showurl.setText(sAge);i gues you are using wrong textview here your textviwes are showname and showage – kgandroid Jul 26 '14 at 21:26
  • 1
    Post your errors from LogCat – arleitiss Jul 26 '14 at 21:26
  • The code threw an exception. View the trace logs for a relevant cause. – user2864740 Jul 26 '14 at 21:26
  • 1
    Read [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/q/23353173/655987) – yildirimyigit Jul 26 '14 at 21:32
  • kgandroid is correct. The TextViews are wrong. Plus your code has three other mistakes. The main one being that SName shouldn't be capitalized when you set it. Also, this is wrong: TextView showsname,showage; (you added an s inside showname). – Stephan Branczyk Jul 26 '14 at 21:34
  • I don't even understand how you were able to compile this. It shouldn't even compile in the first place. Not only that, but Eclipse or Android Studio should underline some of those syntax errors with red squiggly underlines. Do you have that turned off for some reason? Or are you not using Eclipse, nor Android Studio? Or perhaps you're using Eclipse from a different perspective? – Stephan Branczyk Jul 26 '14 at 21:36
  • yeah.there is a lot of compilation error in this code...correct those in the first place – kgandroid Jul 26 '14 at 21:39

1 Answers1

1

First of all, you should include your LogCat, since it will usually give you the exact cause of the error.

Second of all, I don't see a button in your XML nor an "onClick". What are you clicking to execute the code?

Now to the code.

This is probably the cause of the crash:

showsitename.setText(sName);
showurl.setText(sAge);

You never define Textviews called "showsitename" and "showurl". You do have Textviews called "showsname" and "showage". Were you intending to use those?

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
AS98
  • 104
  • 4