实现QuickActions UI模式

时间:2012-10-06 22:45:04

标签: android quickaction

我一直在尝试在我的应用程序中实现QuickActions UI模式。我发现了几个添加QuickAction的库,但它们都有一个共同点 - QuickAction弹出窗口总是在一个活动中实现。

作为我的应用程序的一部分,我有一个可浏览的视图,里面有2个ListFragments,这使我可以在两者之间动态浏览。但是,每当我尝试实现此UI模式时,调用QuickAction.show时都会导致NullPointerExceptions。到目前为止,我已经尝试过NewQuickActions,以及Qberticus的LikeQuickActions。两者都有完全相同的问题。 我的问题是,是否存在一个基本限制,阻止PopupWindow被锚定到片段内的ListItem?或者我只是做错了什么。作为参考,这是我使用的代码。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                                  Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fridge_food_tab, container,false);
    listView = (ListView) v.findViewById(R.id.fridge_food_list);

    getActivity().getSupportLoaderManager().initLoader(FRIDGE_FOOD_LOADER, null, this);

    adapter = new FridgeCursorAdapter(getActivity().getApplicationContext(), 
                  R.layout.fridge_list_layout, null, uiBindFrom, uiBindTo, 
                  CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

    final QuickAction qa = new QuickAction(getActivity());

    ActionItem item = new ActionItem(getResources()
                          .getDrawable(android.R.drawable.ic_input_add));

    qa.addActionItem(item); //<-- Line 51

    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            qa.show(arg1);
        }
    });

listView.setAdapter(adapter);
return v;
}

这是发生的例外

10-06 18:40:53.617: E/AndroidRuntime(17832): FATAL EXCEPTION: main
10-06 18:40:53.617: E/AndroidRuntime(17832): java.lang.NullPointerException
10-06 18:40:53.617: E/AndroidRuntime(17832): at net.londatiga.android.QuickAction.addActionItem(QuickAction.java:151)
10-06 18:40:53.617: E/AndroidRuntime(17832): at app.le.sauce.fridge.FoodTabFragment.onCreateView(FoodTabFragment.java:51)
10-06 18:40:53.617: E/AndroidRuntime(17832): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:160)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.support.v4.view.ViewPager.populate(ViewPager.java:895)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1234)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.View.measure(View.java:15172)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.View.measure(View.java:15172)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.View.measure(View.java:15172)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.View.measure(View.java:15172)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2150)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.View.measure(View.java:15172)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.Choreographer.doFrame(Choreographer.java:525)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.os.Handler.handleCallback(Handler.java:615)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.os.Looper.loop(Looper.java:137)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at android.app.ActivityThread.main(ActivityThread.java:4931)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at java.lang.reflect.Method.invokeNative(Native Method)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at java.lang.reflect.Method.invoke(Method.java:511)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
10-06 18:40:53.617: E/AndroidRuntime(17832):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:4)

不幸的是,这可能不是您正在寻找的答案,因为它更多的是解释而不是解决方案。

在第51行,您拨打qa.addActionItem(item);。这导致addActionItem(ActionItem action) of QuickAction.java。所以,我做了一些寻找;您的崩溃发生在该文件的第151行,即:

public void addActionItem(ActionItem action) {
    mActionItemList.add(action);

    String title    = action.getTitle();
    Drawable icon   = action.getIcon();

    View container  = (View) inflater.inflate(R.layout.action_item, null);

    ImageView img   = (ImageView) container.findViewById(R.id.iv_icon);
    TextView text   = (TextView) container.findViewById(R.id.tv_title);

    if (icon != null) { 
        img.setImageDrawable(icon); // This line here.
    } else {
        img.setVisibility(View.GONE);
    }

    // ...

}

现在,显然,在这一行,icon不能是null。所以,它似乎是img。从那里,我们可以得出结论container不包含ID为iv_icon的元素。但是,当我查看action_item XML文件时,它确实存在。

所以,我有一些建议:

  1. 确认没有可能正在阅读的第二个action_item.xml文件(在您的图书馆,其中或任何其他文件中)。
  2. 清理QuickAction项目,让它构建,然后清理它,让它构建,然后运行它。
  3. 使用库代码。 (测试null,尝试弄清楚什么是膨胀的,什么不是。)
  4. 如果其他所有方法都失败,请与图书馆创建者联系。他的电子邮件可以在the library homepage找到。