Android java.lang.NullPointerException:println需要一条消息

时间:2013-10-24 02:23:07

标签: android nullpointerexception logcat

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

事实上,如果我注册(如果注册表操作上的复选框remerberMe isChecked(),则在下次连接时显示主要活动,否则显示登录活动),首先,它可以正常工作。

但是,Login活动在注册操作后在下一个连接上调用时不起作用。

登录活动

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

2 个答案:

答案 0 :(得分:9)

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

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

答案 1 :(得分:0)

图像是你的imageView和imageurl是你的服务器上的toload图像的网址。

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);
}