当我运行此活动时,出现错误致命错误,似乎将我的activity_shipper布局指向了我的FloatingActionButton
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shippers_man);
fabAdd=findViewById(R.id.fab_add);
fabAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCreateShipperLayout();
}
});
recyclerView = findViewById(R.id.recycler_shippers);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//Firebase
database = FirebaseDatabase.getInstance();
shippers = database.getReference(Common.SHIPPERS_TABLE);
loadAllShippers();
}
我明白了:
FATAL EXCEPTION: main
Process: com.example.fmy.frutifellesserver, PID: 19015
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fmy.frutifellesserver/com.example.fmy.frutifellesserver.ShippersMan}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class <unknown>
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.fmy.frutifellesserver.ShippersMan.onCreate(ShippersMan.java:44)
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable()' on a null object reference
at android.support.design.widget.FloatingActionButton.access$001(FloatingActionButton.java:69)
at android.support.design.widget.FloatingActionButton$ShadowDelegateImpl.setBackgroundDrawable(FloatingActionButton.java:862)
showCreateShipperLayout是一种创建AlertDialog的方法,可让我注册新的托运人
我尝试删除浮动XML按钮,但没有崩溃。 可能是错误吗?
activity_shippers_man的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/background"
tools:context=".ShippersMan">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_shippers"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:srcCompat="@drawable/ic_add_black_24dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="@android:color/white"
app:fabSize="normal"
android:backgroundTint="@color/colorAccent"/>
</RelativeLayout>
答案 0 :(得分:0)
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/ic_add_black_24dp"
android:tint="@android:color/white"
app:backgroundTint="@color/colorAccent"
app:fabSize="normal" />