使用ShowcaseView定位MenuItems失败

时间:2015-11-03 10:05:40

标签: android android-actionbar-compat showcaseview

我正在使用https://github.com/amlcurran/ShowcaseView来显示Menuitems的ShowcaseView,如下所示:

MainActivity.java:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);



        // Set our view from the "main" layout resource
        setContentView(R.layout.activity_main);
    //getWindow().setBackgroundDrawable(null);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        //Toolbar will now take on default actionbar characteristics
        setContentView(R.layout.activity_main);

    setTitle("DDIT Results");

    try {

        new ShowcaseView.Builder(MainActivity.this)
                .withMaterialShowcase()
                .setStyle(R.style.MyTheme)
                .setTarget(new ActionItemTarget(MainActivity.this, R.id.action_notify))
                .setContentTitle("Enable Notification")
                .setContentText("Click On the Button and get notified whenever the Result-table Changes")

                .build()
        .show();
    }
    catch(Exception e)
    {
        Toast.makeText(MainActivity.this,""+e,Toast.LENGTH_LONG).show();
    }

}

导致应用崩溃的原因。

menu_main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

<item android:id="@+id/action_rateus" android:title="@string/rate_us" android:orderInCategory="100" app:showAsAction="never"/>
<item android:id="@+id/action_notify" android:title="@string/notify" android:icon="@drawable/ic_notifications_none_white_24dp"
    android:orderInCategory="100" app:showAsAction="ifRoom" />

<item android:id="@+id/action_privacy_policy" android:title="Privacy Policy" android:orderInCategory="100" app:showAsAction="never"/>
<item android:id="@+id/action_email_us" android:title="@string/action_email_us" android:orderInCategory="100" app:showAsAction="never"/>
<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />

日志:

java.lang.RuntimeException: insertShowcaseViewWithType cannot be used when the theme has no ActionBar
        at com.github.amlcurran.showcaseview.targets.ActionBarReflector.getHomeButton(ActionBarReflector.java:43)
        at com.github.amlcurran.showcaseview.targets.ActionBarReflector.getActionBarView(ActionBarReflector.java:36)
        at com.github.amlcurran.showcaseview.targets.ActionItemTarget.setUp(ActionItemTarget.java:49)
        at com.github.amlcurran.showcaseview.targets.ActionItemTarget.getPoint(ActionItemTarget.java:43)
        at com.github.amlcurran.showcaseview.ShowcaseView$1.run(ShowcaseView.java:176)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

是否可以让showcaseview定位到操作栏中的特定项目?我搜索了这个问题并且没有提出任何解决方案。希望有人能够识别出这个错误并快速回答它 - 否则我可以提供更多信息。如果需要,可以提供更多帮助。

2 个答案:

答案 0 :(得分:0)

步骤1:创建一个Activity类并使用OnClickListener实现它。

public class MainActivity extends Activity implements View.OnClickListener {

private ImageView menuDisplayIcon;
private ShowcaseView showCaseView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // For no title
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // For ull screen view
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // Setting layout
    setContentView(R.layout.activity_main);
    // Initializing UI
    initUI();
    // Showcase
    showcaseUtils();

}

/**
 * Initializing UI components wrt MainActivity
 */
private void initUI() {
    menuDisplayIcon = (ImageView) findViewById(R.id.menuDisplayIcon);
}

/**
 * Showcase view
 */
private void showcaseUtils() {
    showCaseView = new ShowcaseView.Builder(this).setTarget(new ViewTarget(menuDisplayIcon))
            .setContentTitle("Title").setContentText("Description").setOnClickListener(this).build();
    showCaseView.setButtonText("Finish");
}


@Override
public void onClick(View v) {
    if (showCaseView != null) {
        showCaseView.hide();
    }
}

第2步:布局activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<include layout="@layout/actionbar_layout" />


</LinearLayout>

第3步:布局:actionbar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="right">

<ImageView
    android:id="@+id/menuDisplayIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/settingsicon" />

</LinearLayout>

注意:在GitHub上提供Showcase SDk。

任何查询都可以随意询问。

答案 1 :(得分:0)

目前,ShowcaseView不使用操作栏支持最新版本的AppCompat。您可以在此问题中查看更多详细信息here

我目前的建议是使用带有工具栏的AppCompat活动而不是默认的ActionBar。这应该工作正常。这是as an Activity的演示,并使用Target