gpt4 book ai didi

java - Android Studio - 编译错误 : (App keeps stopping) -> RuntimeException: Unable to instantiate activity

转载 作者:行者123 更新时间:2023-12-02 00:52:58 25 4
gpt4 key购买 nike

我突然遇到一个问题,Android Studio 不想再编译我的项目了。 VD 已启动,并且没有指定其他语法错误,但应用程序不断关闭。即使我创建一个新项目并在那里更改一些小东西,也没有任何作用了。

我已经搜索了错误,但不明白到底是什么设置,因为没有人指定任何内容。我相对较新,所以如果有人能以易于理解和详细的方式向我解释这一点,我会非常高兴 - 谢谢!

E/AndroidRuntime: FATAL EXCEPTION: main
Process: kuehl.leon.tictactoe, PID: 2719
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{kuehl.leon.tictactoe/kuehl.leon.tictactoe.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2718)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at kuehl.leon.tictactoe.MainActivity.<init>(MainActivity.java:13)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1173)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

我只是想让我的东西再次正确编译!

最佳答案

此错误表明您在调用 setContentView() 之前调用了 findViewById()

你有可能在做类似的事情吗?

public class MainActivity extends AppCompatActivity {

TextView label = (TextView) findViewById(R.id.label);

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

如果您可以分享您的 Activity 代码,将有助于确定问题。

关于java - Android Studio - 编译错误 : (App keeps stopping) -> RuntimeException: Unable to instantiate activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57843112/

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