Android java.lang.NullPointerException:println需要一条消息[重复]

Posted

技术标签:

【中文标题】Android java.lang.NullPointerException:println需要一条消息[重复]【英文标题】:Android java.lang.NullPointerException: println needs a message [duplicate] 【发布时间】:2013-10-24 02:23:06 【问题描述】:

当我尝试登录时,我的应用出现问题。

事实上,如果我先注册(如果复选框remerberMe isChecked() 注册操作,在下一个连接时会显示主要活动,否则会出现登录活动),它工作正常。

但是,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();
    

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

【问题讨论】:

【参考方案1】:

在catch 块中e.getMessage() 可能是null。试试这个:

String msg = (e.getMessage()==null)?"Login failed!":e.getMessage();
Log.i("Login Error1",msg); 

【讨论】:

我已经完成了,但是现在消息“登录失败!”出现在 logcat 和 Toast 中(因此 e.getmessage 返回 null)并且线程不会自动销毁但是 getRowCount() 返回 1 并且我输入的用户名和密码与我在注册时输入的相同。所以db.Login(username, password) 应该返回 true 但它返回 false。销毁线程我想使用Thread.currentThread().destroy(); 但destroy() 是删除线。请帮帮我 既然要进入catch块,首先你应该找出抛出了什么异常,在catch中做一个e.printStackTrace()并报告错误日志。此外,由于它与您之前提出的问题完全无关,您可能需要关闭此问题并提出新问题,以便更多人可以帮助您。【参考方案2】:

图片是你的imageView,imageurl是你从服务器加载图片的url。

try 
    InputStream in = new java.net.URL(imageURl).openStream();
    this.image = BitmapFactory.decodeStream(in);             
 catch (Exception e)  
    String msg = (e.getMessage() == null) ? "image load failed!" :e.getMessage();
    Log.i("image load error", msg);

【讨论】:

以上是关于Android java.lang.NullPointerException:println需要一条消息[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}

Java关于e.printStackTrace()介绍

Android逆向系列文章— Android基础逆向

Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )

Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )

android 21 是啥版本