本文介绍了安卓:saveBookmark活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图启动saveBookmark活动,我得到强制关闭了一些未知的原因。
它说,它是NULL,但它应该是null作为标题和URL是可选的(也是,我试过用字符串数据替换为空,我也得到同样的强制关闭)。

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).

下面是按钮监听器:

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

下面是saveBookmark方式:

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)

鸭preciate的帮助!

Appreciate the help!

推荐答案

您可以做些什么叫做重载 - 这效果最好,如果你有可选参数

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);
}

我们在这里定义的方法的2倍,具有不同的参数安排 - Java是足够聪明,知道这取决于哪些参数你给它使用哪一个。这仅仅是如何使用重载来完成你想要的一个例子。

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.

这篇关于安卓:saveBookmark活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-16 20:12