如何从putExtra方法获取数据

时间:2013-12-10 09:45:18

标签: android

我使用putExtra()提供数据。但数据没有到达另一页。
我认为数据检索会有一些问题。

这是 putExtra代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Bundle b = getIntent().getExtras();

    if (b != null && b.containsKey("email")) {
    email = getIntent().getExtras().getString("email");
    }

    if (b != null && b.containsKey("password")) {
        password = getIntent().getExtras().getString("password");
        }
    Log.i("Email",email+"VV");
    Log.i("Password",password+"AA");

     /*email=getIntent().getExtras().getString("email");
     password=getIntent().getExtras().getString("password");*/
     // Log.i("Email",email+"VV");
     // Log.i("Password",password+"AA");

    //jsonen();
     Imagebtn_start = (ImageButton) findViewById(R.id.image_btnstart);
     conDetector = new ConnectionDetector(getApplicationContext());
     Imagebtn_start.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            isInternetPresent = conDetector.isConnectingToInternet();

            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
               .detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
               .penaltyLog().build());

            ArrayList<NameValuePair> pp = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("email",email));
            postParameters.add(new BasicNameValuePair("password",password));

            String teamresponse = null;
            JSONObject json=null;
            String userID = null;
            JSONObject json_data = null;
            String teamStatus ="";
            try {
                  teamresponse=CustomHttpClient.executeHttpPost("http://10.0.2.2/football365/login.php", postParameters);
                  json = new JSONObject(teamresponse);
                  userID=json.getString("userID");
                }
            catch (Exception e) {

                e.printStackTrace();
            }
             Log.i("USERID", userID+"");


            String result = teamresponse.toString();
            JSONObject jsonobj = null;
            try{

            jsonobj = new JSONObject (result);
            teamStatus=jsonobj.getString("teamStatus");

            }
            catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
            }



            if (isInternetPresent){

                    if(isLogIn()){
                        if(teamStatus.equals("1")){
                            Log.i("Check it",isLogIn()+"");
                            Intent goLogin=new Intent(MainActivity.this,Team.class);
                            startActivity(goLogin);
                        }
                        else if(teamStatus.equals("0"))
                        {
                            Log.i("Check it no team",isLogIn()+"");
                            Intent goLogin=new Intent(MainActivity.this,NoTeam.class);
                            startActivity(goLogin);
                        }

                    }

                    else
                    {
                        Log.i("Check it in else",isLogIn()+"");
                        Intent goLogin=new Intent(MainActivity.this,Login.class);
                        startActivity(goLogin);
                    }


             }
             else{
                 showAlertDialog(MainActivity.this, "No Internet Connection",
                            "You don't have internet connection.", false);
             }
        }
    });
}

错误。

12-10 16:23:43.762: E/AndroidRuntime(1261): FATAL EXCEPTION: main
12-10 16:23:43.762: E/AndroidRuntime(1261): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.totallyfinish/com.example.totallyfinish.MainActivity}: java.lang.NullPointerException
12-10 16:23:43.762: E/AndroidRuntime(1261):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at android.os.Looper.loop(Looper.java:137)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at android.app.ActivityThread.main(ActivityThread.java:4340)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at java.lang.reflect.Method.invokeNative(Native Method)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at java.lang.reflect.Method.invoke(Method.java:511)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at dalvik.system.NativeStart.main(Native Method)
12-10 16:23:43.762: E/AndroidRuntime(1261): Caused by: java.lang.NullPointerException
12-10 16:23:43.762: E/AndroidRuntime(1261):     at com.example.totallyfinish.MainActivity.onCreate(MainActivity.java:57)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at android.app.Activity.performCreate(Activity.java:4465)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-10 16:23:43.762: E/AndroidRuntime(1261):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-10 16:23:43.762: E/AndroidRuntime(1261):     ... 11 more

我放了创建方法...... Plz,检查。我改变了我的代码,错误消失了。现在,我正在检查...... :)

这是putExtra方法。我知道这是复杂的编码,但请检查一下。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    String email=null;
    String password=null;

    img_btn_login=(ImageButton)findViewById(R.id.image_btn_login);
    img_btn_Register=(ImageButton)findViewById(R.id.image_btn_register);
    edit_txt_EmailAddress=(EditText)findViewById(R.id.edit_txt_EmailAddress);
    edit_txt_Password=(EditText)findViewById(R.id.editText1);

    prefsNagSetting = getApplicationContext().getSharedPreferences(NAG_PREFS, Context.MODE_PRIVATE);
    // GET THE NAG SETTING
    final boolean blNagSetting = prefsNagSetting.getBoolean(NAG_SETTING, false);



    img_btn_Register.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent goRegister=new Intent(Login.this,Register.class);
            startActivity(goRegister);
        }
    });

    link_forgetPassoword=(Button)findViewById(R.id.button1);
    link_forgetPassoword.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
            myWebLink.setData(Uri.parse("http://www.speedlink-td.info/service/football365/password/index.html"));
                startActivity(myWebLink);
        }
    });

    img_btn_login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(edit_txt_EmailAddress.getText().toString().trim().equals("")||edit_txt_EmailAddress.getText().toString().trim().equals(null)){
                 showAlertDialog(Login.this, "Email",
                            "Enter Email", false);
                edit_txt_EmailAddress.requestFocus();

            }

            else if(!isValidEmail(edit_txt_EmailAddress.getText().toString().trim()))
            {
                showAlertDialog(Login.this, "Email",
                        "Enter Valid Email", false);
                edit_txt_EmailAddress.selectAll();
            }

             else if(edit_txt_Password.getText().toString().equals("")||edit_txt_Password.getText().toString().trim().equals(null))
             {
                    showAlertDialog(Login.this, "Password", "Enter Password", false);
                    edit_txt_Password.requestFocus();
            }
            else{
                    JSONObject json=null;
                    int status=2;
                    String userID=null;
                    String teamID=null;
                    String teamName=null;
                    String admin=null;
                    String backNo=null;
                    String userName=null;

                    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                    postParameters.add(new BasicNameValuePair("email",edit_txt_EmailAddress.getText().toString()));//// define the parameter
                    postParameters.add(new BasicNameValuePair("password",edit_txt_Password.getText().toString()));

                    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                       .detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
                       .penaltyLog().build());

                    ArrayList<NameValuePair> pp = new ArrayList<NameValuePair>();
                    //postParameters.add(new BasicNameValuePair("userID","396797666"));

                    String response = null;
                    try {
                          response=CustomHttpClient.executeHttpPost("http://10.0.2.2/football365/login.php", postParameters);
                          json=new JSONObject(response);
                          userID=json.getString("userID");
                        }
                    catch (Exception e) {

                        e.printStackTrace();
                    }
                     Log.i("USERID", userID+"");
                     JSONObject json_data = null;
                     String teamStatus ="";

                    String result = response.toString();
                    JSONObject jsonobj = null;
                    try{

                    jsonobj = new JSONObject (result);
                    teamStatus=jsonobj.getString("teamStatus");

                    }
                    catch(JSONException e){
                        Log.e("log_tag", "Error parsing data "+e.toString());
                    }

                    Log.i("TeamStatus", result+"");

                    Log.i("RESULT", teamStatus+"");
                    //Log.i("halo",teamStatus+"");

                    String[] teamSaving=new String[6];
                    String[] noteamSaving=new String[2];

                if (blNagSetting == true){
                    if(teamStatus.equals("1")){
                        try {
                            Log.i("Team Kyaw Win tal","Log in ma twar buu.");

                            userName=jsonobj.getString("userName");
                            userID=jsonobj.getString("userID");
                            teamID=jsonobj.getString("teamID");
                            teamName=jsonobj.getString("teamName");
                            admin=jsonobj.getString("admin");
                            backNo=jsonobj.getString("backNo");

                            teamSaving[0]=userName;
                            teamSaving[1]=userID;
                            teamSaving[2]=teamID;
                            teamSaving[3]=teamName;
                            teamSaving[4]=admin;
                            teamSaving[5]=backNo;

                            String email=edit_txt_EmailAddress.getText().toString().trim();
                            String password = edit_txt_Password.getText().toString().trim();

                            Log.i("Email",email);
                            Log.i("Password",password);

                            Intent teamshitoMain=new Intent(Login.this,MainActivity.class);
                            teamshitoMain.putExtra("email", email);
                            teamshitoMain.putExtra("password", password);
                        //  teamshitoMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(teamshitoMain);


                            Log.i("USerName", userName+"");
                            Log.i("UserID",userID+"");
                            Log.i("TeamId",teamID+"");

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }


                        Intent teamshi=new Intent(Login.this,Team.class);
                        teamshi.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(teamshi);
                        finish();
                    }
                    else if(teamStatus.equals("0"))
                    {
                        try {

                            Log.i("NoTeam Kyaw Win tal","Log in ma twar buu.");
                            userName=jsonobj.getString("userName");
                            userID=jsonobj.getString("userID");

                            noteamSaving[0]=userName;
                            noteamSaving[1]=userID;

                            String email=edit_txt_EmailAddress.getText().toString().trim();
                            String password = edit_txt_Password.getText().toString().trim();

                            Log.i("Email",email);
                            Log.i("Password",password);

                            Intent teamshitoMain=new Intent(Login.this,MainActivity.class);
                            teamshitoMain.putExtra("email", email);
                            teamshitoMain.putExtra("password", password);
                        //  teamshitoMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(teamshitoMain);

                            Log.i("USerNameNN", userName+"");
                            Log.i("UserIDNN",userID+"");

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        Intent noteamshi=new Intent(Login.this,NoTeam.class);
                        noteamshi.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(noteamshi);
                        finish();
                    }

                }else
                {
                    if(teamStatus.equals("1")){
                        try {
                            Log.i("Team Win tal","Log in hmar");

                            userName=jsonobj.getString("userName");
                            userID=jsonobj.getString("userID");
                            teamID=jsonobj.getString("teamID");
                            teamName=jsonobj.getString("teamName");
                            admin=jsonobj.getString("admin");
                            backNo=jsonobj.getString("backNo");

                            teamSaving[0]=userName;
                            teamSaving[1]=userID;
                            teamSaving[2]=teamID;
                            teamSaving[3]=teamName;
                            teamSaving[4]=admin;
                            teamSaving[5]=backNo;

                            String email=edit_txt_EmailAddress.getText().toString().trim();
                            String password = edit_txt_Password.getText().toString().trim();

                            Log.i("Email",email);
                            Log.i("Password",password);

                            Intent teamshitoMain=new Intent(Login.this,MainActivity.class);
                            teamshitoMain.putExtra("email", email);
                            teamshitoMain.putExtra("password", password);
                        //  teamshitoMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(teamshitoMain);

                            Log.i("USerName", userName+"");
                            Log.i("UserID",userID+"");
                            Log.i("TeamId",teamID+"");



                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }


                        Intent teamshi=new Intent(Login.this,Team.class);
                        teamshi.putExtra("TeamSaving", teamSaving);
                        startActivity(teamshi);
                        insertSave();


                    }
                    else if(teamStatus.equals("0"))
                    {
                        try {
                            Log.i("noTeam Win tal","Log in hmar");

                            userName=jsonobj.getString("userName");
                            userID=jsonobj.getString("userID");

                            noteamSaving[0]=userName;
                            noteamSaving[1]=userID;

                            Log.i("USerNameNN", userName+"");
                            Log.i("UserIDNN",userID+"");

                            String email=edit_txt_EmailAddress.getText().toString().trim();
                            String password = edit_txt_Password.getText().toString().trim();
                            Log.i("Email",email);
                            Log.i("Password",password);

                            Intent teamshitoMain=new Intent(Login.this,MainActivity.class);
                            teamshitoMain.putExtra("email", email);
                            teamshitoMain.putExtra("password", password);
                        //  teamshitoMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(teamshitoMain);

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        Intent noteamshi=new Intent(Login.this,NoTeam.class);
                        noteamshi.putExtra("NoTeamSaving", noteamSaving);
                        startActivity(noteamshi);
                        insertSave();
                    }
                    else{
                        showAlertDialog(Login.this,"Invalid Email and Password", "Enter Valid Email and Password", false);
                    }
                }
                }
        }
    });

}

2 个答案:

答案 0 :(得分:1)

你有没有试过?

email=getIntent().getExtras().getString("email");
password=getIntent().getExtras().getString("password");

您还可以检查您的意图是否具有值。

String email="";
Bundle b = getIntent().getExtras();

if (b != null && b.containsKey("email")) {
    email = getIntent().getExtras().getString("email");
}

答案 1 :(得分:0)

试试这个:

Intent intentfromLogin = getIntent();
Bundle extras =intentfromLogin.getExtras();    
email=extras .getStringExtra("email");
password=extras .getStringExtra("password");

您需要从意图“额外数据”中获取