在给定时间之后,装载机不会变得不可见

时间:2018-04-22 09:11:58

标签: java android runnable loader android-handler

我有一个带按钮的活动,当我按下按钮片段时  圆形装载机将打开。但是在给出10秒的时间后,装载机仍然显示并且不会变得不可见。

这是我的代码: -

Main_Activity.java

loader_fragment=(Fragment) getFragmentManager().findFragmentById(R.id
                .loader_fragment);

  loader=loader_fragment.getActivity().findViewById(R.id.loder_login);

        handler=new Handler();
        runnable=new Runnable() {
            @Override
            public void run(
                loader.setVisibility(View.INVISIBLE);
                timer.cancel();

            }
    };
    timer=new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            handler.post(runnable);

        }
    }, 10000,1000);

Fragment.java

package com.example.user.attendance;

import android.app.Fragment;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Loader_fragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View loadder_fragment=inflater.inflate(R.layout.fragment_loader_fragment, container, false);

        return loadder_fragment;
    }


}

logcat的:

04-22 14:39:12.503 6270-6270/com.example.user.attendance E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.user.attendance, PID: 6270
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.attendance/com.example.user.attendance.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Activity android.app.Fragment.getActivity()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Activity android.app.Fragment.getActivity()' on a null object reference
        at com.example.user.attendance.MainActivity.onCreate(MainActivity.java:169)
        at android.app.Activity.performCreate(Activity.java:6679)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6119) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

默认情况下,加载程序是Visible.Can任何告诉我为什么我得到NullPointerException。

1 个答案:

答案 0 :(得分:0)

getFragmentManager()会返回null,因为您尚未将该片段添加到FragmentManager中。 首先,您应该创建Loader_fragment的实例,然后将其添加到FragmentManager,如下面的代码段所示:

getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, loader_fragment).commit();