我的工作在Android测验。我想补充计时器上的ImageView在问题XML
,我要叫 CountDownTimer
从播放按钮欢迎XML
。我刚才设置的的onClick
在问题XML
中加入这样的行的ImageView
有关XML
<的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_above =@ + ID /问题
机器人:layout_centerHorizontal =真
机器人:背景=@可绘制/ timer_bttn
机器人:的onClick =的onClick/>
欢迎XML
<按钮
机器人:文本=播放
机器人:ID =@ + ID / playBtn
机器人:layout_width =80dip
机器人:layout_alignParentRight =真
机器人:layout_height =WRAP_CONTENT
机器人:paddingTop =5dip
机器人:paddingBottom会=5dip
机器人:文字颜色=#FFFFFF
机器人:背景=@可绘制/ start_button/>
然后,我创建TimerActivity
公共类TimerActivity扩展活动实现OnClickListener {
私人按钮startB;
公共TextView的文字;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.question);
startB =(按钮)this.findViewById(R.id.playBtn);
startB.setOnClickListener((OnClickListener)本);
文=(TextView的)findViewById(R.id.textView1);
}
@覆盖
公共无效的onClick(视图v){
新CountDownTimer(30000,1000){//此处调整毫秒秒
公共无效onTick(长millisUntilFinished){
text.setText(+的String.Format(%D分,%D秒,
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
公共无效onFinish(){
text.setText(完成了!);
}
}。开始();
}
}
在此code我刚才写$ C $定时器C到的图像调用和执行code在TimerActivity。但我的计时器不上手,当我点击播放button.Where我的播放按钮是工作在不同Activity.so任何一个可以帮助我如何呼叫计时器在不同的XML。
我的logcat: -
ENTRY org.eclipse.ui 4 0 2013年9月2日17:19:38.741
!消息未处理的事件循环异常
!STACK 0
java.lang.IllegalArgumentException:如果小工具有错误的父
在org.eclipse.swt.SWT.error(SWT.java:4342)
在org.eclipse.swt.SWT.error(SWT.java:4276)
在org.eclipse.swt.SWT.error(SWT.java:4247)
在org.eclipse.swt.widgets.Widget.error(Widget.java:468)
在org.eclipse.swt.widgets.Control.setMenu(Control.java:3462)
在com.android.ddmuilib.logcat.LogCatPanel $ 20.handleEvent(LogCatPanel.java:1042)
在org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
在org.eclipse.swt.widgets.Widget.showMenu(Widget.java:1472)
在org.eclipse.swt.widgets.Widget.wmContextMenu(Widget.java:1574)
在org.eclipse.swt.widgets.Control.WM_CONTEXTMENU(Control.java:4673)
在org.eclipse.swt.widgets.Table.WM_CONTEXTMENU(Table.java:5972)
在org.eclipse.swt.widgets.Control.windowProc(Control.java:4532)
在org.eclipse.swt.widgets.Table.windowProc(Table.java:5913)
在org.eclipse.swt.widgets.Display.windowProc(Display.java:4989)
在org.eclipse.swt.internal.win32.OS.CallWindowProcW(本机方法)
在org.eclipse.swt.internal.win32.OS.CallWindowProc(OS.java:2440)
在org.eclipse.swt.widgets.Table.callWindowProc(Table.java:564)
在org.eclipse.swt.widgets.Table.callWindowProc(Table.java:430)
在org.eclipse.swt.widgets.Widget.wmRButtonUp(Widget.java:2395)
在org.eclipse.swt.widgets.Control.WM_RBUTTONUP(Control.java:5134)
在org.eclipse.swt.widgets.Control.windowProc(Control.java:4596)
在org.eclipse.swt.widgets.Table.windowProc(Table.java:5913)
在org.eclipse.swt.widgets.Display.windowProc(Display.java:4989)
在org.eclipse.swt.internal.win32.OS.DispatchMessageW(本机方法)
在org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2546)
在org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3756)
在org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine $ 9.run(PartRenderingEngine.java:1053)
在org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
在org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
在org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
在org.eclipse.ui.internal.Workbench $ 5.run(Workbench.java:588)
在org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
在org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
在org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
在org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
在org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
在org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
在org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
在sun.reflect.NativeMethodAccessorImpl.invoke(来源不明)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(来源不明)
在java.lang.reflect.Method.invoke(来源不明)
在org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
在org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
在org.eclipse.equinox.launcher.Main.run(Main.java:1438)
在org.eclipse.equinox.launcher.Main.main(Main.java:1414)
感谢的提前
解决方案我看不出什么问题,你的计时器实施
但是你有这个
文本=(TextView的)this.findViewById(R.id.button1);
和你有这样的
< ImageView的
机器人:ID =@ + ID /按钮1
所以它应该是
<的TextView
机器人:ID =@ + ID / textView1
和
文本=(TextView的)findViewById(R.id.textView1);
请尝试以下。在点击按钮计时开始的。
公共类MainActivity扩展活动实现OnClickListener {
私人按钮startB;
公共TextView的文字;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
startB =(按钮)this.findViewById(R.id.button1);
startB.setOnClickListener((OnClickListener)本);
文=(TextView的)findViewById(R.id.textView1);
}
@覆盖
公共无效的onClick(视图v){
新CountDownTimer(30000,1000){//此处调整毫秒秒
公共无效onTick(长millisUntilFinished){
text.setText(+的String.Format(%D分,%D秒,
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
公共无效onFinish(){
text.setText(完成了!);
}
}。开始();
}
}
检查此链接
有关低于1.5的Java版本或用于不完全支持的TimeUnit类下列等式可用于系统:
INT秒=(INT)(毫秒/ 1000)%60;
INT分钟=(int)的((毫秒/(1000 * 60))%60);
INT小时=(INT)((毫秒/(1000 * 60 * 60))%24);
编辑:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
<的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =35dp
机器人:文本=TextView的/>
<按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / textView1
机器人:layout_below =@ + ID / textView1
机器人:layout_marginTop =68dp
机器人:文本=按钮/>
< / RelativeLayout的>
I am working on Android quiz .I wanted to add timer on ImageView In Question XML
, I want to call the CountDownTimer
from Welcome XML
on Play button. I just set the onClick
in Question xml
by adding a line like this in ImageView
In Question XML
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/question"
android:layout_centerHorizontal="true"
android:background="@drawable/timer_bttn"
android:onClick="onClick"/>
In Welcome xml
<Button
android:text="Play"
android:id="@+id/playBtn"
android:layout_width="80dip"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip"
android:textColor="#ffffff"
android:background="@drawable/start_button" />
then i created TimerActivity
public class TimerActivity extends Activity implements OnClickListener {
private Button startB;
public TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question);
startB = (Button) this.findViewById(R.id.playBtn);
startB.setOnClickListener((OnClickListener) this);
text = (TextView) findViewById(R.id.textView1);
}
@Override
public void onClick(View v) {
new CountDownTimer(30000, 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
text.setText(""+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
text.setText("done!");
}
}.start();
}
}
In this code I have just written code of timer to call on Image and implement code in TimerActivity. but My Timer not get started when I click a play button.Where My Play button is working in Different Activity.so can any one help me How to call Timer In different XML.
My logCat:-
!ENTRY org.eclipse.ui 4 0 2013-09-02 17:19:38.741
!MESSAGE Unhandled event loop exception
!STACK 0
java.lang.IllegalArgumentException: Widget has the wrong parent
at org.eclipse.swt.SWT.error(SWT.java:4342)
at org.eclipse.swt.SWT.error(SWT.java:4276)
at org.eclipse.swt.SWT.error(SWT.java:4247)
at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
at org.eclipse.swt.widgets.Control.setMenu(Control.java:3462)
at com.android.ddmuilib.logcat.LogCatPanel$20.handleEvent(LogCatPanel.java:1042)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
at org.eclipse.swt.widgets.Widget.showMenu(Widget.java:1472)
at org.eclipse.swt.widgets.Widget.wmContextMenu(Widget.java:1574)
at org.eclipse.swt.widgets.Control.WM_CONTEXTMENU(Control.java:4673)
at org.eclipse.swt.widgets.Table.WM_CONTEXTMENU(Table.java:5972)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4532)
at org.eclipse.swt.widgets.Table.windowProc(Table.java:5913)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4989)
at org.eclipse.swt.internal.win32.OS.CallWindowProcW(Native Method)
at org.eclipse.swt.internal.win32.OS.CallWindowProc(OS.java:2440)
at org.eclipse.swt.widgets.Table.callWindowProc(Table.java:564)
at org.eclipse.swt.widgets.Table.callWindowProc(Table.java:430)
at org.eclipse.swt.widgets.Widget.wmRButtonUp(Widget.java:2395)
at org.eclipse.swt.widgets.Control.WM_RBUTTONUP(Control.java:5134)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4596)
at org.eclipse.swt.widgets.Table.windowProc(Table.java:5913)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4989)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2546)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3756)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
Thank's in advance
解决方案I don't see anything wrong with your timer implementation
But you have this
text = (TextView) this.findViewById(R.id.button1);
And you have this
<ImageView
android:id="@+id/button1"
So it should be
<TextView
android:id="@+id/textView1"
And
text = (TextView) findViewById(R.id.textView1);
Try the below. On click of the button timer starts.
public class MainActivity extends Activity implements OnClickListener {
private Button startB;
public TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB = (Button) this.findViewById(R.id.button1);
startB.setOnClickListener((OnClickListener) this);
text = (TextView) findViewById(R.id.textView1);
}
@Override
public void onClick(View v) {
new CountDownTimer(30000, 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
text.setText(""+String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
text.setText("done!");
}
}.start();
}
}
Check this link
For Java versions below 1.5 or for systems that do not fully support the TimeUnit class the following equations can be used:
int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours = (int) ((milliseconds / (1000*60*60)) % 24);
Edit:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="68dp"
android:text="Button" />
</RelativeLayout>
上一篇:如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和奇数、偶数、总和、如何使用
下一篇:我是否需要调用getWritableDatabase()每次我处理数据数据、getWritableDatabase