【问题标题】:Button in fragment not working片段中的按钮不起作用
【发布时间】:2015-12-01 07:52:41
【问题描述】:

如果我从共享首选项中删除所有内容,我可以让我的按钮工作。我怎样才能让我的按钮工作并保持我的偏好?

我不确定我发布的代码是否正确。我很少发布任何东西。如果我做错了,请告诉我我需要做些什么来修复它。

public class FairgroveDirectory extends Fragment
{
    ImageButton fairgrove_cell;

    public FairgroveDirectory() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fairgrove_directory, container, false);
        fairgrove_cell = (ImageButton) root.findViewById(R.id.fairgrove_cell);
        fairgrove_cell.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Toast.makeText(getActivity(), "You Clicked the button!", Toast.LENGTH_LONG).show();
            }
        });
        SharedPreferences fontSize =PreferenceManager.getDefaultSharedPreferences(getActivity());

        // Get the font size option.  We use "FONT_SIZE" as the key.
        // Make sure to use this key when you set the value in SharedPreferences.
        // We specify "Medium" as the default value, if it does not exist.
        String fontSizePref = fontSize.getString("FONT_SIZE", "Small");

        // Select the proper theme ID.
        // These will correspond to your theme names as defined in themes.xml.
        Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.FontSizeSmall);
        if ("Medium".equals(fontSizePref)) {
            contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.FontSizeMedium);
        }
        else if ("Large".equals(fontSizePref)) {
            contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.FontSizeLarge);
        }
        else if ("XLarge".equals(fontSizePref)) {
            contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.FontSizeXLarge);
        }
        // Set the theme for the activity.

        // clone the inflater using the ContextThemeWrapper
        LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

        // inflate the layout using the cloned inflater, not default inflater
        return localInflater.inflate(R.layout.fairgrove_directory, container, false);
    }
}

【问题讨论】:

  • 返回根目录;用你的代码替换它

标签: android button fragment


【解决方案1】:

问题是由以下原因引起的:

return localInflater.inflate(R.layout.fairgrove_directory, container, false);

行,因为从fairgrove_directory 的不同对象访问视图并从onCreateView 返回新的视图对象。

使用相同的对象root 访问视图,如ImageVIew 以从onCreateView 返回视图:

return root;

【讨论】:

  • @jamesburnison:两者之间没有关系。所以让我知道返回root 有什么问题?
  • [CODE]LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); [代码]
  • 在那一行我得到“无法访问变量 localInflater”
  • @jamesburnison:删除LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); 行,然后尝试或请显示您更新的代码
  • 现在在这一行 [CODE]Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.FontSizeSmall);[\CODE] 我得到“无法访问变量 ContextThemeWrapper”
【解决方案2】:

替换return localInflater.inflate(R.layout.fairgrove_directory, container, false); return root;

【讨论】:

    【解决方案3】:

    替换" LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); // 使用克隆的充气器充气布局,而不是默认充气器 return localInflater.inflate(R.layout.fairgrove_directory, 容器, false);"
    返回“根”;

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 2014-06-22
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多