java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.view.ViewGroup.getPaddingLeft()”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33531212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.ViewGroup.getPaddingLeft()' on a null object reference
提问by alireza_fn
I'm trying to use an Intent to start an Activity, but the mentioned error occurs on second activity's setContentView();
我正在尝试使用 Intent 来启动一个 Activity,但是提到的错误发生在第二个 Activity 的 setContentView();
Here are my code and the layout file.
这是我的代码和布局文件。
itemactivity.xml:
项目活动.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>
MainActivity:
主要活动:
Intent i=new Intent(MainActivity.this,ItemActivity.class);
startActivity(i);
ItemActivity:
项目活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.itemactivity); // this is where error occurs
super.onCreate(savedInstanceState);
}
Here is complete error log:
这是完整的错误日志:
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.access00(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)
回答by AdamMc331
You need to call super.onCreate()
first to preform any necessary initializations to the Activity before you set it's view. Simply flip those two lines:
super.onCreate()
在设置其视图之前,您需要先调用以对 Activity 执行任何必要的初始化。只需翻转这两行:
super.onCreate(savedInstanceState);
setContentView(R.layout.itemactivity); // this is where error occurs
For more information on what this method's doing, check out this answer.
有关此方法的作用的更多信息,请查看此答案。
回答by bobby
You should use it after all views ready,you can call it after super.onResume()
你应该在所有视图准备好后使用它,你可以在 super.onResume() 之后调用它