我在使用CountDown时遇到错误

时间:2013-12-03 04:15:54

标签: android countdown

我是新手,我收到错误,我不知道为什么D:

当我按下按钮时,我的应用程序崩溃了。 这是我的代码:

我尝试了不同版本的倒计时,没有人跑D: 感谢...

MainActivity

    package com.example.chrontest;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    Button Start = (Button) findViewById(R.id.button1);
    Start.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            Jungle CuentaAtras = new Jungle(6000, 1000);
            CuentaAtras.start();
        }});


    }
}

Jungle.class

    package com.example.chrontest;

    import android.os.CountDownTimer;
    import android.widget.TextView;

    public class Jungle extends CountDownTimer{

    TextView Texto = (TextView) findViewById(R.id.textView1);

    public Jungle(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }

    private TextView findViewById(int textview1) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub
        Texto.setText("GAME OVER");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        Texto.setText ("" + millisUntilFinished / 1000);

    }
 }

这里是我的LogCat:

12-03 04:13:41.175: D/gralloc_goldfish(1395): Emulator without GPU emulation detected.
12-03 04:13:52.325: D/AndroidRuntime(1395): Shutting down VM
12-03 04:13:52.325: W/dalvikvm(1395): threadid=1: thread exiting with uncaught exception (group=0x41465700)
12-03 04:13:52.345: E/AndroidRuntime(1395): FATAL EXCEPTION: main
12-03 04:13:52.345: E/AndroidRuntime(1395): java.lang.NullPointerException
12-03 04:13:52.345: E/AndroidRuntime(1395):     at com.example.chrontest.Jungle.onTick(Jungle.java:28)
12-03 04:13:52.345: E/AndroidRuntime(1395):     at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
12-03 04:13:52.345: E/AndroidRuntime(1395):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 04:13:52.345: E/AndroidRuntime(1395):     at android.os.Looper.loop(Looper.java:137)
12-03 04:13:52.345: E/AndroidRuntime(1395):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-03 04:13:52.345: E/AndroidRuntime(1395):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 04:13:52.345: E/AndroidRuntime(1395):     at java.lang.reflect.Method.invoke(Method.java:525)
12-03 04:13:52.345: E/AndroidRuntime(1395):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-03 04:13:52.345: E/AndroidRuntime(1395):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-03 04:13:52.345: E/AndroidRuntime(1395):     at dalvik.system.NativeStart.main(Native Method)
12-03 04:13:53.925: I/Process(1395): Sending signal. PID: 1395 SIG: 9

更新:

大声笑我不放我的布局,而今天我看到我没有任何观点或任何东西可以看到我的倒计时,或者只是这会出现在屏幕上,大声笑我不知道:|

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginBottom="34dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:text="Button" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

您的jungle课程不是activity class

所以你无法得到

TextView Texto = (TextView) findViewById(R.id.textView1);在这里使用它

您应该做的是计算countdown并回复activity课程。你应该设置TextView

<强>更新

package com.example.chrontest;

import android.os.CountDownTimer;
import android.widget.TextView;

public class Jungle extends CountDownTimer{

Context mContext;

public Jungle(long millisInFuture, long countDownInterval, Context context) {
super(millisInFuture, countDownInterval);
mContext = context;
// TODO Auto-generated constructor stub
}

@Override
public void onFinish() {
// TODO Auto-generated method stub
mcontext.MethodToSetText("YourText");
}

@Override
public void onTick(long millisUntilFinished) {
     mcontext.MethodToSetText("" + millisUntilFinished / 1000);

}

}

并致电

Jungle CuentaAtras = new Jungle(6000, 1000, this);

答案 1 :(得分:0)