2015-11-04 10 views
7

Próbuję użyć funkcji Intent do rozpoczęcia działania, ale wspomniany błąd występuje drugie działanie setContentView();Wywołanie metody java.lang.NullPointerException: próba wywołania metody wirtualnej 'int android.view.ViewGroup.getPaddingLeft()' dla odwołania do obiektu o wartości zerowej

Oto mój kod i plik układu.

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> 

główną działalność:

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); 
} 

Oto pełna dziennika błędów:

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) 

Odpowiedz

13

Najpierw zadzwoń pod numer super.onCreate(), aby wstępnie przeprowadzić niezbędne inicjalizacje działania przed ustawieniem jego widoku. Wystarczy obrócić te dwie linie:

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

Aby uzyskać więcej informacji na temat tego, co sprawka tej metody jest, sprawdź this answer.

0

Powinieneś go używać po przygotowaniu wszystkich widoków, możesz zadzwonić po super.onResume()

Powiązane problemy