gpt4 book ai didi

java - 错误 : Unable to instantiate activity ComponentInfo. 应用程序每次在 LOgCAT 中出现此错误时都会崩溃

转载 作者:行者123 更新时间:2023-12-02 03:10:24 24 4
gpt4 key购买 nike

她是MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

FrameLayout cat1 = (FrameLayout) findViewById(R.id.cat1);

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

cat1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,Wallpapers.class);
startActivity(i);
}
});
}

@Override
public void onClick(View view) {

}

}

这是 list 代码

<?xml version="1.0" encoding="utf-8"?>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.example.akkie.customize.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name="com.example.akkie.customize.Wallpapers">

</activity>
</application>
<supports-screens android:largeScreens="true"
android:smallScreens="true"
android:normalScreens="true"
android:xlargeScreens="true"
android:compatibleWidthLimitDp="320"/>

这是日志猫

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.akkie.customize, PID: 3969
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.akkie.customize/com.example.akkie.customize.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:3133)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416)
at android.app.ActivityThread.access$1100(ActivityThread.java:230)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1822)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
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:116)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:203)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193)
at com.example.akkie.customize.MainActivity.<init>(MainActivity.java:16)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1096)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3123)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416) 
at android.app.ActivityThread.access$1100(ActivityThread.java:230) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1822) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7409) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

I/Process:发送信号。 PID:3969 SIG:9应用程序终止。

我已经在 stackoverflow 上搜索了正确的答案,但没有一个符合我的场景。尝试了所有我能做的解决方案和改变。请帮忙。

最佳答案

移动:

FrameLayout cat1 = (FrameLayout) findViewById(R.id.cat1);

onCreate() 中调用 setContentView() 后。您无法找到尚不存在的事物。

关于java - 错误 : Unable to instantiate activity ComponentInfo. 应用程序每次在 LOgCAT 中出现此错误时都会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41151207/

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