java.lang.NullPointerException : Attempt to invoke virtual method 'int android.view.ViewGroup.getPaddingLeft()' on a null object reference 异常

标签 java android android-intent layout

我正在尝试使用 Intent 启动 Activity,但提到的错误发生在第二个 Activity 的 setContentView() 上;

这是我的代码和布局文件。

itemactivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

</LinearLayout>

主要 Activity :

Intent i=new Intent(MainActivity.this,ItemActivity.class);
startActivity(i);

项目 Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.itemactivity); // this is where error occurs
    super.onCreate(savedInstanceState);
}

这是完整的错误日志:

java.lang.RuntimeException: Unable to start activity         ComponentInfo{bertaberim.team.beertaberim/bertaberim.team.beertaberim.ItemActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getPaddingLeft()' on a null object reference
11-04 23:01:20.621 20634-20634/? E/AndroidRuntime:     at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
11-04 23:01:20.621 20634-20634/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
11-04 23:01:20.621 20634-20634/? E/AndroidRuntime:     at android.app.ActivityThread.access$1000(ActivityThread.java:198)
11-04 23:01:20.621 20634-20634/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
11-04 23:01:20.621 20634-20634/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-04 23:01:20.621 20634-20634/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
11-04 23:01:20.621 20634-20634/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6837)
11-04 23:01:20.621 20634-20634/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-04 23:01:20.621 20634-20634/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-04 23:01:20.621 20634-20634/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
11-04 23:01:20.621 20634-20634/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

最佳答案

在设置 View 之前,您需要先调用 super.onCreate() 以对 Activity 执行任何必要的初始化。只需翻转这两行:

super.onCreate(savedInstanceState);
setContentView(R.layout.itemactivity); // this is where error occurs

有关此方法的作用的更多信息,请查看 this answer .

关于java.lang.NullPointerException : Attempt to invoke virtual method 'int android.view.ViewGroup.getPaddingLeft()' on a null object reference 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33531212/

相关文章:

android - 单击 ListView 中的特定项目时,如何将 ListView 的选中项目框的颜色设置为绿色以外的其他颜色?

android - 在 Jetpack Compose UI 中设置内容 View 的方法

android - 共享图像在 viber 和 facebook 中不起作用

android - Gallery intent.getData() 在 onActivityResult 中返回 null

android - 用户点击按钮后读取 nfc 芯片

android - 找不到处理 Intent act=android.intent.action.OPEN_DOCUMENT 的 Activity

java - java 获取 ArrayIndexOutOfBoundsException

Android Studio 无法在 Windows 7 上运行

java - 如何在点击警报后获得相同的JSP页面

java - getResourceAsStream() 找到资源,但会写入一个空文件