tags:

views:

127

answers:

5

Thank you all for your answers and time!

Hello,

I've been trying to call a non-static method, located in my main application Class, from the Preferences Class. Because the method I call is not static, I instantiate the main class and then try to call the specific method I want but it's force closing.

Preferences.class (from where I call the method):

Preference sorted = (Preference) findPreference("sortPref");
        sorted.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                Object d = new Dmarks();
                ((Dmarks) d).queryBookmarks();

                return true;
            }
        });

the Dmarks.class method I call:

    public void queryBookmarks() {

        Toast.makeText(context, "blah blah", Toast.LENGTH_LONG).show(); 
//context is not null and the Toast is working if I  call it from Dmarks.class

        }

The Logcat:

E/AndroidRuntime(11718): FATAL EXCEPTION: main
E/AndroidRuntime(11718): java.lang.NullPointerException
E/AndroidRuntime(11718):        at android.content.ContextWrapper.getContentReso
lver(ContextWrapper.java:90)
E/AndroidRuntime(11718):        at android.app.Activity.managedQuery(Activity.ja
va:1520)
E/AndroidRuntime(11718):        at com.droidil.droidmarks.Dmarks.queryBookmarks(
Dmarks.java:101)
E/AndroidRuntime(11718):        at com.droidil.droidmarks.Preferences$2.onPrefer
enceChange(Preferences.java:47)
E/AndroidRuntime(11718):        at android.preference.Preference.callChangeListe
ner(Preference.java:756)
E/AndroidRuntime(11718):        at android.preference.ListPreference.onDialogClo
sed(ListPreference.java:219)
E/AndroidRuntime(11718):        at android.preference.DialogPreference.onDismiss
(DialogPreference.java:384)
E/AndroidRuntime(11718):        at android.app.Dialog$ListenersHandler.handleMes
sage(Dialog.java:1047)
E/AndroidRuntime(11718):        at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime(11718):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(11718):        at android.app.ActivityThread.main(ActivityThrea
d.java:4627)
E/AndroidRuntime(11718):        at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime(11718):        at java.lang.reflect.Method.invoke(Method.java:5
21)
E/AndroidRuntime(11718):        at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(11718):        at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:626)
E/AndroidRuntime(11718):        at dalvik.system.NativeStart.main(Native Method)

D/dalvikvm(11718): GC_FOR_MALLOC freed 4248 objects / 282248 bytes in 40ms
W/ActivityManager(  244):   Force finishing activity com.droidil.droidmarks/.Pre
ferences

Appreciate any help! :)

EDIT: I can't make queryBookmarks() a static function because it uses Android function managedQuery which is not a static function.

A: 

Where does context come from? It sounds like the exception is that context is null. Do you need to inject the context into your Dmarks instance yourself? Somehow, this value needs to be set.

Also, what is the point of code like this

Object d = new Dmarks();
((Dmarks) d).queryBookmarks();

instead of just writing

Dmarks d = new Dmarks();
d.queryBookmarks();

?

matt b
as I mentioned in the comment, context is not null and the queryBookmarks() method works perfectly when I call it from within Dmarks.class.regarding the code writing, you're right... no point :) just pure Eclipse auto-completion.
liorry
by the way, this is not the real queryBookmarks() method. It's much longer... but as I said, it's been tested and worked perfect so it's no what causing the force close. (also tested getBaseContext() instead of context... works as well)
liorry
Well there's a difference between how queryBookmarks() runs in a normal Dmarks instance and how it runs when you simply create a new instance with the no-arg constructor. Perhaps the Android runtime is injecting some of the classes dependencies? What happens at `ContextWrapper.java:90`? That's the real source of the NPE.
matt b
I think you are right and it's a problem with the context but I've tried replacing context with everything - getBaseContext(), getApplicationContext() and even Dmarks.this - same force close... I have no idea what am I missing :\
liorry
A: 

Contex maybe not null but looks like it's missing something, why don't you try to inject the actual instance of main class into Prefernces?

//in main    
preferncesInstance.setDmarks(this);

Then you can call queryBook on working instance...

Hurda
not sure if I understood right on where to put this but I get "Syntax error on token "this", delete this token"
liorry
+2  A: 

Make Dmarks implement Preference.OnPreferenceChangeListener instead. Then you'll have a properly initialized context.

JRL
I tried putting this code inside the Main Activity Class but It can't recognize findPreference().----Preference sorted = (Preference) findPreference("sortPref"); sorted.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { //Toast.makeText(getBaseContext(), "Bookmarks sorted", Toast.LENGTH_LONG).show(); return true; } });
liorry
Change your class declaration to `class Dmarks implements Preference.OnPreferenceChangeListener` and then add the `onPreferenceChange()` method to your class.
JRL
Dmarks -> public class Dmarks extends ListActivity.So I had to do: public class Dmarks extends ListActivity implements OnPreferenceChangeListener. no error but nothing happens. I'll look into it. Thank you!
liorry
+2  A: 

Dmarks is your primary Application class? You should not try to instantiate this in your Activity. Instead, use the getApplication function in your Activity to get the current instance.

You will need to cast it to your subclass type:

DMark dmark = (DMark)getApplication();
dmark.queryBookmarks();
Mayra
Mayra,Thanks for replying!Dmarks is my primary Application class.If I understood right, I should put this:Dmarks dmark = (Dmarks)getApplication();dmark.queryBookmarks();instead of the:Dmarks d = new Dmarks();d.queryBookmarks();I tried this and got "Cannot cast from Application to Dmarks".
liorry
By primary application class, I mean a class that extends "Application". From your error, it sounds like that is not the case.
Mayra
oh.. no. Dmarks extends the main activity class (listActivity). Thanks for trying to help! :)
liorry
Oh, ok. There is no way to call a member function from one activity in another activity. You need to rethink your class organization. What is this function trying to do? Why do you need it in both Activities? You could create a common base class for your Activities, or move it into an Application subclass.
Mayra
Basically I want to run a sorting function when user selects the orderBy (title ASC, title DESC, most visited) from the Preferences Activity. I've managed to perform action when user selects one of the above but I want to actually perform the sort before he exist the Preferences Activity back to the Main Activity (using the "Back" device button).The sorting function is located inside the Main Activity Class. Perhaps I should consider, as you suggested, to move it outside to a shared Class (sort of a Helper Class). Will I be able to call if from the Preference Class that way?
liorry
It sounds like you have a data model that you want shared between multiple activities? I would place that in an Application subclass, where it can be accessed from all Activities. The Activity class itself should only be responsible for the view. In the model-view-control pattern, your Activity is the view while the Application contains the model and is the controller.
Mayra
Many thanks for all your help! You definitely opened my mind and pointed me to the right direction in order to find the solution ;)Greatly appreciated!
liorry
A: 

i Think you must also call toast setView() and send suitable view

mhd2