Carlos Landeras
Carlos Landeras

Reputation: 11063

Android - starting new Activity (java.lang.NullPointerException)

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!

Upvotes: 1

Views: 10259

Answers (5)

Rashmi.B
Rashmi.B

Reputation: 1787

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

Upvotes: 3

Prakash
Prakash

Reputation: 449

you are missing setContentView(R.layout.) in onCreate method. replace with the one you have.

:)

Upvotes: 0

himanshu
himanshu

Reputation: 1980

Yes Rashmi.B is right I also could not find the setContentView(R.layout.TestingActivity);.Add the line in ur onCreate() method and again run the code.

Upvotes: 0

kalpana c
kalpana c

Reputation: 2739

setContentView(R.layout.main); is not dispaly in your TestingActivity.class file. If it is not added please add this.

Upvotes: 1

Vamshi
Vamshi

Reputation: 1495

There is no setContentView(R.layout.yourxml); In your code

Upvotes: 2

Related Questions