- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个测验应用程序,它有五个 fragment ,每个 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 中扫描黑底白字条码。我使用过 zxing,它允许我只扫描白底黑字。我如何扫描和倒置条形码或使用哪个库?感谢您的帮助。 最佳答案 如果您仍在引用 journeyapps 嵌入式
所以我在 youtube 上观看了一些介绍性类(class)以学习 OpenGL 的基础知识并学习了诸如制作三角形和简单相机类等内容。我一直想尝试制作体素引擎,这显然是第一个我想做的是一个我最终可以复
这个问题在这里已经有了答案: Div with cut out edges, border and transparent background (6 个答案) 关闭 8 年前。
我有一张图片,我正在查看用 HTML 创建的小型网站的基本定制。 我知道您可以对图像进行倒 Angular 处理,如 this question here 中所示,这给出了 45 度切割。 我希望每个
我必须在 iOS 上创建一个自定义形状(倒 T)边框的 Uiview。我附上下面的截图。我进行了很多研究,找到了一种使用 here 中的 UIBezierPath 的方法. 但我不知道如何将我的 Vi
我是一名优秀的程序员,十分优秀!