gpt4 book ai didi

android - countdowntimer-app-keeps-crashing(java.lang.NullPointerException:尝试调用虚拟方法 'android.content.Context.getApplicationInfo()')

转载 作者:行者123 更新时间:2023-12-02 07:28:16 27 4
gpt4 key购买 nike

我是android studio的初学者,我试图在android studio中构建计时器应用,但它总是崩溃,我不明白为什么没人能帮忙
这是我的代码

package com.example.timers;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

TextView hello;
CountDownTimer countDownTimer;

public MainActivity() {
hello = (TextView) findViewById(R.id.textView);
}

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

countDownTimer = new CountDownTimer(10000,1000) {
@Override
public void onTick(long l) {
hello.setText(String.valueOf(l/1000));
}

@Override
public void onFinish() {

}
}.start();
}
}
这就是它在logcat中显示的内容
2020-09-20 15:09:50.663 13306-13306/com.example.timers E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.timers, PID: 13306
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.timers/com.example.timers.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:152)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:655)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
at com.example.timers.MainActivity.<init>(MainActivity.java:15)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
请有人帮我解决这个问题,我已经尝试了很久了,但是仍然没有找到一种解决方案,该解决方案对其他人都适用。

最佳答案

去掉

public MainActivity() {
hello = (TextView) findViewById(R.id.textView);
}
并在 onCreate方法中初始化hello对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hello = (TextView) findViewById(R.id.textView);
countDownTimer = new CountDownTimer(10000,1000) {
@Override
public void onTick(long l) {
hello.setText(String.valueOf(l/1000));
}

@Override
public void onFinish() {

}
}.start();
}

关于android - countdowntimer-app-keeps-crashing(java.lang.NullPointerException:尝试调用虚拟方法 'android.content.Context.getApplicationInfo()'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63977711/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com