作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想在用户从 ListView 中选择选项时显示 float 按钮。在选中的项目上要显示 float 按钮,否则应隐藏 float 按钮。这是我使用的代码
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Click action
ParseObject message = createMessage();
if (message == null) {
// error
AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
builder.setMessage(R.string.error_selecting_file)
.setTitle(R.string.error_selecting_file_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
send(message);
finish();
}
}
});
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (l.getCheckedItemCount() > 0) {
CoordinatorLayout.LayoutParams p = new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.WRAP_CONTENT, CoordinatorLayout.LayoutParams.WRAP_CONTENT);
p.anchorGravity = Gravity.BOTTOM | Gravity.END;
p.setAnchorId(R.id.colayout);
fab.setLayoutParams(p);
fab.setVisibility(View.VISIBLE);
}
}
这里是布局资源文件
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/colayout"
tools:context=".RecipientsActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/empty_recipients_list_message" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:visibility="gone"
android:clickable="true"
android:src="@drawable/ic_checkbox_marked_circle_outline_white_48dp" />
</android.support.design.widget.CoordinatorLayout>
这是 Logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
at com.teamtreehouse.ribbit.RecipientsActivity.onListItemClick(RecipientsActivity.java:129)
at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1214)
at
最佳答案
您需要全局实例化您的 float 操作按钮。您不是在全局实例化它
您应该在您的 onCreate 之外将 FloatingActionButton fab;
声明为全局然后
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Click action
ParseObject message = createMessage();
if (message == null) {
// error
AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
builder.setMessage(R.string.error_selecting_file)
.setTitle(R.string.error_selecting_file_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
send(message);
finish();
}
}
});
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (l.getCheckedItemCount() > 0) {
CoordinatorLayout.LayoutParams p = new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.WRAP_CONTENT, CoordinatorLayout.LayoutParams.WRAP_CONTENT);
p.anchorGravity = Gravity.BOTTOM | Gravity.END;
p.setAnchorId(R.id.colayout);
fab.setLayoutParams(p);
fab.setVisibility(View.VISIBLE);
}
}
关于android - setLayoutParams FloatingButton 上的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36111552/
我想在用户从 ListView 中选择选项时显示 float 按钮。在选中的项目上要显示 float 按钮,否则应隐藏 float 按钮。这是我使用的代码 FloatingActionButton f
我正在开发一个应用程序,我有一个带有 CollapsingToolbarLayout 的页面和一个 ImageView(用户的图片)代替 FloatingActionButton. 默认行为是在 Co
我是一名优秀的程序员,十分优秀!