本文介绍了NullPointerException出现在android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好我是Android应用程序开发的新手,我正在构建一个教育应用程序启动器,并且在一个活动上NullPointerException使应用程序在运行时停止,我不知道如何解决这个问题。 这是我的代码:Hello I am new to android app development, and I am currently building an educational app launcher and on an activity the NullPointerException makes the app to stop while running and I don't know how to solve this.This is my code:package com.example.homestudent;import com.parse.GetCallback;import com.parse.Parse;import com.parse.ParseObject;import com.parse.ParseQuery;import com.parse.ParseException;import android.os.Bundle;import android.os.NetworkOnMainThreadException;import android.os.StrictMode;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.graphics.Typeface;public class UserLogin extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.user_login); // Use StrictMode to counter the actions of the NetworkOnMainThread Exception StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); // Connect to Parse Database Parse.initialize(this, "fjXPa3CFuEjoLwVsNOqWI4GqbTPEnWPgyicL75Kk", "2sbmKPxjFT9OFWHhb7qQjDIqnjTEAhVgWRS27r4u"); // Create Segoe Ui Light Typeface Typeface tfSegoeUILight = Typeface.createFromAsset(getAssets(), "fonts/segoeuil.ttf"); // Create Big Noodle Titling Typeface Typeface tfBigNoodleTitling = Typeface.createFromAsset(getAssets(), "fonts/big_noodle_titling.ttf"); // Create TextView class and connect to layout final TextView tvSchoolName = (TextView) findViewById(R.id.tvSchoolName); tvSchoolName.setTypeface(tfSegoeUILight); // Create EditTesk class and connect to layout final EditText etUserName = (EditText) findViewById(R.id.etUserName); etUserName.setTypeface(tfSegoeUILight); // Create Button class and connect to layout Button btnSubmit = (Button) findViewById(R.id.btnSubmit1); btnSubmit.setTypeface(tfBigNoodleTitling); // Put the value of "code" from the intent into String variable String SchoolCode1 = getIntent().getExtras().getString("code"); // Convert the string value of SchoolCode1 and put it into an int variable int SchoolCode = Integer.parseInt(SchoolCode1); // ParseQuery to get the School Name from the database ParseQuery<ParseObject> query = ParseQuery.getQuery("SchoolData"); query.whereEqualTo("Code", SchoolCode); query.getFirstInBackground(new GetCallback<ParseObject>() { public void done(ParseObject object, ParseException e) { if (object == null) { tvSchoolName.setText("No School"); } else { String SchoolName = object.getString("SchoolName"); tvSchoolName.setText(String.valueOf(SchoolName)); } } }); } @Override public void onBackPressed() { // do nothing. } logcat是The logcat is04-16 12:52:03.731: D/dalvikvm(20239): GC_CONCURRENT freed 378K, 7% free 6948K/7431K, paused 2ms+3ms04-16 12:52:04.544: D/OpenGLRenderer(20239): Flushing caches (mode 0)04-16 12:52:05.676: D/dalvikvm(20239): GC_CONCURRENT freed 384K, 7% free 7014K/7495K, paused 2ms+4ms04-16 12:52:09.731: D/OpenGLRenderer(20239): Flushing caches (mode 1)04-16 12:52:09.895: D/AndroidRuntime(20239): Shutting down VM04-16 12:52:09.895: W/dalvikvm(20239): threadid=1: thread exiting with uncaught exception (group=0x40bea1f8)04-16 12:52:09.895: E/AndroidRuntime(20239): FATAL EXCEPTION: main04-16 12:52:09.895: E/AndroidRuntime(20239): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.homestudent/com.example.homestudent.UserLogin}: java.lang.NullPointerException04-16 12:52:09.895: E/AndroidRuntime(20239): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)04-16 12:52:09.895: E/AndroidRuntime(20239): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)04-16 12:52:09.895: E/AndroidRuntime(20239): at android.app.ActivityThread.access$600(ActivityThread.java:128)04-16 12:52:09.895: E/AndroidRuntime(20239): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)04-16 12:52:09.895: E/AndroidRuntime(20239): at android.os.Handler.dispatchMessage(Handler.java:99)04-16 12:52:09.895: E/AndroidRuntime(20239): at android.os.Looper.loop(Looper.java:137)04-16 12:52:09.895: E/AndroidRuntime(20239): at android.app.ActivityThread.main(ActivityThread.java:4514)04-16 12:52:09.895: E/AndroidRuntime(20239): at java.lang.reflect.Method.invokeNative(Native Method)04-16 12:52:09.895: E/AndroidRuntime(20239): at java.lang.reflect.Method.invoke(Method.java:511)04-16 12:52:09.895: E/AndroidRuntime(20239): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)04-16 12:52:09.895: E/AndroidRuntime(20239): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)04-16 12:52:09.895: E/AndroidRuntime(20239): at dalvik.system.NativeStart.main(Native Method)04-16 12:52:09.895: E/AndroidRuntime(20239): Caused by: java.lang.NullPointerException04-16 12:52:09.895: E/AndroidRuntime(20239): at com.example.homestudent.UserLogin.onCreate(UserLogin.java:58)04-16 12:52:09.895: E/AndroidRuntime(20239): at android.app.Activity.performCreate(Activity.java:4465)04-16 12:52:09.895: E/AndroidRuntime(20239): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)04-16 12:52:09.895: E/AndroidRuntime(20239): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)04-16 12:52:09.895: E/AndroidRuntime(20239): ... 11 more04-16 12:54:11.387: I/Process(20239): Sending signal. PID: 20239 SIG: 904-16 12:54:31.098: D/libEGL(21248): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so04-16 12:54:31.106: D/libEGL(21248): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so04-16 12:54:31.106: D/libEGL(21248): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so04-16 12:54:31.309: D/OpenGLRenderer(21248): Enabling debug mode 004-16 12:54:40.739: D/dalvikvm(21248): GC_CONCURRENT freed 285K, 6% free 6849K/7239K, paused 4ms+4ms04-16 12:54:41.856: D/OpenGLRenderer(21248): Flushing caches (mode 0)04-16 12:54:55.684: D/OpenGLRenderer(21248): Flushing caches (mode 0)04-16 12:55:01.223: D/dalvikvm(21248): GC_CONCURRENT freed 364K, 7% free 6953K/7431K, paused 2ms+3ms04-16 12:55:02.208: D/OpenGLRenderer(21248): Flushing caches (mode 0)04-16 12:55:03.958: D/dalvikvm(21248): GC_CONCURRENT freed 380K, 7% free 7023K/7495K, paused 2ms+5ms04-16 12:55:08.372: D/OpenGLRenderer(21248): Flushing caches (mode 1)04-16 12:55:08.520: D/AndroidRuntime(21248): Shutting down VM04-16 12:55:08.520: W/dalvikvm(21248): threadid=1: thread exiting with uncaught exception (group=0x40bea1f8)04-16 12:55:08.520: E/AndroidRuntime(21248): FATAL EXCEPTION: main04-16 12:55:08.520: E/AndroidRuntime(21248): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.homestudent/com.example.homestudent.UserLogin}: java.lang.NullPointerException04-16 12:55:08.520: E/AndroidRuntime(21248): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)04-16 12:55:08.520: E/AndroidRuntime(21248): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)04-16 12:55:08.520: E/AndroidRuntime(21248): at android.app.ActivityThread.access$600(ActivityThread.java:128)04-16 12:55:08.520: E/AndroidRuntime(21248): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)04-16 12:55:08.520: E/AndroidRuntime(21248): at android.os.Handler.dispatchMessage(Handler.java:99)04-16 12:55:08.520: E/AndroidRuntime(21248): at android.os.Looper.loop(Looper.java:137)04-16 12:55:08.520: E/AndroidRuntime(21248): at android.app.ActivityThread.main(ActivityThread.java:4514)04-16 12:55:08.520: E/AndroidRuntime(21248): at java.lang.reflect.Method.invokeNative(Native Method)04-16 12:55:08.520: E/AndroidRuntime(21248): at java.lang.reflect.Method.invoke(Method.java:511)04-16 12:55:08.520: E/AndroidRuntime(21248): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)04-16 12:55:08.520: E/AndroidRuntime(21248): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)04-16 12:55:08.520: E/AndroidRuntime(21248): at dalvik.system.NativeStart.main(Native Method)04-16 12:55:08.520: E/AndroidRuntime(21248): Caused by: java.lang.NullPointerException04-16 12:55:08.520: E/AndroidRuntime(21248): at com.example.homestudent.UserLogin.onCreate(UserLogin.java:58)04-16 12:55:08.520: E/AndroidRuntime(21248): at android.app.Activity.performCreate(Activity.java:4465)04-16 12:55:08.520: E/AndroidRuntime(21248): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)04-16 12:55:08.520: E/AndroidRuntime(21248): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)04-16 12:55:08.520: E/AndroidRuntime(21248): ... 11 more推荐答案 这篇关于NullPointerException出现在android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 13:41