gpt4 book ai didi

android - setLayoutParams FloatingButton 上的 NullPointerException

转载 作者:太空狗 更新时间:2023-10-29 16:32:12 27 4
gpt4 key购买 nike

我想在用户从 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com