【问题标题】:Why does MainActivity.this return null?为什么 MainActivity.this 返回 null?
【发布时间】:2019-10-05 21:20:42
【问题描述】:

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

MainActivity(在 OnCreate 函数之外):

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

清单:

        <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) 无济于事

【问题讨论】:

    标签: java android android-intent


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2022-12-26
      • 2016-12-28
      • 2015-11-25
      • 2015-07-04
      • 2019-09-29
      • 2015-02-25
      • 2015-10-21
      • 2021-08-12
      • 2011-03-18
      相关资源
      最近更新 更多