【问题标题】:Xml button field value is always nullXML 按钮字段值始终为空
【发布时间】:2017-07-18 17:36:49
【问题描述】:

我搜索了很多线程,但无法理解代码中的问题。我正在尝试在片段的按钮上添加setOnClickListener 功能。它给了我

allNews.setOnClickListener(new View.OnClickListener() 处的空指针异常。

经过调试,我了解到变量allNews 为空。

下面是我的布局代码。

<LinearLayout
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <Button
            android:id = "@+id/b_all"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:gravity="center"
            android:text="All \n news"
            android:textColor="#FFF"
            android:textSize="15sp"
            android:background="@drawable/button_bg_round"
            android:padding="5dp"
            android:textAllCaps="false"


            />

这是我的java代码:

public class CategoriesFragment extends Fragment {

    public Button allNews;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

          View rootView = inflater.inflate(R.layout.activity_news_menu, container, false);
        getActivity().setTitle("Play");

        allNews = (Button) rootView.findViewById(R.id.b_all);

        return rootView;
    }

    public void onViewCreated(View view, @android.support.annotation.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        allNews.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try {

                    startActivity(new Intent(getActivity(), Newsmain.class));

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

我无法理解我错过了什么。在同一个 xml 中有一个 scrollview。我们应该单独处理这些布局/字段吗?

我们非常感谢这方面的任何帮助。

谢谢 萨提亚

已解决:我用 activity_news_category 重命名了 xml,然后变量不再为空。 Prev xml 被命名为activity_news_menu。感谢所有试图提供帮助的人。

【问题讨论】:

  • 您确定,您正在为片段膨胀正确的 xml 布局文件吗?
  • 我想要那个片段的 xml 布局。除了 View rootView = inflater.inflate(R.layout.activity_news_menu, container, false); 之外,我是否需要在连接的任何一个文件中包含其他任何内容? ?
  • 您的代码看起来不错。我只是想确保“activity_news_menu.xml”实际上有一个 id 为“@+id/b_all”的按钮
  • 是的。我刚刚发布了完整的 xml。
  • 这对我来说似乎很好。我能想到的唯一可能是膨胀错误的 xml,导致 findViewById 无法找到带有“@+id/b_all”的按钮。调试时,你发现 onCreateView 里面的 allNews null?

标签: java android xml button fragment


【解决方案1】:

public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      View rootView = inflater.inflate(R.layout.activity_news_menu, container, false);
    getActivity().setTitle("Play");

    allNews = (Button) rootView.findViewById(R.id.b_all);

allNews.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {

                startActivity(new Intent(getActivity(), Newsmain.class));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    return rootView;
}

在 OncreateView 方法中使用 set onclicklistener。希望这会有所帮助

【讨论】:

  • Tq...我也试过了,但它给出了相同的空指针异常。
【解决方案2】:
public void onViewCreated(View view, @android.support.annotation.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

allNews = (Button) view.findViewById(R.id.b_all);
        allNews.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try {

                    startActivity(new Intent(getActivity(), Newsmain.class));

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

更新: 而不是rootView 应该是view

onViewCreated() 中添加allNews = (Button) rootView.findViewById(R.id.b_all); 并将其从onCreateView() 中删除

【讨论】:

  • 谢谢...还是同样的问题。变量 allNews 为 null,因此为 nullpointerException
  • 是的...使用 rootView 会出错。 allNews = (Button)view.findViewById(R.id.b_all);
【解决方案3】:

1) 正确关闭LinearLayout

2) 去掉android:id = "@+id/b_all"之间的空格,这样你就有了:

  <LinearLayout
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <Button
                android:id="@+id/b_all"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:gravity="center"
                android:text="All \n news"
                android:textColor="#FFF"
                android:textSize="15sp"
                android:background="@drawable/button_bg_round"
                android:padding="5dp"
                android:textAllCaps="false"
                />

</LinearLayout>

然后重建项目。

【讨论】:

  • R.layout.activity_news_menu ....确保这是实际的布局文件名....这个文件名表明您正在膨胀菜单而不是正常的活动 XML
  • 非常感谢。我用activity_news_category重命名了xml,然后它通过了
  • 我很高兴它有帮助。快乐编码:)
猜你喜欢
  • 2021-07-18
  • 1970-01-01
  • 2022-11-11
  • 2015-07-24
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多