title: Android - starting new Activity (java.lang.NullPointerException)
body: <p>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.</p>
  
  <p>I am having this error while running Intent from the main activity:</p>
  
  <pre><code>   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
  </code></pre>
  
  <p>Im starting the activity from the main Activity launcher with this code:</p>
  
  <pre><code>Intent intent= new Intent(this,TestingActivity.class);
  startActivity(intent);&lt;/i&gt;
  </code></pre>
  
  <p><strong>The Activity Class is real simple and it's the following:</strong>
      package es.LandeSoft;</p>
  
  <pre><code>  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);           
  
      }
  
  
  }
  </code></pre>
  
  <p><strong>The TestingActivity XML is the following:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:id="@+id/TextViewInfo"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/TextViewInfo_Text" /&gt;
  
  
      &lt;EditText
          android:id="@+id/txtPref"
          android:inputType="text"        
          android:layout_width="match_parent"
          android:layout_height="wrap_content" &gt;
          &lt;requestFocus /&gt;
      &lt;/EditText&gt;
  
      &lt;Button
          android:id="@+id/btnGuardar"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/btn_guardar" /&gt;
  
       &lt;Button
          android:id="@+id/btnRecuperar"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/btn_recuperar" /&gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>and The Android Manifest:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="es.LandeSoft"
        android:versionCode="1"
        android:versionName="1.0"&gt;
      &lt;uses-sdk android:minSdkVersion="8" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt; 
      &lt;uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"/&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  
  
  
      &lt;application android:label="@string/app_name"
                   android:debuggable="true" 
                   android:icon="@drawable/landesofticon"&gt;
                   &lt;activity android:name="es.LandeSoft.LandeSoftActivity"
                      android:label="@string/app_name"&gt;                   
  
                      &lt;intent-filter&gt;
                      &lt;action android:name="android.intent.action.MAIN" /&gt;
                      &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
                      &lt;/intent-filter&gt;
                  &lt;/activity&gt;
  
                   &lt;activity android:name="es.LandeSoft.FacebookClass"&gt;&lt;/activity&gt;
                   &lt;activity android:name="es.LandeSoft.TestingActivity"&gt;&lt;/activity&gt;
  
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>I dont understand why the OnCreate Method is crashing, can someone give me  a clue?
  Thanks in advance!</p>