2

I write this counter but when I lunch the app get crashed I don't now where the error some help and thank you tell where is the Error here or how can I do code in right way

public class MainActivity extends AppCompatActivity {

@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");

    }
}
}

this my log cat

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.kira.counter, PID: 4598 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kira.counter/com.example.kira.counter.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:116) at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:147) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:27) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:50) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190) at com.example.kira.counter.MainActivity.(MainActivity.java:23) at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1208) at android.app.Instrumentation.newActivity(Instrumentation.java:1061) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)  at android.app.ActivityThread.access$800(ActivityThread.java:135)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5001)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)  at dalvik.system.NativeStart.main(Native Method) 

ppreetikaa
  • 1,149
  • 2
  • 15
  • 22
  • Do you have a stack trace or log cat from the crash? – Dawood ibn Kareem May 21 '17 at 00:42
  • 1
    If you want help with debugging your code, you need to provide a clear explanation of what the problem is (i.e. the symptoms) ... including all relevant diagnostics that are available to you (e.g. the stacktrace / logcat messages). *"i have log cat"* - so share it with us! – Stephen C May 21 '17 at 00:48
  • So, if you paste the log cat into your question, someone might be able to help you. If you don't do this, you're unlikely to get any help. – Dawood ibn Kareem May 21 '17 at 00:49
  • ok i do it right now – اليوم عاجل May 21 '17 at 00:55
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Tom May 21 '17 at 01:14
  • 1
    OK, the log cat is telling you that the error is on line 23. I'm guessing that that's the line `TextView txt = (TextView)findViewById(R.id.textView);` which seems to be in the wrong place in your class. You probably want to place it inside the `onCreate` method, as Daniel Choi's answer explains. – Dawood ibn Kareem May 21 '17 at 01:31
  • So log cat shows there is a null pointer exception, but don't show WHICH variable is the one who causes it. I know it is TextView txt who is not assigned, but Android debuggin tools are like a facepalm if you are newbie... – Windgate Jun 27 '22 at 13:49

1 Answers1

3

I believe you can not initialize a TextView before runtime because the views are not set yet. Classes should be capitalized. Try this.

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);
 }
}