tags:

views:

44

answers:

3

Hello!

I'm Trying to start the saveBookmark activity and I get Force Close for some unknown reason. It says it's NULL but it's supposed to be null as "title" and "url" are optional (also, I've tried replacing null with string data and I get the same Force Close).

Here is the button listener:

Button addBookBtn = (Button) findViewById(R.id.add_bookmark);
        addBookBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                saveBookmark(context, null, null);
            }
        });

Here is the saveBookmark method:

public static final void saveBookmark(Context c, String title, String url) {
        Intent i = new Intent(Intent.ACTION_INSERT, android.provider.Browser.BOOKMARKS_URI);
        i.putExtra("title", title);
        i.putExtra("url", url);
        c.startActivity(i);
    }

This is the FATAL Error I get when I click the Add button:

E/AndroidRuntime(17252): FATAL EXCEPTION: main
E/AndroidRuntime(17252): java.lang.NullPointerException
E/AndroidRuntime(17252):        at com.droidil.droidmarks.Dmarks.saveBookmark(Dm
arks.java:81)
E/AndroidRuntime(17252):        at com.droidil.droidmarks.Dmarks$2.onClick(Dmark
s.java:71)
E/AndroidRuntime(17252):        at android.view.View.performClick(View.java:2408
)
E/AndroidRuntime(17252):        at android.view.View$PerformClick.run(View.java:
8818)
E/AndroidRuntime(17252):        at android.os.Handler.handleCallback(Handler.jav
a:587)
E/AndroidRuntime(17252):        at android.os.Handler.dispatchMessage(Handler.ja
va:92)
E/AndroidRuntime(17252):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(17252):        at android.app.ActivityThread.main(ActivityThrea
d.java:4627)
E/AndroidRuntime(17252):        at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime(17252):        at java.lang.reflect.Method.invoke(Method.java:5
21)
E/AndroidRuntime(17252):        at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(17252):        at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:626)
E/AndroidRuntime(17252):        at dalvik.system.NativeStart.main(Native Method)

Appreciate the help!

+1  A: 

You can do something called overloading - this works best if you have optional parameters.

public static final void saveBookmark(Context c) {
  Intent i = new Intent(Intent.ACTION_INSERT, android.provider.Browser.BOOKMARKS_URI);
  c.startActivity(i);
}

public static final void saveBookmark(Context c, String title, String url) {
  Intent i = new Intent(Intent.ACTION_INSERT, android.provider.Browser.BOOKMARKS_URI);
  i.putExtra("title", title);
  i.putExtra("url", url);
  c.startActivity(i);
}

We define the method 2 times here, with different arrangements of parameters - Java is smart enough to know which one to use depending on which parameters you give it. This is just an example of how you can use overloading to accomplish what you want.

xil3
I Still get the same FATAL ERROR... :\Maybe context is empty somehow? Tried getApplicationContext() but same... force close.
liorry
By the way, tried saveBookmark(context); to force it to choose the first method you wrote and it still force closed :\Maybe Manifest issues? Intent-filters?
liorry
Is it always the same error? Or is it different now?
xil3
Try clean + build - it may have not recompiled properly.
xil3
Ok, I may need to see all of the code - is saveBookmark not within the Activity class?
xil3
It's inside Main Class which extends ListActivity
liorry
Then, for the context you should pass WhateverTheClassNameIs.this - let me know if that works. (ie. saveBookmark(WhateverTheClassNameIs.this);)
xil3
GENIUS! :) works perfectly! Thank you for your help!
liorry
A: 

Did you put an <action android:name="android.intent.action.ACTION_INSERT" /> entry for the activity in the manifest? Else it's the context that's null but if you do extend ListActivity there's no reason for that.

gatnowurry
Yes I did put this intent. and the list is working great, listing all the bookmarks... with favicons....
liorry
A: 

From the Intent API entry:

public Intent putExtra(String name, Bundle value)

Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

You aren't including the package prefix in your putExtra() calls, e.g.:

i.putExtra("yourpackage.title", title);

I think we'll need some more info to fully diagnose your exception, but this could be an issue.

McStretch
Thanks for replying. xil3 solved it for me, I had to put saveBookmark(WhateverTheClassNameIs.this);) instead of passing the context.
liorry