我必须为实习开发一个应用程序,并且正在使用AndroidStudio.
现在,我只是在尝试事情,并且正在开发Spank计数器应用程序(是的,我知道).
我知道那里已经有一个回应:
What is a NullPointerException, and how do I fix it?
和许多其他类似我的问题.
事实是我误解了错误,但是在我的代码中找不到它.@H_403_8@
(我没有放入xml文件,因为我认为这不是有用的,如果您想看到它,请打个招呼.)
对不起,如果很难阅读这篇文章.这是我第一次在论坛上提问.
在此先感谢那些勇于阅读并帮助我的人.
所以这里是错误:
--------- beginning of crash06-02 18:05:31.515 2680-2680/com.example.florian.fessee E/AndroIDRuntime: FATAL EXCEPTION: main Process: com.example.florian.fessee, PID: 2680 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.florian.fessee/com.example.florian.fessee.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String androID.content.Context.getPackagename()' on a null object reference at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2236) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at androID.app.ActivityThread.access0(ActivityThread.java:151) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:135) at androID.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String androID.content.Context.getPackagename()' on a null object reference at androID.content.Contextwrapper.getPackagename(Contextwrapper.java:132) at androID.preference.PreferenceManager.getDefaultSharedPreferencesname(PreferenceManager.java:374) at androID.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369) at com.example.florian.fessee.MainActivity.<init>(MainActivity.java:23) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.class.newInstance(Class.java:1606) at androID.app.Instrumentation.newActivity(Instrumentation.java:1066) at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2226) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)Â at androID.app.ActivityThread.access0(ActivityThread.java:151)Â at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)Â at androID.os.Handler.dispatchMessage(Handler.java:102)Â at androID.os.Looper.loop(Looper.java:135)Â at androID.app.ActivityThread.main(ActivityThread.java:5254)Â at java.lang.reflect.Method.invoke(Native Method)Â at java.lang.reflect.Method.invoke(Method.java:372)Â at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)Â at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:698)Â 06-02 18:11:28.569 3101-3101/com.example.florian.fessee D/AndroIDRuntime: Shutting down VM06-02 18:11:28.570 3101-3101/com.example.florian.fessee E/AndroIDRuntime: FATAL EXCEPTION: main Process: com.example.florian.fessee, PID: 3101 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.florian.fessee/com.example.florian.fessee.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String androID.content.Context.getPackagename()' on a null object reference at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2236) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at androID.app.ActivityThread.access0(ActivityThread.java:151) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:135) at androID.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String androID.content.Context.getPackagename()' on a null object reference at androID.content.Contextwrapper.getPackagename(Contextwrapper.java:132) at androID.preference.PreferenceManager.getDefaultSharedPreferencesname(PreferenceManager.java:374) at androID.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369) at com.example.florian.fessee.MainActivity.<init>(MainActivity.java:23) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.class.newInstance(Class.java:1606) at androID.app.Instrumentation.newActivity(Instrumentation.java:1066) at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2226) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)Â at androID.app.ActivityThread.access0(ActivityThread.java:151)Â at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)Â at androID.os.Handler.dispatchMessage(Handler.java:102)Â at androID.os.Looper.loop(Looper.java:135)Â at androID.app.ActivityThread.main(ActivityThread.java:5254)Â at java.lang.reflect.Method.invoke(Native Method)Â at java.lang.reflect.Method.invoke(Method.java:372)Â at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)Â at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:698)Â 06-02 18:16:53.409 3339-3339/com.example.florian.fessee D/AndroIDRuntime: Shutting down VM06-02 18:16:53.435 3339-3339/com.example.florian.fessee E/AndroIDRuntime: FATAL EXCEPTION: main Process: com.example.florian.fessee, PID: 3339 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.florian.fessee/com.example.florian.fessee.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String androID.content.Context.getPackagename()' on a null object reference at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2236) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at androID.app.ActivityThread.access0(ActivityThread.java:151) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:135) at androID.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String androID.content.Context.getPackagename()' on a null object reference at androID.content.Contextwrapper.getPackagename(Contextwrapper.java:132) at androID.preference.PreferenceManager.getDefaultSharedPreferencesname(PreferenceManager.java:374) at androID.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369) at com.example.florian.fessee.MainActivity.<init>(MainActivity.java:23) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.class.newInstance(Class.java:1606) at androID.app.Instrumentation.newActivity(Instrumentation.java:1066) at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2226) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)Â at androID.app.ActivityThread.access0(ActivityThread.java:151)Â at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)Â at androID.os.Handler.dispatchMessage(Handler.java:102)Â at androID.os.Looper.loop(Looper.java:135)Â at androID.app.ActivityThread.main(ActivityThread.java:5254)Â at java.lang.reflect.Method.invoke(Native Method)Â at java.lang.reflect.Method.invoke(Method.java:372)Â at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)Â at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:698)Â 06-02 18:19:42.932 3515-3515/com.example.florian.fessee D/AndroIDRuntime: Shutting down VM06-02 18:19:42.935 3515-3515/com.example.florian.fessee E/AndroIDRuntime: FATAL EXCEPTION: main Process: com.example.florian.fessee, PID: 3515 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.florian.fessee/com.example.florian.fessee.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String androID.content.Context.getPackagename()' on a null object reference at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2236) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at androID.app.ActivityThread.access0(ActivityThread.java:151) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:135) at androID.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String androID.content.Context.getPackagename()' on a null object reference at androID.content.Contextwrapper.getPackagename(Contextwrapper.java:132) at androID.preference.PreferenceManager.getDefaultSharedPreferencesname(PreferenceManager.java:374) at androID.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369) at com.example.florian.fessee.MainActivity.<init>(MainActivity.java:23) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.class.newInstance(Class.java:1606) at androID.app.Instrumentation.newActivity(Instrumentation.java:1066) at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2226) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)Â at androID.app.ActivityThread.access0(ActivityThread.java:151)Â at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)Â at androID.os.Handler.dispatchMessage(Handler.java:102)Â at androID.os.Looper.loop(Looper.java:135)Â at androID.app.ActivityThread.main(ActivityThread.java:5254)Â at java.lang.reflect.Method.invoke(Native Method)Â at java.lang.reflect.Method.invoke(Method.java:372)Â at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)Â at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:698)Â
这是我的代码:
package com.example.florian.fessee;@R_403_5565@ androID.content.SharedPreferences;@R_403_5565@ androID.preference.PreferenceManager;@R_403_5565@ androID.support.v7.app.AppCompatActivity;@R_403_5565@ androID.os.Bundle;@R_403_5565@ androID.vIEw.VIEw;@R_403_5565@ androID.Widget.button;@R_403_5565@ androID.Widget.TextVIEw;@R_403_5565@ androID.Widget.Toast;public class MainActivity extends AppCompatActivity { private int compt = 0; private button b1 = null; private button b5 = null; private button b01 = null; private button b05 = null; private button braz = null; private TextVIEw t = null; private String COMPT = "comptfess"; SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = preferences.edit(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); b1 = (button) findVIEwByID(R.ID.button1); b5 = (button) findVIEwByID(R.ID.button5); b01 = (button) findVIEwByID(R.ID.button01); b05 = (button) findVIEwByID(R.ID.button05); braz = (button) findVIEwByID(R.ID.buttonraz); t = (TextVIEw) findVIEwByID(R.ID.textVIEw); compt = preferences.getInt(COMPT, 0); b1.setonClickListener(b1Listener); b5.setonClickListener(b5Listener); b01.setonClickListener(b01Listener); b05.setonClickListener(b05Listener); braz.setonClickListener(brazListener); if (compt == 1) { t.setText("Je dois 1 fessée à SophIE."); } else if (compt!=0){ t.setText("Je dois " + compt + "fessées à SophIE."); } } private VIEw.OnClickListener b1Listener = new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { compt +=1; editor.putInt(COMPT,compt); editor.commit(); if (compt == 1) { t.setText("Je dois 1 fessée à SophIE."); } else if (compt!=0){ t.setText("Je dois " + compt + "fessées à SophIE."); } } }; private VIEw.OnClickListener b5Listener = new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { compt +=5; editor.putInt(COMPT,compt); editor.commit(); if (compt == 1) { t.setText("Je dois 1 fessée à SophIE."); } else if (compt!=0){ t.setText("Je dois " + compt + "fessées à SophIE."); } } }; private VIEw.OnClickListener b01Listener = new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if(compt!=0){ compt -=1; editor.putInt(COMPT,compt); editor.commit(); if (compt==0){ t.setText("Je ne dois plus de fessées å SophIE"); } else if (compt == 1) { t.setText("Je dois 1 fessée à SophIE."); } else{ t.setText("Je dois " + compt + "fessées à SophIE."); } } else { Toast.makeText(MainActivity.this, "Un nombre de fessées doit être positif.", Toast.LENGTH_SHORT).show(); } } }; private VIEw.OnClickListener b05Listener = new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if(compt>4){ compt -=5; editor.putInt(COMPT,compt); editor.commit(); if (compt==0){ t.setText("Je ne dois plus de fessées å SophIE."); } else if (compt == 1) { t.setText("Je dois 1 fessée à SophIE."); } else{ t.setText("Je dois " + compt + "fessées à SophIE."); } } else { Toast.makeText(MainActivity.this, "Un nombre de fessées doit être positif.", Toast.LENGTH_SHORT).show(); } } }; private VIEw.OnClickListener brazListener = new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { compt = 0; editor.putInt(COMPT,compt); editor.commit(); t.setText("Je ne dois plus de fessées à SophIE."); } };}
解决方法:
这是你的问题:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = preferences.edit();
在创建活动之前,您无法初始化SharedPreferences.像这样分开声明和实例化:
...SharedPreferences preferences;SharedPreferences.Editor editor;...@OverrIDepublic voID onCreate(Bundle savedInstanceState){ ... preferences = PreferenceManager.getDefaultSharedPreferences(this); editor = preferences.edit(); ...}
总结 以上是内存溢出为你收集整理的java-尝试获取Activity的DefaultSharedPreferences时出现NullPointerException全部内容,希望文章能够帮你解决java-尝试获取Activity的DefaultSharedPreferences时出现NullPointerException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)