当片段被销毁时杀死后台线程

时间:2017-02-25 12:44:52

标签: android multithreading android-fragments

我有一个片段,它有一个更新进度条值的后台线程,当进程值达到某个值时更改进度条可绘制当我尝试在相同的活动中启动onther片段时,此错误

    E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.support.v4.app.FragmentActivity.getResources()' on a null object reference
    at com.example.mego.adas.ui.CarFragment$PotProgressThread$1.run(CarFragment.java:831)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:7409)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.mego.adas, PID: 25935
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.support.v4.app.FragmentActivity.getResources()' on a null object reference
    at com.example.mego.adas.ui.CarFragment$PotProgressThread$1.run(CarFragment.java:831)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:7409)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这是线程的代码

    class PotProgressThread implements Runnable {

    @Override
    public void run() {
        while (potSensorValue <= 1025 && fragmentIsRunning) {

            if (fragmentIsRunning) {

                // Update the progress bar
                potProgressBarHandler.post(new Runnable() {
                    public void run() {
                        potProgressBar.setProgress((int) potSensorValue);
                        if (potSensorValue >= 800) {
                            potProgressBar.setProgressDrawable(getActivity().
                                    getResources().getDrawable(R.drawable.progressbarred));
                        } else {
                            potProgressBar.setProgressDrawable(getActivity().
                                    getResources().getDrawable(R.drawable.progressbarblue));

                        }
                    }
                });
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                break;
            }
        }
    }
}

这里我在onCreateView()方法中开始我的线程

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View rootView = inflater.inflate(R.layout.fragment_car, container, false);
    initializeScreen(rootView);

    //check the internet connection
     connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
     networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
        buildGoogleApiClient();

        //set up the firebase
        mFirebaseDatabase = FirebaseDatabase.getInstance();

        //get the references for the childes
        //the main child for the directions services
        carDatabaseReference = mFirebaseDatabase.getReference().child(constant.FIREBASE_CAR);

        //the childes for the direction root
        connectionStateDatabaseReference = mFirebaseDatabase.getReference()
                .child(constant.FIREBASE_CAR).child(constant.FIREBASE_CONNECTION_STATE);

        accidentStateDatabaseReference = mFirebaseDatabase.getReference()
                .child(constant.FIREBASE_CAR).child(constant.FIREBASE_ACCIDENT_STATE);

        startStateStateDatabaseReference = mFirebaseDatabase.getReference()
                .child(constant.FIREBASE_CAR).child(constant.FIREBASE_START_STATE);

        lightsStateDatabaseReference = mFirebaseDatabase.getReference()
                .child(constant.FIREBASE_CAR).child(constant.FIREBASE_LIGHTS_STATE);

        lockStateDatabaseReference = mFirebaseDatabase.getReference()
                .child(constant.FIREBASE_CAR).child(constant.FIREBASE_LOCK_STATE);

        mappingServicesDatabaseReference = mFirebaseDatabase.getReference()
                .child(constant.FIREBASE_CAR).child(constant.FIREBASE_MAPPING_SERVICES);

        sensorsValuesDatabaseReference = mFirebaseDatabase.getReference()
                .child(constant.FIREBASE_CAR).child(constant.FIREBASE_SENSORES_VALUES);

        connectionStateDatabaseReference.setValue(1);


    }
    //show Snackbar if there is no internet net connection
    else {
        if (carFragment != null) {

            //   Snackbar snackbar = Snackbar.make(carFragment, R.string.no_internet_connection, Snackbar.LENGTH_LONG);
            // snackbar.show();
        }
    }

    //get the date from the connected thread
    bluetoothHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            //if message is what we want
            if (msg.what == handlerState) {
                // msg.arg1 = bytes from connect thread
                String readMessage = (String) msg.obj;

                //keep appending to string until ~ char
                recDataString.append(readMessage);

                //determine the end of the line
                endOfLineIndex = recDataString.indexOf("~");
                if (fragmentIsRunning) {
                    refreshUI();
                }

            }
        }
    };

    //setup ans start the threads
    potThread = new Thread(new PotProgressThread());
    tempThread = new Thread(new TemperatureProgressThread());
    ldrThread = new Thread(new LdrProgressThread());


    fragmentIsRunning = true;
    potThread.start();
    tempThread.start();
    ldrThread.start();


    //set the buttons listener
    lightsButton.setOnClickListener(this);
    lockButton.setOnClickListener(this);
    disconnectButton.setOnClickListener(this);
    startButton.setOnClickListener(this);

    //setup the map fragment
    MapFragment mapFragment = (MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.my_location_fragment_car);
    mapFragment.getMapAsync(this);

    // Inflate the layout for this fragment
    return rootView;
}

这是onDestoryview()方法的代码

fragmentIsRunning = false;
potThread.interrupt();

我怎么能杀了它

1 个答案:

答案 0 :(得分:0)

Dalvik在运行时保留所有线程引用,因此除非终止或完成(某些reference),否则您的线程将继续运行。因此,根据您启动线程的位置,您可能会创建多个。

在您的情况下,您的线程执行未完成,您更改了片段,因此pod 将为空。

解决方案在getActivity()之前,检查getActivity()是否为真,如果不是,则表示该片段已经分离,对isAdded()的调用将返回null。

请记住,在线程中,在您调用getActivity()之前的任何地方,最好再次检查isAdded(),因为用户可以在线程执行期间随时退出活动。

  

其他解决方案

我不确定但您可以通过全局初始化getActivity()并在线程中使用该上下文来实现。

Context

现在在你的帖子中使用这个上下文。

Context mContext;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
{
     mContext = getActivity();
     ....
}

我希望你能得到解决方案。