[英]Button Click in Coordinator Layout not working
I have place a coordinater layout inside a relative layout. 我在相对布局中放置了一个协调器布局。 The coordinator layout consists of two button.
协调器布局包含两个按钮。 I designed the usual onclick function but the button does not seem to be clicked.
我设计了常用的onclick功能,但似乎没有点击按钮。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/relativeLayoutMain"
android:layout_margin="0dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.shaby.payshare.WorkPageOneFragment">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add1"
android:layout_gravity="bottom|right"
android:text="ADDBene"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add2"
android:layout_gravity="bottom|left"
android:text="AddItem"/>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
Java File part as Java文件部分为
buttonAddB= (Button)view.findViewById(R.id.add1);
buttonAddB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Hi", Toast.LENGTH_LONG).show();
}
});
The question is 1) Am I doing the correct thing? 问题是1)我做的是正确的吗? 2) If yes then why is the button not working?
2)如果是,那么为什么按钮不起作用? Help.
救命。
Two ideas: 两个想法:
1) Check to see what view
is in buttonAddB= (Button)view.findViewById(R.id.add1)
and that it refers to what you think it refers to; 1)检查
buttonAddB= (Button)view.findViewById(R.id.add1)
view
是什么,它指的是你认为它所指的是什么;
2) You don't say what happens when things fail, so I am guessing it's a NPE. 2)你没有说出事情失败会发生什么,所以我猜它是一个NPE。 Try changing
Toast.makeText(getContext(), "Hi", Toast.LENGTH_LONG).show()
to Toast.makeText(v.getContext(), "Hi", Toast.LENGTH_LONG).show()
to see if that works. 尝试将
Toast.makeText(getContext(), "Hi", Toast.LENGTH_LONG).show()
更改为Toast.makeText(v.getContext(), "Hi", Toast.LENGTH_LONG).show()
以查看是否有效。 getContext()
may be returning null. getContext()
可能返回null。
If these don't fix it, post a little more information about the failure. 如果这些不能解决问题,请发布有关失败的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.