Java 使用浮动操作按钮时出现致命异常
“我的导航抽屉”中的一个片段包含两个RelativeLayout,它们应该充当按钮来导航到两个不同的活动。 这是该布局的代码:Java 使用浮动操作按钮时出现致命异常,java,android,android-layout,android-fragments,floating-action-button,Java,Android,Android Layout,Android Fragments,Floating Action Button,“我的导航抽屉”中的一个片段包含两个RelativeLayout,它们应该充当按钮来导航到两个不同的活动。 这是该布局的代码: <?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" and
<?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"
android:background="#B3E5FC"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/existingClientChoiceLayout"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:background="@color/colorAccent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="126dp"
android:text="Existing Client"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/newClientChoiceLayout"
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@color/light_red">
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="138dp"
android:text="New Client"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
(相关)片段代码如下所示:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.new_entry_layout, container, false);
existingClientChoiceLayout = (RelativeLayout) rootView.findViewById(R.id.existingClientChoiceLayout);
newClientChoiceLayout = (RelativeLayout) rootView.findViewById(R.id.newClientChoiceLayout);
existingClientChoiceLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, ExistingClientsViewActivity.class));
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
}
});
newClientChoiceLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, NewClientActivity.class));
}
});
return rootView;
}
现在,当我运行代码时,一切看起来都很好。当我从导航菜单中单击此片段的选项时,我会收到奇怪的错误消息:
java.lang.ClassCastException:android.support.v7.widget.AppCompatTextView无法强制转换为android.widget.RelativeLayout
但是,如果我删除了晶圆厂,一切都正常
我搜索过类似标题的相关问题,但没有一个与我的具体问题相匹配。遇到类似问题,我就是这样解决的:
如果你不需要晶圆厂按钮,删除它,但如果你需要它,声明它。然后检查fab是否不为空,如果不为空,请单击“添加侦听器”。请用文件名为每个示例代码部分命名。例如,在何处使用FAB XML并不明显。重新清理项目并重新生成此代码不会导致此错误。清理和重建,或者确保您发布了正确的错误跟踪。谢谢各位,清理和重建是我所要做的一切。问题解决了
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.new_entry_layout, container, false);
existingClientChoiceLayout = (RelativeLayout) rootView.findViewById(R.id.existingClientChoiceLayout);
newClientChoiceLayout = (RelativeLayout) rootView.findViewById(R.id.newClientChoiceLayout);
existingClientChoiceLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, ExistingClientsViewActivity.class));
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
}
});
newClientChoiceLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, NewClientActivity.class));
}
});
return rootView;
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: judeochalifu.teylur, PID: 4746
java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.RelativeLayout
at judeochalifu.teylur.fragments.NewEntryFragment.onCreateView(NewEntryFragment.java:47)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.