使用putExtra和getExtras的Nullpointer异常

时间:2014-05-13 16:59:53

标签: java android sqlite android-intent nullpointerexception

我有一个用户签名的活动,我想保留下一个活动的用户名。 这就是我所拥有的:

public void onClick(View v) 
{
    // get The User name and Password
    String userName=editTextUserName.getText().toString();
    String password=editTextPassword.getText().toString();
    String user = userName;
    // fetch the Password from database for respective user name
    String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);

    // check if the Stored password matches with  Password entered by user
    if(password.equals(storedPassword))
    {
        Toast.makeText(MainActivity.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show(); 
        //dialog.dismiss();
        Intent intentLetterChecker=new Intent(MainActivity.this,HomeActivity.class);
        intentLetterChecker.putExtra("Username", user);
        startActivity(intentLetterChecker);

    }
}

注意:我还尝试过没有String用户,只使用userName但没有区别。然后在我的信函检查意图我全局声明当前用户和我的onCreate我有:

Bundle extras = getIntent().getExtras();
        currentUser = extras.getString("Username");

这是我的logcat:

05-13 11:41:41.424: E/AndroidRuntime(8971): FATAL EXCEPTION: main
05-13 11:41:41.424: E/AndroidRuntime(8971): Process: com.gmail.Sheridjohn.letterchecker, PID: 8971
05-13 11:41:41.424: E/AndroidRuntime(8971): java.lang.NullPointerException
05-13 11:41:41.424: E/AndroidRuntime(8971):     at com.gmail.Sheridjohn.letterchecker.LowerCaseActivity.onGesturePerformed(LowerCaseActivity.java:88)
05-13 11:41:41.424: E/AndroidRuntime(8971):     at android.gesture.GestureOverlayView.fireOnGesturePerformed(GestureOverlayView.java:729)
05-13 11:41:41.424: E/AndroidRuntime(8971):     at android.gesture.GestureOverlayView.access$400(GestureOverlayView.java:55)
05-13 11:41:41.424: E/AndroidRuntime(8971):     at android.gesture.GestureOverlayView$FadeOutRunnable.run(GestureOverlayView.java:744)
05-13 11:41:41.424: E/AndroidRuntime(8971):     at android.os.Handler.handleCallback(Handler.java:733)
05-13 11:41:41.424: E/AndroidRuntime(8971):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-13 11:41:41.424: E/AndroidRuntime(8971):     at android.os.Looper.loop(Looper.java:136)
05-13 11:41:41.424: E/AndroidRuntime(8971):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-13 11:41:41.424: E/AndroidRuntime(8971):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 11:41:41.424: E/AndroidRuntime(8971):     at java.lang.reflect.Method.invoke(Method.java:515)
05-13 11:41:41.424: E/AndroidRuntime(8971):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-13 11:41:41.424: E/AndroidRuntime(8971):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-13 11:41:41.424: E/AndroidRuntime(8971):     at dalvik.system.NativeStart.main(Native Method)

我的应用程序记录了一个用户,显示了一个字母表,供他们绘制,每个字母有两个计数器。当用户获得正确的字母时,一个递增,而当另一个不正确时,另一个递增。我想将此信息插入到我的SQLite数据库中,因为这似乎是最明智的方法。所以我的信函检查器中的插入方法如下:

public void insertCorrectInt(String user, String letter,int value)
{
    ContentValues newValues = new ContentValues();
    // Assign values for each row.
    newValues.put("USERNAME", user);
    newValues.put("LETTER", letter);
    newValues.put("ISCORRECT",value);

    // Insert the row into table
    db.insert("LOGIN", null, newValues);
}

我称之为:

if(letters[i].equals("a"))
{
    correctCountera++;
    db.insertCorrectInt(currentUser,"a",correctCountera);
}
if(letters[i].equals("b"))
{
    correctCounterb++;
    db.insertCorrectInt(currentUser,"b",correctCounterb);
}

非常感谢任何帮助/建议,谢谢!

编辑: 我忘记发布我的onGesturePerformed()抱歉!这是漫长而有点暴力的力量:

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) 
{ 
ArrayList<Prediction> predictions = new ArrayList<Prediction>();  //this methods asks to recognize the gesture against loaded gesture library
predictions = mLibrary.recognize(gesture);

// We want at least one prediction
if (predictions.size() > 0)
{
    Prediction prediction = predictions.get(0);    //get the 1st prediction auto generated for you by Android
    // We want at least some confidence in the result
    if (prediction.score > 1.0 && prediction.name.equals(letters[i]))
    {
        // Show the spell
        //Toast.makeText(MainActivity.this, prediction.name, Toast.LENGTH_SHORT).show();

        AlertDialog.Builder correctAlertDialogBuilder = new AlertDialog.Builder(LowerCaseActivity.this);

            // set title
        correctAlertDialogBuilder.setTitle("Correct!");

        if(letters[i].equals("a"))
        {
            correctCountera++;
            db.insertCorrectInt(currentUser,"a",correctCountera);
        }
        if(letters[i].equals("b"))
        {
            correctCounterb++;
            db.insertCorrectInt(currentUser,"b",correctCounterb);
        }
        if(letters[i].equals("c"))
        {
            correctCounterc++;
            db.insertCorrectInt(currentUser,"c",correctCounterc);
        }
        if(letters[i].equals("d"))
        {
            correctCounterd++;
            db.insertCorrectInt(currentUser,"d",correctCounterd);
        }
        if(letters[i].equals("e"))
        {
            correctCountere++;
            db.insertCorrectInt(currentUser,"e",correctCountere);
        }
        if(letters[i].equals("f"))
        {
            correctCounterf++;
            db.insertCorrectInt(currentUser,"f",correctCounterf);
        }
        if(letters[i].equals("g"))
        {
            correctCounterg++;
            db.insertCorrectInt(currentUser,"g",correctCounterg);
        }
        if(letters[i].equals("h"))
        {
            correctCounterh++;
            db.insertCorrectInt(currentUser,"h",correctCounterh);
        }
        if(letters[i].equals("i"))
        {
            correctCounteri++;
            db.insertCorrectInt(currentUser,"i",correctCounteri);
        }
        if(letters[i].equals("j"))
        {
            correctCounterj++;
            db.insertCorrectInt(currentUser,"j",correctCounterj);
        }
        if(letters[i].equals("k"))
        {
            correctCounterk++;
            db.insertCorrectInt(currentUser,"k",correctCounterk);
        }
        if(letters[i].equals("l"))
        {
            correctCounterk++;
            db.insertCorrectInt(currentUser,"l",correctCounterl);
        }
        if(letters[i].equals("m"))
        {
            correctCounterm++;
            db.insertCorrectInt(currentUser,"m",correctCounterm);
        }
        if(letters[i].equals("n"))
        {
            correctCountern++;
            db.insertCorrectInt(currentUser,"n",correctCountern);
        }
        if(letters[i].equals("o"))
        {
            correctCountero++;
            db.insertCorrectInt(currentUser,"o",correctCountero);
        }
        if(letters[i].equals("p"))
        {
            correctCounterp++;
            db.insertCorrectInt(currentUser,"p",correctCounterp);
        }
        if(letters[i].equals("q"))
        {
            correctCounterq++;
            db.insertCorrectInt(currentUser,"q",correctCounterq);
        }
        if(letters[i].equals("r"))
        {
            correctCounterr++;
            db.insertCorrectInt(currentUser,"CORRECTr",correctCounterr);
        }
        if(letters[i].equals("s"))
        {
            correctCounters++;
            db.insertCorrectInt(currentUser,"s",correctCounters);
        }
        if(letters[i].equals("t"))
        {
            correctCountert++;
            db.insertCorrectInt(currentUser,"t",correctCountert);
        }
        if(letters[i].equals("u"))
        {
            correctCounteru++;
            db.insertCorrectInt(currentUser,"u",correctCounteru);
        }
        if(letters[i].equals("v"))
        {
            correctCounterv++;
            db.insertCorrectInt(currentUser,"v",correctCounterv);
        }
        if(letters[i].equals("w"))
        {
            correctCounterw++;
            db.insertCorrectInt(currentUser,"w",correctCounterw);
        }

        if(letters[i].equals("x"))
        {
            correctCounterx++;
            db.insertCorrectInt(currentUser,"x",correctCounterx);
        }
        if(letters[i].equals("y"))
        {
            correctCountery++;
            db.insertCorrectInt(currentUser,"y",correctCountery);
        }
        if(letters[i].equals("z"))
        {
            correctCounterz++;
            db.insertCorrectInt(currentUser,"z",correctCounterz);
        }

            // set dialog message
        correctAlertDialogBuilder
        .setMessage("Would you like to move on or try again?")
        .setCancelable(false)
        .setPositiveButton("Move on",new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog,int id) 
            {
                // if this button is clicked, increment i and close alert
                if(i == 25)
                {
                    i = 0;
                    updateTextView(guide);
                }
                else
                {
                    i++;
                    updateTextView(guide);
                }
                dialog.cancel();
            }})

        .setNegativeButton("Try Again",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) 
        {
            // if this button is clicked, just close the dialog box and do nothing
            dialog.cancel();
        }});

        // create alert dialog
        AlertDialog correctDialog = correctAlertDialogBuilder.create();

        // show it
        correctDialog.show();
    }
    else
    {
        AlertDialog.Builder incorrectAlertDialogBuilder = new AlertDialog.Builder(LowerCaseActivity.this);

        // set title
        incorrectAlertDialogBuilder.setTitle("Incorrect!");

        /*if(letters[i] == "a")
        {
            incorrectCountera++;
            loginDataBaseAdapter.insertInt("INCORRECTa",incorrectCountera);
        }
        if(letters[i] == "b")
        {
            incorrectCounterb++;
            loginDataBaseAdapter.insertInt("INCORRECTb",incorrectCounterb);
        }
        if(letters[i] == "c")
        {
            incorrectCounterc++;
            loginDataBaseAdapter.insertInt("INCORRECTc",incorrectCounterc);
        }
        if(letters[i] == "d")
        {
            incorrectCounterd++;
            loginDataBaseAdapter.insertInt("INCORRECTd",incorrectCounterd);
        }
        if(letters[i] =="e")
        {
            incorrectCountere++;
            loginDataBaseAdapter.insertInt("INCORRECTe",incorrectCountere);
        }
        if(letters[i] == "f")
        {
            incorrectCounterf++;
            loginDataBaseAdapter.insertInt("INCORRECTf",incorrectCounterf);
        }
        if(letters[i] == "g")
        {
            incorrectCounterg++;
            loginDataBaseAdapter.insertInt("INCORRECTg",incorrectCounterg);
        }
        if(letters[i] == "h")
        {
            incorrectCounterh++;
            loginDataBaseAdapter.insertInt("INCORRECTh",incorrectCounterh);
        }
        if(letters[i] == "i")
        {
            incorrectCounteri++;
            loginDataBaseAdapter.insertInt("INCORRECTi",incorrectCounteri);
        }
        if(letters[i] == "j")
        {
            incorrectCounterj++;
            loginDataBaseAdapter.insertInt("INCORRECTj",incorrectCounterj);
        }
        if(letters[i] == "k")
        {
            incorrectCounterk++;
            loginDataBaseAdapter.insertInt("INCORRECTk",incorrectCounterk);
        }
        if(letters[i] == "l")
        {
            incorrectCounterk++;
            loginDataBaseAdapter.insertInt("INCORRECTl",incorrectCounterl);
        }
        if(letters[i] == "m")
        {
            incorrectCounterm++;
            loginDataBaseAdapter.insertInt("INCORRECTm",incorrectCounterm);
        }
        if(letters[i] == "n")
        {
            incorrectCountern++;
            loginDataBaseAdapter.insertInt("INCORRECTn",incorrectCountern);
        }
        if(letters[i] == "o")
        {
            incorrectCountero++;
            loginDataBaseAdapter.insertInt("INCORRECTo",incorrectCountero);
        }
        if(letters[i] == "p")
        {
            incorrectCounterp++;
            loginDataBaseAdapter.insertInt("INCORRECTp",incorrectCounterp);
        }
        if(letters[i] == "q")
        {
            incorrectCounterq++;
            loginDataBaseAdapter.insertInt("INCORRECTq",incorrectCounterq);
        }
        if(letters[i] == "r")
        {
            incorrectCounterr++;
            loginDataBaseAdapter.insertInt("INCORRECTr",incorrectCounterr);
        }
        if(letters[i] == "s")
        {
            incorrectCounters++;
            loginDataBaseAdapter.insertInt("INCORRECTs",incorrectCounters);
        }
        if(letters[i] == "t")
        {
            incorrectCountert++;
            loginDataBaseAdapter.insertInt("INCORRECTt",incorrectCountert);
        }
        if(letters[i] == "u")
        {
            correctCounteru++;
            loginDataBaseAdapter.insertInt("INCORRECTu",correctCounteru);
        }
        if(letters[i] == "v")
        {
            incorrectCounterv++;
            loginDataBaseAdapter.insertInt("INCORRECTv",incorrectCounterv);
        }
        if(letters[i] == "w")
        {
            incorrectCounterw++;
            loginDataBaseAdapter.insertInt("INCORRECTw",incorrectCounterw);
        }

        if(letters[i] == "x")
        {
            incorrectCounterx++;
            loginDataBaseAdapter.insertInt("INCORRECTx",incorrectCounterx);
        }
        if(letters[i] == "y")
        {
            incorrectCountery++;
            loginDataBaseAdapter.insertInt("INCORRECTy",incorrectCountery);
        }
        if(letters[i] == "z")
        {
            correctCounterz++;
            loginDataBaseAdapter.insertInt("INCORRECTi",correctCounteri);
        }*/

        // set dialog message
        incorrectAlertDialogBuilder
        .setMessage("Would you like to move on or try again?")
        .setCancelable(false)
        .setPositiveButton("Move on",new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog,int id) 
            {
                // if this button is clicked, increment i and close alert
                if(i == 25)
                {
                    i = 0;
                    updateTextView(guide);
                }
                else
                {
                    i++;
                    updateTextView(guide);
                }
                dialog.cancel();
            }})

        .setNegativeButton("Try Again",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) 
            {
                // if this button is clicked, just close the dialog box and do nothing
                dialog.cancel();
            }});

        // create alert dialog
        AlertDialog incorrectDialog = incorrectAlertDialogBuilder.create();

        incorrectDialog.show();
    }
}

Java第88行是{

之后的public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture)

0 个答案:

没有答案