本文介绍了Android的显示java.lang.NullPointerException:需要的println消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的应用程序有问题,当我尝试登录。
在事实上,如果我注册(如果复选框 remerberMe
器isChecked()
在册的行动,在下连接显示在主活动,否则登录活动出现),第一,它工作正常。
但是,登录
活动不能正常工作时,它被称为寄存器操作之后的下一个连接上。
登录活动
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.login);
的setTitle(R.string.loginTitle); //导入所有的资产如按钮,文本字段
inputUsername =(EditText上)findViewById(R.id.username_value);
控件inputPassword =(EditText上)findViewById(R.id.password_value);
btnLogin =(按钮)findViewById(R.id.loginButton);
btnExit =(按钮)findViewById(R.id.exitButton);
btnLinkToRegister =(按钮)findViewById(R.id.btnLinkToRegister);
与rememberMe =(复选框)findViewById(R.id.rememberNameRegister); //登录按钮单击事件
btnLogin.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
在LoginAction();
}
}); //链接到注册屏
btnLinkToRegister.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(查看视图){
意图I =新意图(getApplicationContext()
RegisterActivity.class);
startActivity(ⅰ);
完();
}
}); btnExit.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
完(); }
});
} 公共无效的LoginAction(){
Log.i(登录,登陆行动);
progressDialog = ProgressDialog.show(LoginActivity.this,空,
getResources()的getString(R.string.loginProgressMessage),TRUE)。 //尝试{
runOnUiThread(新的Runnable(){
字符串username = inputUsername.getText()的toString()。
。字符串密码= inputPassword.getText()的toString(); @覆盖
公共无效的run(){
Log.i(登录时运行,用户名+用户名); 开关(username.length()){
情况下0:
blankUserName(); 打破;
} 开关(password.length()){
情况下0:
blankPassWord();
//使用Thread.stop();
打破;
} 尝试{
如果(username.length()大于0&放大器;&放大器; password.length()大于0){
Log.i(登录时运行,密码+密码);
//密码pass =新密码(用户名,密码);
DatabaseHelper DB =新DatabaseHelper(
getApplicationContext());
诠释计数= db.getRowCount();
Log.i(登录,getRowCompte+计数);
如果(计数== 1){
Log.i(登录,getLogin1);
//如果(db.getLogin(用户名,密码)== 1){
如果(db.Login(用户名,密码)==真){
Log.i(登录,rememberMe.isChecked());
如果(rememberMe.isChecked()==真){
statut =上;
}否则如果(rememberMe.isChecked()== FALSE){
statut =关;
}
Log.i(登录,OK器isChecked);
Log.i(登录,getRowCompteStat+ db.getRowCountStat());
如果(db.getRowCountStat()== 1){
db.UpdateStatut(statut);
Log.i(登录,getRowCompte+计数);
}
Toast.makeText(getApplicationContext(),
学生Moyenne \\ n卞福汝!
Toast.LENGTH_LONG).show();
Log.i(登录时运行,Connecté);
意图莫伊=新意图(
getApplicationContext(),
MoyenneMain.class);
//启动之前关闭所有的意见
//仪表板
moy.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(莫伊);
完();
db.close();
}否则如果(db.Login(用户名,密码)== FALSE){
//如果(db.getLogin(用户名,密码)== 0){
// Log.i(登录时运行,假密码);
Toast.makeText(LoginActivity.this,无效的用户名/密码,Toast.LENGTH_LONG).show();
意向登录=新意图(getApplicationContext(),LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(登录);
完();
db.close();
}
}否则如果(计数== 0){
Log.i(登录时运行,Enregistrez VOUS);
Toast.makeText(LoginActivity.this,
Enregistrez VOUS!,Toast.LENGTH_SHORT)
。显示();
意向登记=新意图(
getApplicationContext(),
RegisterActivity.class);
register.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(注册);
完();
db.close();
}
} }赶上(例外五){
Log.i(登录ERROR1,e.getMessage());
Toast.makeText(LoginActivity.this,e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
});
//}赶上(例外五){
// Thread.currentThread()destroy()方法。
// Log.i(登录误差2,e.getMessage());
// Toast.makeText(LoginActivity.this,e.getMessage(),
// Toast.LENGTH_LONG).show();
//} } 私人无效blankUserName(){
Toast.makeText(LoginActivity.this,联合国Entrez的NOM Utilisateur高级副总裁!,
Toast.LENGTH_SHORT).show();
意向登录=新意图(getApplicationContext(),LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(登录);
完();
} 私人无效blankPassWord(){
Toast.makeText(LoginActivity.this,联合国Entrez的德MOT高级副总裁过时!
Toast.LENGTH_SHORT).show();
意向登录=新意图(getApplicationContext(),LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(登录);
完();
}
DatabaseHelper的一部分
公众诠释getRowCountStat(){
字符串countQuery =SELECT * FROM+ TABLE_STATUT;
SQLiteDatabase分贝= this.getReadableDatabase();
光标光标= db.rawQuery(countQuery,NULL);
INT rowCount等= cursor.getCount();
db.close();
cursor.close(); //返回行计数
返回rowCount等;
}公众诠释getRowCount(){
字符串countQuery =SELECT * FROM+ TABLE_LOGIN;
SQLiteDatabase分贝= this.getReadableDatabase();
光标光标= db.rawQuery(countQuery,NULL);
INT rowCount等= cursor.getCount();
db.close();
cursor.close(); //返回行计数
返回rowCount等;
} 公共布尔登录(用户名字符串,字符串密码)抛出的SQLException
{
光标mCursor = db.rawQuery(SELECT * FROM+ TABLE_LOGIN +WHERE用户名=?AND PASSWORD =?
,新的String [] {用户名,密码});
如果(mCursor!= NULL){
如果(mCursor.getCount()大于0)
{
返回true;
}
}
返回false;
}公众诠释UpdateStatut(字符串statut){
最终静态INT idStat = 1;
SQLiteDatabase分贝= this.getWritableDatabase();
ContentValues CV =新ContentValues();
cv.put(STATUT,statut);
返回db.update(TABLE_STATUT,CV,ID_STAT +=?,
新的String [] {将String.valueOf(idStat)});
}
的logcat
1月10日至24日:48:08.819:E / AndroidRuntime(29242):致命异常:主要
1月10日至24日:48:08.819:E / AndroidRuntime(29242):显示java.lang.NullPointerException:需要的println消息
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在android.util.Log.println_native(本机方法)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在android.util.Log.i(Log.java:143)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在com.android.moyenne.activity.LoginActivity $ 4.run(LoginActivity.java:163)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在android.app.Activity.runOnUiThread(Activity.java:3707)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在com.android.moyenne.activity.LoginActivity.LoginAction(LoginActivity.java:78)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在com.android.moyenne.activity.LoginActivity $ 1.onClick(LoginActivity.java:46)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在android.view.View.performClick(View.java:2408)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在android.view.View $ PerformClick.run(View.java:8816)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在android.os.Handler.handleCallback(Handler.java:587)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在android.os.Handler.dispatchMessage(Handler.java:92)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在android.os.Looper.loop(Looper.java:123)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在android.app.ActivityThread.main(ActivityThread.java:4627)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在java.lang.reflect.Method.invokeNative(本机方法)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在java.lang.reflect.Method.invoke(Method.java:521)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
1月10日至24日:48:08.819:E / AndroidRuntime(29242):在dalvik.system.NativeStart.main(本机方法)
1月10日至24日:48:12.260:I /流程(29242):发送信号。 PID:29242 SIG:9
解决方案
在catch块 e.getMessage()
可能是空
。试试这个:
弦乐味精=(e.getMessage()== NULL)登录失败!:e.getMessage();
Log.i(登录ERROR1,味精);
I have a problem with my app when I try to login.
In fact, if I register (if the checkbox remerberMe
isChecked()
on register action, at the next connection the main activity is displayed otherwise the login activity appears) first, it works fine.
However, the Login
Activity doesn't work when it's called on the next connection after register action.
Login Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
setTitle(R.string.loginTitle);
// Importing all assets like buttons, text fields
inputUsername = (EditText) findViewById(R.id.username_value);
inputPassword = (EditText) findViewById(R.id.password_value);
btnLogin = (Button) findViewById(R.id.loginButton);
btnExit = (Button) findViewById(R.id.exitButton);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegister);
rememberMe = (CheckBox) findViewById(R.id.rememberNameRegister);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginAction();
}
});
// Link to Register Screen
btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
public void LoginAction() {
Log.i("Login", "Login Action");
progressDialog = ProgressDialog.show(LoginActivity.this, null,
getResources().getString(R.string.loginProgressMessage), true);
// try {
runOnUiThread(new Runnable() {
String username = inputUsername.getText().toString();
String password = inputPassword.getText().toString();
@Override
public void run() {
Log.i("Login Run", "username " + username);
switch (username.length()) {
case 0:
blankUserName();
break;
}
switch (password.length()) {
case 0:
blankPassWord();
// thread.stop();
break;
}
try {
if (username.length() > 0 && password.length() > 0) {
Log.i("Login Run", "password " + password);
// Password pass = new Password(username, password);
DatabaseHelper db = new DatabaseHelper(
getApplicationContext());
int count = db.getRowCount();
Log.i("Login", "getRowCompte " + count);
if (count == 1) {
Log.i("Login","getLogin1 ");
// if (db.getLogin(username, password) == 1) {
if (db.Login(username, password)== true) {
Log.i("Login", "rememberMe.isChecked()");
if (rememberMe.isChecked() == true) {
statut = "on";
} else if (rememberMe.isChecked() == false) {
statut = "off";
}
Log.i("Login", "ok ischecked");
Log.i("Login","getRowCompteStat "+ db.getRowCountStat());
if (db.getRowCountStat() == 1) {
db.UpdateStatut(statut);
Log.i("Login", "getRowCompte " + count);
}
Toast.makeText(getApplicationContext(),
"Student Moyenne \n Bienvenu!",
Toast.LENGTH_LONG).show();
Log.i("Login Run", "Connecté");
Intent moy = new Intent(
getApplicationContext(),
MoyenneMain.class);
// Close all views before launching
// Dashboard
moy.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(moy);
finish();
db.close();
} else if (db.Login(username, password)==false){
// if (db.getLogin(username, password) == 0) {
// Log.i("Login Run", "faux password");
Toast.makeText(LoginActivity.this,"Invalid Username/Password",Toast.LENGTH_LONG).show();
Intent login = new Intent(getApplicationContext(),LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
finish();
db.close();
}
} else if (count == 0) {
Log.i("Login Run", "Enregistrez vous");
Toast.makeText(LoginActivity.this,
"Enregistrez vous!", Toast.LENGTH_SHORT)
.show();
Intent register = new Intent(
getApplicationContext(),
RegisterActivity.class);
register.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(register);
finish();
db.close();
}
}
} catch (Exception e) {
Log.i("Login Error1", e.getMessage());
Toast.makeText(LoginActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
});
// } catch (Exception e) {
// Thread.currentThread().destroy();
// Log.i("Login Error2", e.getMessage());
// Toast.makeText(LoginActivity.this, e.getMessage(),
// Toast.LENGTH_LONG).show();
// }
}
private void blankUserName() {
Toast.makeText(LoginActivity.this, "Entrez un nom Utilisateur SVP!",
Toast.LENGTH_SHORT).show();
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
finish();
}
private void blankPassWord() {
Toast.makeText(LoginActivity.this, "Entrez un mot de passe SVP!",
Toast.LENGTH_SHORT).show();
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
finish();
}
A part of DatabaseHelper
public int getRowCountStat() {
String countQuery = "SELECT * FROM " + TABLE_STATUT;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();
// return row count
return rowCount;
}
public int getRowCount() {
String countQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();
// return row count
return rowCount;
}
public boolean Login(String username, String password) throws SQLException
{
Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_LOGIN + " WHERE username=? AND password=?"
, new String[]{username,password});
if (mCursor != null) {
if(mCursor.getCount() > 0)
{
return true;
}
}
return false;
}
public int UpdateStatut(String statut) {
final static int idStat = 1;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(STATUT, statut);
return db.update(TABLE_STATUT, cv, ID_STAT + "=?",
new String[] { String.valueOf(idStat) });
}
Logcat
10-24 01:48:08.819: E/AndroidRuntime(29242): FATAL EXCEPTION: main
10-24 01:48:08.819: E/AndroidRuntime(29242): java.lang.NullPointerException: println needs a message
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.util.Log.println_native(Native Method)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.util.Log.i(Log.java:143)
10-24 01:48:08.819: E/AndroidRuntime(29242): at com.android.moyenne.activity.LoginActivity$4.run(LoginActivity.java:163)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.app.Activity.runOnUiThread(Activity.java:3707)
10-24 01:48:08.819: E/AndroidRuntime(29242): at com.android.moyenne.activity.LoginActivity.LoginAction(LoginActivity.java:78)
10-24 01:48:08.819: E/AndroidRuntime(29242): at com.android.moyenne.activity.LoginActivity$1.onClick(LoginActivity.java:46)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.view.View.performClick(View.java:2408)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.view.View$PerformClick.run(View.java:8816)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.os.Handler.handleCallback(Handler.java:587)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.os.Looper.loop(Looper.java:123)
10-24 01:48:08.819: E/AndroidRuntime(29242): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-24 01:48:08.819: E/AndroidRuntime(29242): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 01:48:08.819: E/AndroidRuntime(29242): at java.lang.reflect.Method.invoke(Method.java:521)
10-24 01:48:08.819: E/AndroidRuntime(29242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-24 01:48:08.819: E/AndroidRuntime(29242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-24 01:48:08.819: E/AndroidRuntime(29242): at dalvik.system.NativeStart.main(Native Method)
10-24 01:48:12.260: I/Process(29242): Sending signal. PID: 29242 SIG: 9
解决方案
In the catch block e.getMessage()
may be null
. Try this:
String msg = (e.getMessage()==null)?"Login failed!":e.getMessage();
Log.i("Login Error1",msg);
这篇关于Android的显示java.lang.NullPointerException:需要的println消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!