我正在尝试创建一个计时器并在屏幕上显示每个滴答声的计时器。问题是,我得到了一个 javanullexception。
这是我的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)
有任何想法吗?