java - 使用倒计时器时应用程序停止

标签 java android android-fragments crash

我正在开发一个测验应用程序,它有五个 fragment ,每个 fragment 都有一个问题。 fragment 以两种方式移动。

  1. 当用户单击某个选项时,下一个 fragment 会立即出现。
  2. 我有一个倒计时器,如果用户没有单击任何选项,则 10 秒后 fragment 将移动到新 fragment 。

我已经实现了点击功能。当用户单击选项时,应用程序会将其带到下一个 fragment ,但如果在下一个 fragment 中用户未单击任何选项,则应用程序将立即停止。 请看一下,我正在分享代码。

Question.java( fragment 1)

 package com.example.sheikhspc.testapp;


import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class Question1 extends Fragment implements View.OnClickListener {


public Question1() {
    // Required empty public constructor
}
int counter = 0;
View view;
Fragment frag = null;
TextView tv;
ImageView imageView1, imageViewR, imageView2;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_question1, container, false);
    imageView1 = (ImageView) view.findViewById(R.id.wrong1);
    imageViewR = (ImageView) view.findViewById(R.id.right);
    imageView2 = (ImageView) view.findViewById(R.id.wrong2);
    tv = (TextView) view.findViewById(R.id.timer1);

    imageView1.setOnClickListener(this);
    imageViewR.setOnClickListener(this);
    imageView2.setOnClickListener(this);
    new CountDownTimer(10000, 1000) {

        public void onTick(long millisUntilFinished) {
            tv.setText("Remaining Time: " + millisUntilFinished / 1000);
            //here you can have your logic to set text to edittext
        }

        public void onFinish() {
            frag = new Question2();
            FragmentManager fmm = getFragmentManager();
            FragmentTransaction fragmentTransactionn =  fmm.beginTransaction();
            fragmentTransactionn.addToBackStack(null);

            fragmentTransactionn.replace(R.id.container, frag);

            fragmentTransactionn.commit();

        }


    }.start();


    return view;
}


@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.wrong1:
            frag = new Question2();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction fragmentTransaction = fm.beginTransaction();

            fragmentTransaction.replace(R.id.container,frag);

            fragmentTransaction.commit();

            break;
        case R.id.right:
            frag = new Question2();
            FragmentManager fm1 = getFragmentManager();
            FragmentTransaction fragmentTransaction1 = fm1.beginTransaction();

            fragmentTransaction1.replace(R.id.container,frag);

            fragmentTransaction1.commit();





            break;
        case R.id.wrong2:
            frag = new Question2();
            FragmentManager fm2 = getFragmentManager();
            FragmentTransaction fragmentTransaction2 = fm2.beginTransaction();

            fragmentTransaction2.replace(R.id.container,frag);

            fragmentTransaction2.commit();

            break;
        default:
            new CountDownTimer(10000, 1000) {

                public void onTick(long millisUntilFinished) {
                    tv.setText("Remaining Time: " + millisUntilFinished / 1000);
                    //here you can have your logic to set text to edittext
                }

                public void onFinish() {
                    frag = new Question2();
                    FragmentManager fmm = getFragmentManager();
                    FragmentTransaction fragmentTransactionn = fmm.beginTransaction();
                    fragmentTransactionn.addToBackStack(null);

                    fragmentTransactionn.replace(R.id.container, frag);

                    fragmentTransactionn.commit();

                }


            }.start();
            break;

    }
}
}

Question2.java( fragment 2)

package com.example.sheikhspc.testapp;


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.app.Fragment;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


public class Question2 extends Fragment implements View.OnClickListener {


public Question2() {
    // Required empty public constructor
}


Fragment frag = null;
TextView tv;
View view;
ImageView imageView1, imageViewR, imageView2;

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

    tv = (TextView)view.findViewById(R.id.timer2);
    imageView1 = (ImageView) view.findViewById(R.id.wrong1);
    imageViewR = (ImageView) view.findViewById(R.id.right);
    imageView2 = (ImageView) view.findViewById(R.id.wrong2);
    imageView1.setOnClickListener(this);
    imageViewR.setOnClickListener(this);
    imageView2.setOnClickListener(this);
    new CountDownTimer(10000, 1000) {

        public void onTick(long millisUntilFinished) {
            tv.setText("seconds remaining: " + millisUntilFinished / 1000);
            //here you can have your logic to set text to edittext
        }

        public void onFinish() {
            tv.setText("done!");
            frag = new Question3();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction fragmentTransaction = fm.beginTransaction();

            fragmentTransaction.replace(R.id.container,frag);

            fragmentTransaction.commit();

        }

    }.start();


    return view;
}


@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.wrong1:
            frag = new Question3();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction fragmentTransaction = fm.beginTransaction();

            fragmentTransaction.replace(R.id.container,frag);

            fragmentTransaction.commit();

            break;
        case R.id.right:
            frag = new Question3();
            FragmentManager fm1 = getFragmentManager();
            FragmentTransaction fragmentTransaction1 = fm1.beginTransaction();

            fragmentTransaction1.replace(R.id.container,frag);

            fragmentTransaction1.commit();





            break;
        case R.id.wrong2:
            frag = new Question3();
            FragmentManager fm2 = getFragmentManager();
            FragmentTransaction fragmentTransaction2 = fm2.beginTransaction();

            fragmentTransaction2.replace(R.id.container,frag);

            fragmentTransaction2.commit();

            break;
        default:
            new CountDownTimer(10000, 1000) {

                public void onTick(long millisUntilFinished) {
                    tv.setText("seconds remaining: " + millisUntilFinished / 1000);
                    //here you can have your logic to set text to edittext
                }

                public void onFinish() {
                    tv.setText("done!");
                    frag = new Question3();
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fm.beginTransaction();

                    fragmentTransaction.replace(R.id.container,frag);

                    fragmentTransaction.commit();

                }

            }.start();

            break;

    }
}
}

错误日志

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.sheikhspc.testapp, PID: 32330
              java.lang.NullPointerException: Attempt to invoke virtual  method 'android.app.FragmentTransaction  android.app.FragmentManager.beginTransaction()' on a null object reference
                  at com.example.sheikhspc.testapp.Question1$1.onFinish(Question1.java:58)
                  at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5343)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                  at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
I/Process: Sending signal. PID: 32330 SIG: 9
Application terminated.

Second.java(显示所有 fragment 的 Activity )

package com.example.sheikhspc.testapp;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.app.Fragment;
import android.os.CountDownTimer;
import android.provider.DocumentsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Second extends AppCompatActivity {

Fragment frag;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();

    frag = new Question1();
    fragmentTransaction.replace(R.id.container,frag);

    fragmentTransaction.commit();

}
}

最佳答案

在替换点击 fragment 之前,您需要取消计时器。

当您在 onCreate 方法中创建 CountDownTimer 的新实例时,将其分配给一个变量,例如

CountDownTimer timer = new CountDownTimer(10000, 1000) {...};
timer.start();

然后在 onClick 方法中,即当用户单击任何选项时停止计时器,例如

@Override
public void onClick(View v) {
switch (v.getId()) {
    case R.id.btnNext:

        // Add this line to avoid crash
        timer.cancel();

        frag = new FragmentQuestion2();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();

        fragmentTransaction.replace(R.id.container,frag);

        fragmentTransaction.commit();

        break;
}

关于java - 使用倒计时器时应用程序停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39789996/

相关文章:

android - Flurry 会压垮我的应用程序

android - 在 android fragment 上添加动态内容

android - 使用 ViewPager2 将方向更改为横向时显示两 block fragment

java - 阿瓦杰伊 bean : Why does it insert object with same ID instead of updating the object?

java - 如何在网页中创建蜂鸣声?

java - 错误: Evaluating a math expression given in string form in Android Studio

java - 从扩展 Activity 的 MainActivity 类调用扩展 Fragment 的类 Fragment

java - 如何从 Activity 中隐藏 Fragment - Android Studio

java - JDBI:插入数据时出现Long/Interger异常

java - 在play框架scala中获取数据