我有一个片段,其中包含一个图像按钮,该按钮通过计时器和其他4个链接到其他片段的按钮定期更改图像。当我按下任何按钮时,它会跳转到该特定片段,但在按下后退按钮时,应用程序会崩溃。我试过了timer.cancel
,但它没有用。这是片段的代码:
public class Main2Activity extends Fragment {
View v;
Timer timer;
ImageView iv;
int i=0;
Integer[] imagechange = {
R.drawable.capture1,R.drawable.capture2,R.drawable.capture,R.drawable.capture
};
String[] linkstop = new String[]{"http://www.webmd.com/","http://www.drugs.com/","http://www.cancer.gov/","http://www.nlm.nih.gov/medlineplus/"};
ListView lv;
TimerTask updateTask = new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() { // TODO Auto-generated method stub
iv.setImageResource(imagechange[i]);
i++;
if (i > 3) {
i = 0;
}
}
});
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
v=inflater.inflate(R.layout.activity_main2,container,false);
//lv=(ListView)v.findViewById(R.id.list);
iv=(ImageView)v.findViewById(R.id.imge);
timer = new Timer();
timer.scheduleAtFixedRate(updateTask,0,2000);
Button btn=(Button)v.findViewById(R.id.a);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// timer.cancel();
// timer=null;
HomeFragment q=new HomeFragment();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.container_body,q);
ft.addToBackStack(null);
ft.commit();
}
});
Button btn1=(Button)v.findViewById(R.id.b);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// timer.cancel();
// timer=null;
QueriesFragment q=new QueriesFragment();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.container_body,q);
ft.addToBackStack(null);
ft.commit();
}
});
Button btn2=(Button)v.findViewById(R.id.c);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// timer.cancel();
// timer=null;
EquipmentsFragment q=new EquipmentsFragment();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.container_body,q);
ft.addToBackStack(null);
ft.commit();
}
});
Button btn3=(Button)v.findViewById(R.id.d);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// timer.cancel();
//timer=null;
PostQueryFragment q=new PostQueryFragment();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.container_body,q);
ft.addToBackStack(null);
ft.commit();
}
});
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(linkstop[i]));
startActivity(myIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "No application can handle this request."
+ " Please install a webbrowser", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
return v;
}
}
以下是日志文件详细信息:
java.lang.IllegalStateException: Task already scheduled or cancelled
at java.util.Timer.sched(Timer.java:401)
at java.util.Timer.scheduleAtFixedRate(Timer.java:328)
at com.example.aakar.agstart.Main2Activity.onCreateView(Main2Activity.java:82)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:960)
at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1553)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:506)
at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:178)
at android.app.Activity.onKeyUp(Activity.java:2725)
at android.view.KeyEvent.dispatch(KeyEvent.java:2712)
at android.app.Activity.dispatchKeyEvent(Activity.java:3029)
at android.support.v7.internal.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:49)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:258)
at android.support.v7.internal.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:49)
at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:320)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4331)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4032)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4193)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2400)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1964)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1955)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2377)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
答案 0 :(得分:7)
您无法重复已为计时器安排的时间任务。像这样更改你的代码,
timer = new Timer();
timer.scheduleAtFixedRate(createTimerTask(), 0, 100);
并创建这样的函数,
private TimerTask createTimerTask() {
return new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
iv.setImageResource(imagechange[i]);
i++;
if (i > 3) {
i = 0;
}
}
});
}
};
}