-1

I made a layout xml for the settings of my app. I had a preference and I would like clicking on this will be sent to another activity. In the file of settings.java I put the following code to invoke the other activity but I get an error. This is the code:

Preference info;

info.setOnPreferenceClickListener(new OnPreferenceClickListener()) {
    public boolean onPreferenceClick(Preference preference) {
    intent = new Intent(getBaseContext(), OtherActivity.class);
    startActivity(intent);

    }
}

How can i fix? Thanks

This is the error:

07-03 14:43:46.314: E/AndroidRuntime(9691): FATAL EXCEPTION: main 07-03 14:43:46.314: E/AndroidRuntime(9691): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.XXX}: java.lang.NullPointerException 07-03 14:43:46.314: E/AndroidRuntime(9691): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 07-03 14:43:46.314: E/AndroidRuntime(9691): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 07-03 14:43:46.314: E/AndroidRuntime(9691): at android.app.ActivityThread.access$600(ActivityThread.java:141) 07-03 14:43:46.314: E/AndroidRuntime(9691): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 07-03 14:43:46.314: E/AndroidRuntime(9691): at android.os.Handler.dispatchMessage(Handler.java:99) 07-03 14:43:46.314: E/AndroidRuntime(9691): at android.os.Looper.loop(Looper.java:137) 07-03 14:43:46.314: E/AndroidRuntime(9691): at android.app.ActivityThread.main(ActivityThread.java:5039) 07-03 14:43:46.314: E/AndroidRuntime(9691): at java.lang.reflect.Method.invokeNative(Native Method) 07-03 14:43:46.314: E/AndroidRuntime(9691): at java.lang.reflect.Method.invoke(Method.java:511) 07-03 14:43:46.314: E/AndroidRuntime(9691): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 07-03 14:43:46.314: E/AndroidRuntime(9691): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-03 14:43:46.314: E/AndroidRuntime(9691): at dalvik.system.NativeStart.main(Native Method) 07-03 14:43:46.314: E/AndroidRuntime(9691): Caused by: java.lang.NullPointerException 07-03 14:43:46.314: E/AndroidRuntime(9691): at com.XXX.onCreate(Impostazioni.java:56) 07-03 14:43:46.314: E/AndroidRuntime(9691): at android.app.Activity.performCreate(Activity.java:5104) 07-03 14:43:46.314: E/AndroidRuntime(9691): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 07-03 14:43:46.314: E/AndroidRuntime(9691): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 07-03 14:43:46.314: E/AndroidRuntime(9691): ... 11 more

1 Answers1

1

you problem is here getBaseContext() replace it with :

     intent = new Intent(YourMainActivity.this, YourTargetedActivity.class);


     info.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
     public boolean onPreferenceClick(Preference preference) 
     { 
     intent = new Intent(Impostazioni.this, Informazioni.class));
     startActivity(intent);
     return true;
     } 
     });
mmoghrabi
  • 1,233
  • 1
  • 14
  • 23
  • well, but now I get the error at the first line // Error informazioni.setOnPreferenceClickListener(new OnPreferenceClickListener()) { // Error public boolean onPreferenceClick(Preference preference) { intent = new Intent(Impostazioni.this, Informazioni.class); startActivity(intent); } } – John Smoother Jul 03 '13 at 08:43