2013-01-13 73 views
0

我想創建一個計時器並在屏幕上顯示每個滴答的計時器。問題是,我得到了一個狂熱的感覺。在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) 

有什麼想法?

+0

哪條線引發異常? – asgoth

+1

完整的堆棧跟蹤將會有所幫助。 –

+0

查看我的更新後的堆棧跟蹤 – ljpv14

回答

1

您必須從佈局通貨膨脹中訪問定時器,因爲這是實際定位的位置。

timer = (TextView)viewControl.findViewById(R.id.timer); 

所以這條線需要被你誇大了。

+0

它在我的main.xml中,我相信它包含在我的代碼中。 – ljpv14

+0

它不在上面的xml中 - 你只有一個TextView和一個LinearLayout。 –

+0

哦。忘了說我正在誇大我發佈的這個佈局。別介意那個。它不會給我一個問題。 – ljpv14