Android - 启动新的Activity(java.lang.NullPointerException)

时间:2012-01-13 07:42:06

标签: android android-activity sdk

早上好,我正在尝试使用SharedPreferences类,我创建了两个类“TestingActivity”,使用两个静态函数扩展android.app.Activity和AccountSettings,以便从SharedPreferences设置和恢复KEY。

从主要活动运行Intent时出现此错误:

   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

我使用以下代码从主Activity启动器启动活动:

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

活动类非常简单,具体如下:     包装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);           

    }


}

TestingActivity XML如下:

<?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>

和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>

我不明白为什么OnCreate方法会崩溃,有人能给我一些线索吗? 提前谢谢!

5 个答案:

答案 0 :(得分:3)

我看不到您的R.layout.main布局或TestingActivity.xml被调用。检查

答案 1 :(得分:2)

没有setContentView(R.layout.yourxml); 在你的代码中

答案 2 :(得分:1)

的setContentView(R.layout.main);未在您的Testing Activity.class文件中显示。如果没有添加,请添加此项。

答案 3 :(得分:0)

是的Rashmi.B是对的我也找不到setContentView(R.layout.TestingActivity);。在你的onCreate()方法中添加一行并再次运行代码。

答案 4 :(得分:0)

你在onCreate方法中缺少setContentView(R.layout。)。 替换你拥有的那个。

:)