【问题标题】:java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getPaddingLeft()' on a null object referencejava.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.view.ViewGroup.getPaddingLeft()”
【发布时间】:2015-11-04 20:17:11
【问题描述】:

我正在尝试使用 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>

主活动:

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

项目活动:

@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)

【问题讨论】:

    标签: java android android-intent layout


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      你应该在所有视图准备好后使用它,你可以在 super.onResume() 之后调用它

      【讨论】:

        猜你喜欢
        • 2020-09-17
        • 2015-04-20
        • 2015-08-17
        • 2019-01-21
        • 2014-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-23
        相关资源
        最近更新 更多