使用ShowCaseView和ActionBarCompat / ActionBarSherlock Android

时间:2013-11-29 11:41:17

标签: android actionbarsherlock android-actionbar-compat showcaseview

我在我的Android应用程序中使用来自hereShowcaseView库。我希望将ShowcaseViewActionbar Compat一起使用。但我没有得到如何使用它?我尝试过以下代码,但它会抛出NullPointerexception。请帮我解决这个问题。

谢谢。

代码:

public class Activity1 extends ActionBarActivity implements
        OnShowcaseEventListener {

    ShowcaseView sv;
    ShowcaseView.ConfigOptions co;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.screen1);
            co = new ShowcaseView.ConfigOptions();
            co.hideOnClickOutside = false;
            co.block = false;
       }catch (Exception ex) {
            ex.printStackTrace();
       }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);

        ActionViewTarget target = new ActionViewTarget(this,
                ActionViewTarget.Type.OVERFLOW);
        sv = ShowcaseView.insertShowcaseView(target, this, "Edit",
                "Click here to edit image.", co);
     }
}

错误日志:

11-29 17:12:19.792: E/AndroidRuntime(20991): FATAL EXCEPTION: main
11-29 17:12:19.792: E/AndroidRuntime(20991): java.lang.NullPointerException
11-29 17:12:19.792: E/AndroidRuntime(20991):    at com.espian.showcaseview.targets.ViewTarget.getPoint(ViewTarget.java:22)
11-29 17:12:19.792: E/AndroidRuntime(20991):    at com.espian.showcaseview.targets.ActionViewTarget.getPoint(ActionViewTarget.java:52)
11-29 17:12:19.792: E/AndroidRuntime(20991):    at com.espian.showcaseview.ShowcaseView$3.run(ShowcaseView.java:240)
11-29 17:12:19.792: E/AndroidRuntime(20991):    at android.os.Handler.handleCallback(Handler.java:725)
11-29 17:12:19.792: E/AndroidRuntime(20991):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-29 17:12:19.792: E/AndroidRuntime(20991):    at android.os.Looper.loop(Looper.java:137)
11-29 17:12:19.792: E/AndroidRuntime(20991):    at android.app.ActivityThread.main(ActivityThread.java:5283)
11-29 17:12:19.792: E/AndroidRuntime(20991):    at java.lang.reflect.Method.invokeNative(Native Method)
11-29 17:12:19.792: E/AndroidRuntime(20991):    at java.lang.reflect.Method.invoke(Method.java:511)
11-29 17:12:19.792: E/AndroidRuntime(20991):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
11-29 17:12:19.792: E/AndroidRuntime(20991):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
11-29 17:12:19.792: E/AndroidRuntime(20991):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:4)

我解决了这个问题。使用以下代码替换OnCreateOptionsMenu()的代码。

代码:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);

        sv = ShowcaseView.insertShowcaseViewWithType(ShowcaseView.ITEM_ACTION_ITEM,
            R.id.action_edit, FaceFrameActivity_dual.this,"Edit", 
           "Click here to edit image.", co);
     }

答案 1 :(得分:0)

this blog post。我正在使用支持库,它工作正常。

以下代码将创建三个背靠背显示的视图(溢出菜单,页面中的视图和操作栏项)。

    ShowcaseViews views = new ShowcaseViews(activity, new ShowcaseViews.OnShowcaseAcknowledged() {
        @Override
        public void onShowCaseAcknowledged(ShowcaseView showcaseView) {
            Toast.makeText(activity, "clicked", Toast.LENGTH_SHORT).show();
        }
    });

    views.addView(new ShowcaseViews.ItemViewProperties(ShowcaseViews.ItemViewProperties.ID_OVERFLOW, R.id.test, R.id.test, ShowcaseView.ITEM_ACTION_OVERFLOW));
    views.addView(new ShowcaseViews.ItemViewProperties(R.id.search_keyword, R.string.test, R.string.test, 0.5f));
    views.addView(new ShowcaseViews.ItemViewProperties(R.id.action_refresh, R.string.test, R.string.test, ShowcaseView.ITEM_ACTION_ITEM, 0.5f));
    views.show();