Use ShowCaseView with ActionBarCompat / ActionBarS

2019-05-17 12:11发布

问题:

I am using ShowcaseView library from here in my android application. I want use ShowcaseView with Actionbar Compat. But i am not getting how to use it? I have tried following code but it throws NullPointerexception. Please help me solve this issue.

Thank you.

Code :

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

Error log:

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)

回答1:

I solved that issue.Replace code of OnCreateOptionsMenu() with following code.

Code:

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


回答2:

See this blog post. I am using the support library and it works fine.

The following code will create three views that show back to back (overflow menu, a view in my page, and an action bar item).

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