我在运行时之前初始化TextView,因为尚未设置视图

时间:2017-05-21 00:40:41

标签: java android

我写这个计数器,但是当我午餐时,应用程序崩溃我现在不在哪里错误一些帮助,谢谢你告诉我在哪里错误或如何以正确的方式做代码 enter code here

公共类MainActivity扩展了AppCompatActivity {enter code here

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



}
TextView txt = (TextView)findViewById(R.id.textView);
count i;

public void bu1(View view) {
    starrtime();
}

public void bu2(View view) {
    i.cancel();
}

void starrtime(){
    i = new count(100,1000);
    i.start();

}

public class count extends CountDownTimer {

    public count(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }



    @Override
    public void onTick(long millisUntilFinished) {
        txt.setText(String.valueOf(millisUntilFinished));
    }

    @Override
    public void onFinish() {

        txt.setText("Done");

    }
}

}

这是我的日志猫

E / AndroidRuntime:致命异常:主要                   处理:com.example.kira.counter,PID:4598                   java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.kira.counter / com.example.kira.counter.MainActivity}:java.lang.NullPointerException                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)                       在android.app.ActivityThread.access $ 800(ActivityThread.java:135)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:136)                       在android.app.ActivityThread.main(ActivityThread.java:5001)                       at java.lang.reflect.Method.invokeNative(Native Method)                       在java.lang.reflect.Method.invoke(Method.java:515)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)                       at dalvik.system.NativeStart.main(Native Method)                    引起:java.lang.NullPointerException                       在android.support.v7.app.AppCompatDelegateImplBase。(AppCompatDelegateImplBase.java:116)                       在android.support.v7.app.AppCompatDelegateImplV9。(AppCompatDelegateImplV9.java:147)                       在android.support.v7.app.AppCompatDelegateImplV11。(AppCompatDelegateImplV11.java:27)                       在android.support.v7.app.AppCompatDelegateImplV14。(AppCompatDelegateImplV14.java:50)                       在android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)                       在android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)                       在android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)                       在android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)                       在com.example.kira.counter.MainActivity。(MainActivity.java:23)                       at java.lang.Class.newInstanceImpl(Native Method)                       在java.lang.Class.newInstance(Class.java:1208)                       在android.app.Instrumentation.newActivity(Instrumentation.java:1061)                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)                       在android.app.ActivityThread.access $ 800(ActivityThread.java:135)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:136)                       在android.app.ActivityThread.main(ActivityThread.java:5001)                       at java.lang.reflect.Method.invokeNative(Native Method)                       在java.lang.reflect.Method.invoke(Method.java:515)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)                       在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:2)

我相信您无法在运行时之前初始化TextView,因为尚未设置视图。类应该大写。试试这个。

public class MainActivity extends AppCompatActivity{

 private TextView txt;
 private Count i;

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

     txt = (TextView)findViewById(R.id.textView);
 }
}