当我在编辑器中查看我的代码时,似乎没有什么问题。但是,当我在手机上运行代码并尝试从一个活动转到另一个具有浮动操作按钮的活动时,浮动操作按钮将无法找到Id视图,尽管在java和XML中ID完全相同。
我的Java:
package com.test.app.app2_test1;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class TermsOfUse extends AppCompatActivity {
FloatingActionButton mFloatingActionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.terms_of_use);
mFloatingActionButton = (FloatingActionButton) mFloatingActionButton.findViewById(R.id.terms_back_fab);
mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
}我的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.app.app2_test1.TermsOfUse"
android:background="@color/backdrop">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="44dp"
android:orientation="vertical">
</ScrollView>
<View
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="@color/colorPrimary"
android:elevation="4dp"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/terms_back_fab"
android:layout_gravity="bottom"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_margin="16dp"
android:src="@drawable/ic_arrow_back"
app:pressedTranslationZ="12dp"/>
</android.support.design.widget.CoordinatorLayout>在我的控制台中得到的消息如下:
尝试在空对象引用上调用虚拟方法的android.view.View android.view.View
任何帮助都将不胜感激。
发布于 2017-04-02 16:38:46
你把一个观点当作自己的孩子看待!
替换:
mFloatingActionButton = (FloatingActionButton) mFloatingActionButton.findViewById(R.id.terms_back_fab);With:
mFloatingActionButton = (FloatingActionButton)findViewById(R.id.terms_back_fab);https://stackoverflow.com/questions/43170738
复制相似问题