我想創建一個計時器並在屏幕上顯示每個滴答的計時器。問題是,我得到了一個狂熱的感覺。在android中的倒計時器
這裏是我的Java代碼:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requesting to turn the title OFF
requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
timer = (TextView)findViewById(R.id.timer);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.activity_gameplay, null);
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
//Create Timer
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText(String.valueOf(millisUntilFinished/1000));
}
public void onFinish() {
}
}.start();
}
這裏是我的main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<SurfaceView
android:id="@+id/camerapreview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
這裏是我的activity_gameplay.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
<TextView
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lifefull"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Timer : "
android:textSize="20dp"
android:textColor="#FFFF00"
android:textStyle="bold"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
堆棧跟蹤:
01-14 00:57:37.192: E/AndroidRuntime(1235): FATAL EXCEPTION: main
01-14 00:57:37.192: E/AndroidRuntime(1235): java.lang.NullPointerException
01-14 00:57:37.192: E/AndroidRuntime(1235): at com.example.gems.GamePlay$1.onTick(GamePlay.java:62)
01-14 00:57:37.192: E/AndroidRuntime(1235): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
01-14 00:57:37.192: E/AndroidRuntime(1235): at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 00:57:37.192: E/AndroidRuntime(1235): at android.os.Looper.loop(Looper.java:137)
01-14 00:57:37.192: E/AndroidRuntime(1235): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-14 00:57:37.192: E/AndroidRuntime(1235): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 00:57:37.192: E/AndroidRuntime(1235): at java.lang.reflect.Method.invoke(Method.java:511)
01-14 00:57:37.192: E/AndroidRuntime(1235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-14 00:57:37.192: E/AndroidRuntime(1235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-14 00:57:37.192: E/AndroidRuntime(1235): at dalvik.system.NativeStart.main(Native Method)
有什麼想法?
哪條線引發異常? – asgoth
完整的堆棧跟蹤將會有所幫助。 –
查看我的更新後的堆棧跟蹤 – ljpv14