Shu*_*ala 5 android button buttonclick android-layout android-coordinatorlayout
我在相对布局内放置了一个协调器布局。协调器布局由两个按钮组成。我设计了通常的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>
Run Code Online (Sandbox Code Playgroud)
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();
}
});
Run Code Online (Sandbox Code Playgroud)
问题是1)我做对了吗?2)如果是,那么为什么按钮不起作用?救命。
两个想法:
1)检查view其中的内容buttonAddB= (Button)view.findViewById(R.id.add1)以及它是否指的是您认为它指的内容;
2)你没有说当事情失败时会发生什么,所以我猜测这是一个NPE。尝试更改Toast.makeText(getContext(), "Hi", Toast.LENGTH_LONG).show()以Toast.makeText(v.getContext(), "Hi", Toast.LENGTH_LONG).show()查看是否有效。getContext()可能会返回 null。
如果这些不能解决问题,请发布更多有关故障的信息。
| 归档时间: |
|
| 查看次数: |
640 次 |
| 最近记录: |