在测试我的应用时,我在Android Studio-Consol中收到以下错误:
08-21 13:56:28.059 9637-9637/net.dominik.genpush E/AndroIDRuntime﹕ FATAL EXCEPTION: mainProcess: net.dominik.genpush, PID: 9637java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{net.dominik.genpush/net.dominik.genpush.settings}: java.lang.NullPointerException at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2110) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at androID.app.ActivityThread.access0(ActivityThread.java:135) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:136) at androID.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at androID.app.Activity.findVIEwByID(Activity.java:1884) at net.dominik.genpush.settings.<init>(settings.java:23) at java.lang.class.newInstanceImpl(Native Method) at java.lang.class.newInstance(Class.java:1208) at androID.app.Instrumentation.newActivity(Instrumentation.java:1061) at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2101) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at androID.app.ActivityThread.access0(ActivityThread.java:135) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:136) at androID.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)
这是一段代码:
public CheckBox checkBox_push; public TextVIEw textVIEw_appby; public button button_Feedback; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_settings); checkBox_push = (CheckBox) findVIEwByID(R.ID.checkBox_push); textVIEw_appby = (TextVIEw) findVIEwByID(R.ID.textVIEw_appby); button_Feedback = (button) findVIEwByID(R.ID.button_Feedback); } public voID onTextClickAppBy(VIEw x) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.addcategory(Intent.category_broWSABLE); intent.setData(Uri.parse("http://www.dominiktv.net")); startActivity(intent); } //button Status Speichern @OverrIDe public voID onPause() { super.onPause(); save(checkBox_push.isChecked()); } @OverrIDe public voID onResume() { super.onResume(); checkBox_push.setChecked(load()); } private voID save(final boolean isChecked) { SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("check", isChecked); editor.commit(); } private boolean load() { SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE); return sharedPreferences.getBoolean("check", true); } public voID onCheckBoxPush(VIEw b) { if (checkBox_push.isChecked()) { //PB Pushbots.getInstance().setNotifyStatus(true); //PB E Toast.makeText(getApplicationContext(), "Push-Benachrichtigungen aktivIErt", Toast.LENGTH_SHORT).show(); } else { //PB Pushbots.getInstance().setNotifyStatus(false); //PB E Toast.makeText(getApplicationContext(), "Push-Benachrichtigungen deaktivIErt", Toast.LENGTH_SHORT).show(); } }
该应用程序也可以处理该错误,但是在一个应用程序中出现错误并不是很好.
我已经用Google搜索了很多,但是我的Java技能还远远不够,这是我第一个自行创建的应用
解决方法:
Caused by: java.lang.NullPointerException at androID.app.Activity.findVIEwByID(Activity.java:1884) at net.dominik.genpush.settings.<init>(settings.java:23)
初始化活动设置对象(可能是成员变量)时,您太早调用findVIEwByID()了.您发布的代码没有显示该代码.
您实际上只能在onCreate()或更高版本中调用活动函数.
还要将findVIEwByID()放在setContentVIEw()之后,以便它实际上可以返回非null的值.
总结以上是内存溢出为你收集整理的java-E / AndroidRuntime:致命异常:main全部内容,希望文章能够帮你解决java-E / AndroidRuntime:致命异常:main所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)