Android findViewById()仅在Oreo上返回null

时间:2019-01-24 13:25:44

标签: android

我正在尝试仅将2个FloatingActionButton集成到我的应用中的this code而不是3个示例。但是我得到了错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference

问题在于,此错误仅发生在Android Oreo上,并且仅在两行“ findViewById()”和之前的第三条“ findViewById()”都可以了。

这是我的代码

public class MainActivity extends AppCompatActivity {

FloatingActionButton fabFCB, fabManual;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

...................
...................

    FloatingActionButton fab = findViewById(R.id.fabBtn); // this line works fine
    fabFCB = findViewById(R.id.fabBtn1);  //this line return null and the next one
    fabManual = findViewById(R.id.fabBtn2);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
         RelativeLayout.LayoutParams.WRAP_CONTENT,
         RelativeLayout.LayoutParams.WRAP_CONTENT
    );
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.addRule(RelativeLayout.ALIGN_PARENT_END);
    params.bottomMargin = pxfromDp(20.0f);
    params.setMarginEnd(pxfromDp(20.0f));
    fab.setLayoutParams(params);
    fabFCB.setLayoutParams(params); // this line where the error occurs
    fabManual.setLayoutParams(params);

...................
...................


     }
}

我检查了ID名称并进行了多次更改,但没有运气。 我什至尝试通过LayoutInflater按ID查找视图,但也没有运气

LayoutInflater inflator = this.getLayoutInflater();
    View v = inflator.inflate(R.layout.activity_main,null);
    fabFCB = v.findViewById(R.id.fabBtn1);
    fabManual = v.findViewById(R.id.fabBtn2);

这是布局的ID部分

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabBtn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom|end"
            android:layout_marginEnd="20dp"
            android:layout_marginBottom="60dp"
            android:background="@color/indigo"
            android:backgroundTint="@color/indigo"
            android:src="@drawable/baseline_add_white_24"
            app:backgroundTint="@color/indigo"
            app:elevation="6dp"
            app:fabSize="normal"
            app:pressedTranslationZ="12dp"
            app:targetApi="o" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabBtn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom|end"
            android:layout_marginEnd="20dp"
            android:layout_marginBottom="60dp"
            android:background="@color/indigo"
            android:backgroundTint="@color/indigo"
            android:src="@drawable/baseline_add_white_24"
            app:backgroundTint="@color/indigo"
            app:elevation="6dp"
            app:fabSize="normal"
            app:pressedTranslationZ="12dp"
            app:targetApi="o" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom|end"
            android:layout_marginEnd="20dp"
            android:layout_marginBottom="60dp"
            android:background="@color/indigo"
            android:backgroundTint="@color/indigo"
            android:src="@drawable/baseline_add_white_24"
            app:backgroundTint="@color/indigo"
            app:elevation="6dp"
            app:fabSize="normal"
            app:pressedTranslationZ="12dp"
            app:targetApi="o" />

    </RelativeLayout>

我正在使用Android Studio 3.3,并且compileSdkVersion是28

1 个答案:

答案 0 :(得分:0)

谢谢你们,@ Zee和@Chandrakant Dvivedi。 V26有一个布局文件,我对此一无所知,甚至没有创建。那是问题的根源。我删除了它,一切都像魅力一样工作。

相关问题