使用片段内的可展开文本,切换箭头旋转不起作用



我正在一个片段类中执行代码。我想放一个带有文本视图的卡片视图,当我单击箭头时,布局将展开。但是,我现在在切换箭头部分遇到了一个错误。

希望有人能帮助我。谢谢!

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@android:color/white"
android:fitsSystemWindows="true"
android:orientation="vertical">   
<androidx.core.widget.NestedScrollView
android:id="@+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:clipToPadding="false"
android:scrollbars="none"
android:scrollingCache="true">
<LinearLayout
android:paddingTop="20dp"
android:paddingBottom="56dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_medium"
android:layout_marginLeft="@dimen/spacing_middle"
android:layout_marginRight="@dimen/spacing_middle"
android:layout_marginTop="@dimen/spacing_medium"
android:visibility="visible"
app:cardCornerRadius="2dp"
app:cardElevation="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:gravity="center_vertical"
android:minHeight="?attr/actionBarSize"
android:orientation="horizontal">
<View
android:layout_width="@dimen/spacing_large"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/about_intro_header"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/grey_80" />
<ImageButton
android:id="@+id/introduction_toggle_text"
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:background="?attr/selectableItemBackgroundBorderless"
android:tint="@color/grey_80"
app:srcCompat="@drawable/ic_arrow_head_down" />
</LinearLayout>
<LinearLayout
android:id="@+id/introduction_expand_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_large"
android:text="@string/about_introduction" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>

Layout.java

package com.sample;
import android.os.Bundle;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import com.sample.utils.Tools;
import com.sample.utils.ViewAnimation;
public class Layout extends Fragment {
private View parent_view;
private ImageButton introduction_toggle_text;
private NestedScrollView nested_scroll_view;
private View introduction_expand_text;
public Layout() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.layout, container, false);
rootview.findViewById(R.id.nested_scroll_view);
rootview.findViewById(R.id.introduction_expand_text).setVisibility(View.GONE);
rootview.findViewById(R.id.introduction_toggle_text).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggleSectionText(introduction_toggle_text);
}
});

return rootview;
}
private void toggleSectionText(View view) {
boolean show = toggleArrow(view);
if (show) {
ViewAnimation.expand(introduction_expand_text, new ViewAnimation.AnimListener() {
@Override
public void onFinish() {
Tools.nestedScrollTo(nested_scroll_view, introduction_expand_text);
}
});
} else {
ViewAnimation.collapse(introduction_expand_text);
}
}
public boolean toggleArrow(View view) {
if (view.getRotation() == 0) {
view.animate().setDuration(200).rotation(180);
return true;
} else {
view.animate().setDuration(200).rotation(0);
return false;
}
}
}

这是我的日志中显示的错误

2021-03-15 14:56:42.770 8995-8995/com.sample E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nuevaecijatourism.neverending, PID: 8995
java.lang.NullPointerException: Attempt to invoke virtual method 'float android.view.View.getRotation()' on a null object reference
at com.sample.Layout.toggleArrow(About.java:59)
at com.sample.Layout.toggleSectionText(About.java:45)
at com.sample.Layout.access$100(About.java:14)
at com.sample.Layout$1.onClick(About.java:35)
at android.view.View.performClick(View.java:7265)
at android.view.View.performClickInternal(View.java:7221)
at android.view.View.access$3800(View.java:836)
at android.view.View$PerformClick.run(View.java:27927)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:227)
at android.app.ActivityThread.main(ActivityThread.java:7802)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1027)
2021-03-15 14:56:42.781 8995-8995/com.sample I/Process: Sending signal. PID: 8995 SIG: 9

在使用introduction_toggle_text:之前,将以下代码添加到public View onCreateView

尝试更改代码如下:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.layout, container, false);
rootview.findViewById(R.id.nested_scroll_view);
rootview.findViewById(R.id.introduction_expand_text).setVisibility(View.GONE);
introduction_toggle_text = rootview.findViewById(R.id.introduction_toggle_text);
introduction_toggle_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggleSectionText(introduction_toggle_text);
}
});
return rootview;
}

最新更新