Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7044189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:22:48+00:00 2026-05-28T02:22:48+00:00

good morning, I was trying the SharedPreferences Class and I have created two classes,

  • 0

good morning, I was trying the SharedPreferences Class and I have created two classes, “TestingActivity”, extending android.app.Activity and AccountSettings with two static functions to set and recover a KEY from SharedPreferences.

I am having this error while running Intent from the main activity:

   01-12 17:18:47.890: E/AndroidRuntime(5539): FATAL EXCEPTION: main
    01-12 17:18:47.890: E/AndroidRuntime(5539): java.lang.RuntimeException: Unable to    start activity ComponentInfo{es.LandeSoft/es.LandeSoft.TestingActivity}:   java.lang.NullPointerException
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at android.os.Handler.dispatchMessage(Handler.java:99)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at android.os.Looper.loop(Looper.java:130)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at android.app.ActivityThread.main(ActivityThread.java:3691)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at java.lang.reflect.Method.invoke(Method.java:507)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at dalvik.system.NativeStart.main(Native Method)
    01-12 17:18:47.890: E/AndroidRuntime(5539): Caused by: java.lang.NullPointerException
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at es.LandeSoft.TestingActivity.onCreate(TestingActivity.java:30)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    01-12 17:18:47.890: E/AndroidRuntime(5539):     ... 11 more

Im starting the activity from the main Activity launcher with this code:

Intent intent= new Intent(this,TestingActivity.class);
startActivity(intent);</i>

The Activity Class is real simple and it’s the following:
package es.LandeSoft;

  import es.LandeSoft.R;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class TestingActivity extends Activity {

    TextView lblDatos=null;
    EditText DatosAGuardar =null;

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState); 

        Button botonGuardar = (Button) findViewById(R.id.btnGuardar);
        Button botonRecuperar = (Button) findViewById(R.id.btnRecuperar);
        lblDatos = (TextView) findViewById(R.id.textView1);
         DatosAGuardar= (EditText) findViewById(R.id.txtPref);

        botonGuardar.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v) {               
                 AccountSettings.SavePassword(getApplicationContext(),"PASSWORD_APP",DatosAGuardar.getText().toString());
                ShowToast("Se ha guardado el password: " + DatosAGuardar.getText().toString() , 2500);
            }
        });

        botonRecuperar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String clave= AccountSettings.GetPassword(getApplicationContext(), "PASSWORD_APP");
                lblDatos.setText(clave);
                ShowToast("Clave Recuperada: " + clave,2000);

            }
        });

    }

     private void ShowToast(String MessageT, int Duracion)
     {
            Toast miTostada= new Toast(getApplicationContext());
            miTostada.setText(MessageT);
            miTostada.setDuration(Duracion);
            miTostada.setGravity(Gravity.CENTER,0,0);
            miTostada.show();

     }
}


**The AccountSettings.java is the following:**

    package es.LandeSoft;

    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;

    public class AccountSettings {


    public static String GetPassword (Context Contexto, String Key)
    {   
        SharedPreferences savedSession=        Contexto.getSharedPreferences(Key,Context.MODE_PRIVATE);
        return  savedSession.getString(Key, null);      
    }

    public static void SavePassword(Context context, String Key, String Value)
    {
        Editor editor = context.getSharedPreferences(Key,Context.MODE_PRIVATE).edit();
                editor.putString(Key, Value);           

    }


}

The TestingActivity XML is the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/TextViewInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/TextViewInfo_Text" />


    <EditText
        android:id="@+id/txtPref"
        android:inputType="text"        
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnGuardar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_guardar" />

     <Button
        android:id="@+id/btnRecuperar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_recuperar" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

and The Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="es.LandeSoft"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET"/>



    <application android:label="@string/app_name"
                 android:debuggable="true" 
                 android:icon="@drawable/landesofticon">
                 <activity android:name="es.LandeSoft.LandeSoftActivity"
                    android:label="@string/app_name">                   

                    <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>

                 <activity android:name="es.LandeSoft.FacebookClass"></activity>
                 <activity android:name="es.LandeSoft.TestingActivity"></activity>

    </application>
</manifest>

I dont understand why the OnCreate Method is crashing, can someone give me a clue?
Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:22:48+00:00Added an answer on May 28, 2026 at 2:22 am

    I cannot see your R.layout.main for your layout or your TestingActivity.xml being called. Check for that

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good Morning, I have a SharePoint site that I've been trying to fix up
Good morning I'm trying to get a table row (TR) that must have one
Good morning, Suppose I have a class public class Class { int something; int[]
Good morning, I have a .Net 2.0 runtime DLL that I am trying to
Good Morning Stack Overflow, I'm new to asp.net and have a problem I'm trying
Good morning all, i have a new question: I'm trying to save into mysql
Good Morning, I have created a structure to be a cut down version of
Good morning, I've successfully created a little app that registers itself under a custom
Good morning I am learning Java and trying to understand the access to class
Good Morning All, I have created a different background and a few other images

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.