FloatingButton上的NullPointerExcep

FloatingButton上的NullPointerExcep

当用户从列表视图中选择选项时,我想显示浮动按钮。在“要显示浮动按钮”中选择的项目上,否则浮动按钮应隐藏。这是我使用的代码

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

最佳答案

你需要把你的浮动操作按钮实例化得很好。
你不是在全局实例化它
您应该在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/

10-11 16:25