gpt4 book ai didi

java - 为什么 MainActivity.this 返回 null?

转载 作者:行者123 更新时间:2023-11-30 05:00:55 26 4
gpt4 key购买 nike

java.lang.NullPointerException:尝试在空对象引用上调用虚方法“java.lang.String android.content.Context.getPackageName()”

MainActivity(在 OnCreate 函数之外):

Intent intent = new Intent(MainActivity.this, DatasheetActivity.class);

list :

        <activity
android:name=".DatasheetActivity"
android:label="DatasheetActivity"
android:screenOrientation="landscape">
</activity>
<activity
android:name=".MainActivity"
android:label="MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

我也尝试了 new Intent(this, DatasheetActivity.class) 无济于事

最佳答案

所以我刚刚也声明了问题:我在 OnCreate 之外初始化了 Intent。解决方案是声明变量 Intent intent;,然后在 OnCreate 中分配一个新创建的 Intent,您可以在其他函数中使用它:intent = new Intent(this, DatasheetActivity.class)

关于java - 为什么 MainActivity.this 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58252428/

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