Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 即使hasSubMenu返回true,也不会调用onPrepareSubMenu_Android - Fatal编程技术网

Android 即使hasSubMenu返回true,也不会调用onPrepareSubMenu

Android 即使hasSubMenu返回true,也不会调用onPrepareSubMenu,android,Android,下面是我的ActionProvider类 import java.util.Locale; import android.content.Context; import android.util.Log; import android.view.ActionProvider; import android.view.LayoutInflater; import android.view.SubMenu; import android.view.View; import android.widg

下面是我的ActionProvider类

import java.util.Locale;

import android.content.Context;
import android.util.Log;
import android.view.ActionProvider;
import android.view.LayoutInflater;
import android.view.SubMenu;
import android.view.View;
import android.widget.ImageButton;

public class LocaleActionProvider extends ActionProvider {
private static final String TAG = "LocaleActionProvider";

Context mCtx;

public LocaleActionProvider(Context context) {
    super(context);
    mCtx = context;
}

@Override
@Deprecated
public View onCreateActionView() {
    // Inflate the action view to be shown on the action bar.
    LayoutInflater layoutInflater = LayoutInflater.from(mCtx);
    View view = layoutInflater.inflate(R.layout.action_provider_locale,
            null);
    ImageButton button = (ImageButton) view.findViewById(R.id.localeButton);
    // Set up flag icon for specific locale
    Locale locale = Locale.getDefault();
    String locLan = locale.getLanguage();

    if (locLan.equals(Locale.CHINESE.getLanguage())) {
        button.setImageResource(R.drawable.insready);
    } else if (locLan.equals(Locale.ENGLISH.getLanguage())) {
        button.setImageResource(R.drawable.ic_action_locale_en);
    } else if (locLan.equals(Locale.FRENCH.getLanguage())) {
        button.setImageResource(R.drawable.ic_action_search);
    }
    /*
     * button.setOnClickListener(new View.OnClickListener() {
     * 
     * @Override public void onClick(View v) { // Do something... } });
     */
    return view;
}

@Override
public boolean hasSubMenu() {
    return true;
}

@Override
public void onPrepareSubMenu(SubMenu subMenu) {
    Log.i(TAG, "onPrepareSubMenu()");
    // Clear since the order of items may change.
    subMenu.clear();
    subMenu.add(0, 0, 0, "简体中文");
    subMenu.add(0, 1, 1, "English");
    subMenu.add(0, 2, 2, "French");
}

@Override
public boolean onPerformDefaultAction() {
    Log.i(this.getClass().getSimpleName(), "onPerformDefaultAction");

    return super.onPerformDefaultAction();
}
}

我尝试使用调试器或Log.I()来监视onPrepareSubMenu()函数。但是onPrepareSubMenu()从未被调用,换句话说,调试器从未在断点处停止,并且没有日志条目。

我发现在onCreateActionView()中,必须返回null才能调用onPrepareSubMenu()。然后,onCreateActionView()中的所有这些设置都需要进入MainActivity中的OnCreateOptions菜单(),我发现在onCreateActionView()中,必须返回null才能调用onPrepareSubMenu()。然后,onCreateActionView()中的所有这些设置都需要进入MainActivity中的OnCreateOptions菜单()