Ich möchte schwebende Schaltfläche zeigen, wenn der Benutzer Option aus der ListView auswählt. Bei den ausgewählten Elementen sollte die schwebende Schaltfläche angezeigt werden, andernfalls sollte die schwebende Schaltfläche ausgeblendet sein. Hier ist der Code, den ich die Arbeit mitNullPointerException on setLayoutParams FloatingButton
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);
}
}
Hier wird das Layout Ressourcendatei
<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>
Hier ist die 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
Ihr Fab-Widget ist null, fügen Sie bitte den Instanziierungscode dafür hinzu – thepoosh
Wie füge ich Instanziierungscode hinzu? – Savita
@thepoosh Ich habe den Code über – Savita