向下滑动以刷新有问题

时间:2018-12-09 11:48:03

标签: android

我一直试图完成向下滑动以刷新,我想做的就是当我向下滑动时希望它刷新testviewsw1中的值,但是无论何时我尝试执行,它甚至都没有滑动的东西。但是不知道为什么它根本不起作用,我已经从另一个网站上找到了一些帮助,但是仍然出现此错误,并一直在显示,真的需要帮助,这是关于我的毕业设计的。请帮助我,非常感谢所有帮助组成所有人。

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        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:background="@drawable/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swiperefresh"
            android:layout_width="393dp"
            android:layout_height="570dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.421"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.595" />

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="212dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.196"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0"
            tools:ignore="MissingConstraints">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:ignore="MissingPrefix,ScrollViewSize">

                <TextView
                    android:id="@+id/testviewSw1"
                    android:layout_width="128dp"
                    android:layout_height="38dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginBottom="8dp"
                    android:text="燈泡1值"

                    android:textSize="24sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.358"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.577"
                    tools:ignore="HardcodedText,SpUsage" />
            </RelativeLayout>
        </ScrollView>

        <Button
            android:id="@+id/area_set"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="52dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="60dp"
            android:text="Button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.951"
            tools:ignore="HardcodedText,RtlCompat" />

        <ImageButton
            android:id="@+id/listenbutton2"
            android:layout_width="82dp"
            android:layout_height="60dp"

            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="4dp"
            android:background="@drawable/micro"
            android:cropToPadding="false"
            android:onClick="listenButtonOnClick"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.835"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.842"
            tools:ignore="ContentDescription,OnClick" />


        <TextView
            android:id="@+id/resultTextView"
            android:layout_width="337dp"
            android:layout_height="187dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="88dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/listenbutton"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.508"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.17"
            tools:ignore="UnknownIdInLayout" />


        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="36dp"
            android:layout_marginStart="68dp"
            android:layout_marginTop="8dp"
            android:text="區域設置"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"

            app:layout_constraintVertical_bias="0.03"
            tools:ignore="HardcodedText,RtlCompat,UnknownId" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="68dp"
            android:layout_height="18dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="36dp"
            android:text="語音控制"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.732"
            app:layout_constraintStart_toEndOf="@+id/textView4"
            app:layout_constraintVertical_bias="0.0"
            tools:ignore="HardcodedText,UnknownId,UnknownIdInLayout" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="379dp"
            android:layout_height="44dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="#59fffac0"
            android:text="首頁"
            android:textAlignment="center"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.454"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            tools:ignore="HardcodedText" />

        <Switch
            android:id="@+id/switch1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:switchMinWidth="56dp"
            android:text="Switch1:"
            android:textOff="OFF"
            android:textOn="ON"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.817"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.582"
            tools:ignore="HardcodedText,RtlHardcoded" />

    </android.support.constraint.ConstraintLayout>


    ----------


    ----------
     private String led;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.home_main);
            // 下拉更新↓
            final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
            final TextView textView = (TextView) findViewById(R.id.testviewSw1);

            mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
            {
                @Override
                public void onRefresh() {
                    swipeRefreshLayout.setRefreshing(true);
                    (new Handler()).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            swipeRefreshLayout.setRefreshing(false);

                            JsonObject obj = new JsonObject();
                            ObjectMapper mapper = new ObjectMapper();

下面是我的日志

12-11 13:03:42.320 5420-5420 / com.example.aaa.text E / InputEventReceiver:调度输入事件的异常。 12-11 13:03:42.320 5420-5420 / com.example.aaa.text E / MessageQueue-JNI:MessageQueue回调中的异常:handleReceiveCallback 12-11 13:03:42.320 5420-5420 / com.example.aaa.text E / MessageQueue-JNI:java.lang.NullPointerException:尝试在其上调用虚拟方法'boolean android.view.View.canScrollVertically(int)'空对象引用         在android.support.v4.widget.SwipeRefreshLayout.canChildScrollUp(SwipeRefreshLayout.java:678)         在android.support.v4.widget.SwipeRefreshLayout.onInterceptTouchEvent(SwipeRefreshLayout.java:701)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2108)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)         在com.android.internal.policy.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)         在com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)         在android.app.Activity.dispatchTouchEvent(Activity.java:2765)         在android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)         在com.android.internal.policy.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2364)         在android.view.View.dispatchPointerEvent(View.java:9514)         在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)         在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3642)         在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3695)         在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3661)         在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3787)         在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3669)         在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3844)         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3642)         在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3695)         在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3661)         在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3669)         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3642)         在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)         在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)         在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)         在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)         在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)         在android.os.MessageQueue.nativePollOnce(本地方法)         在android.os.MessageQueue.next(MessageQueue.java:323)         在android.os.Looper.loop(Looper.java:135)         在android.app.ActivityThread.main(ActivityThread.java:5417)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 12-11 13:03:42.320 5420-5420 / com.example.aaa.text D / AndroidRuntime:关闭VM 12-11 13:03:42.321 5420-5420 / com.example.aaa.text E / AndroidRuntime:FATAL EXCEPTION:main     流程:com.example.aaa.text,PID:5420     java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean android.view.View.canScrollVertically(int)'         在android.support.v4.widget.SwipeRefreshLayout.canChildScrollUp(SwipeRefreshLayout.java:678)         在android.support.v4.widget.SwipeRefreshLayout.onInterceptTouchEvent(SwipeRefreshLayout.java:701)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2108)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)         在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)         在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)         在com.android.internal.policy.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)         在com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)         在android.app.Activity.dispatchTouchEvent(Activity.java:2765)         在android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)         在com.android.internal.policy.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2364)         在android.view.View.dispatchPointerEvent(View.java:9514)         在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)         在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3642)         在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3695)         在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3661)         在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3787)         在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3669)         在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3844)         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3642)         在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3695)         在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3661)         在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3669)         在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3642)         在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)         在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)         在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)         在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)         在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)         在android.os.MessageQueue.nativePollOnce(本地方法)         在android.os.MessageQueue.next(MessageQueue.java:323)         在android.os.Looper.loop(Looper.java:135)         在android.app.ActivityThread.main(ActivityThread.java:5417)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

以下是我的解决方法

最终SwipeRefreshLayout swipeRefreshLayout =(SwipeRefreshLayout)findViewById(R.id.swiperefresh);         //抓灯泡1值得布局         最终的TextView textView =(TextView)findViewById(R.id.testviewSw1);

    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
    {
        @Override
        public void onRefresh() {
            swipeRefreshLayout.setRefreshing(true);
            ObjectMapper mapper = new ObjectMapper();
            JsonNode actualObj = null;
            try {
                actualObj = mapper.readTree(led);
            } catch (IOException e) {
                e.printStackTrace();
            }
            JsonNode ledObj = actualObj.get("led");
            int xx = ledObj.get("led17").asInt();
            System.out.println("led:" +  ledObj.get("led17"));
            if (xx == 1 ){

                tx1.setText("燈泡1:開");
            }
            else if (xx == 0) {

                tx1.setText("燈泡1:關");
            }

        }

    });

1 个答案:

答案 0 :(得分:0)

要显示滑动布局,您必须将视图作为包裹在单个父级中的滑动布局的子元素,如下所示:

<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">

    <!-- Other components and views -->

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- Child views -->
        </RelativeLayout>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

要更改刷新时的值,您必须将刷新侦听器设置如下:

SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipe);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //Do your changes here 
            }
        });

尝试一下,告诉我是否有问题。