使用Intent在第二个活动中使用putExtra和getExtra方法接收空数据

用户3930098

我有singlePlaceActivity类,我想从该类中将在OnpostExecute中获得的字符串数据(如名称,地址)传递给另一个Email活动我的singlePlace活动成功启动了电子邮件活动,但是我没有string data使用getextra方法在电子邮件活动中收到putExtra ,并且我从logcat进行验证时,我的字符串数据名称,地址,电话没有空值。

SinglePlaceActivity:

    package info.androidhive.slidingmenu;

    import android.app.Activity;      
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.text.Html;
    import android.util.Log;
    import android.widget.TextView;

    public class SinglePlaceActivity extends Activity {
    // flag for Internet connection status
    Boolean isInternetPresent = false;

    // Connection detector class
    ConnectionDetector cd;

    // Alert Dialog Manager
    AlertDialogManager alert = new AlertDialogManager();

    // Google Places
    GooglePlaces googlePlaces;

    // Place Details
    PlaceDetails placeDetails;

    // Progress dialog
    ProgressDialog pDialog;

    // KEY Strings
          // public static String KEY_REFERENCE = "reference"; // id of the place
            public static String reference_value = "reference";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_place);

        Intent i = getIntent();

        // Place referece id
        String reference = i.getStringExtra(reference_value);

        // Calling a Async Background thread
        new LoadSinglePlaceDetails().execute(reference);
    }


    /**
     * Background Async Task to Load Google places
     * */
    class LoadSinglePlaceDetails extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(SinglePlaceActivity.this);
            pDialog.setMessage("Passing Restaurent details to Email ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting Profile JSON
         * */
        protected String doInBackground(String... args) {
            String reference = args[0];

            // creating Places class object
            googlePlaces = new GooglePlaces();

            // Check if used is connected to Internet
            try {
                placeDetails = googlePlaces.getPlaceDetails(reference);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed Places into LISTVIEW
                     * */
                    if(placeDetails != null){
                        String status = placeDetails.status;

                        // check place deatils status
                        // Check for all possible status
                        if(status.equals("OK")){
                            if (placeDetails.result != null) {
                                String name = placeDetails.result.name;
                                String address = placeDetails.result.formatted_address;
                                String phone = placeDetails.result.formatted_phone_number;
                                String latitude = Double.toString(placeDetails.result.geometry.location.lat);
                                String longitude = Double.toString(placeDetails.result.geometry.location.lng);

                                Intent in = new Intent(getApplicationContext(),Email.class);
                               in.putExtra("nameValue",name );  
                               in.putExtra("addValue",address ); 
                               in.putExtra("urlValue",phone ); 

                               startActivity(in);


                               Log.d("Place ", name + address + phone + latitude + longitude);


                            }
                        }
                        else if(status.equals("ZERO_RESULTS")){
                            alert.showAlertDialog(SinglePlaceActivity.this, "Near Places",
                                    "Sorry no place found.",
                                    false);
                        }
                        else if(status.equals("UNKNOWN_ERROR"))
                        {
                            alert.showAlertDialog(SinglePlaceActivity.this, "Places Error",
                                    "Sorry unknown error occured.",
                                    false);
                        }
                        else if(status.equals("OVER_QUERY_LIMIT"))
                        {
                            alert.showAlertDialog(SinglePlaceActivity.this, "Places Error",
                                    "Sorry query limit to google places is reached",
                                    false);
                        }
                        else if(status.equals("REQUEST_DENIED"))
                        {
                            alert.showAlertDialog(SinglePlaceActivity.this, "Places Error",
                                    "Sorry error occured. Request is denied",
                                    false);
                        }
                        else if(status.equals("INVALID_REQUEST"))
                        {
                            alert.showAlertDialog(SinglePlaceActivity.this, "Places Error",
                                    "Sorry error occured. Invalid Request",
                                    false);
                        }
                        else
                        {
                            alert.showAlertDialog(SinglePlaceActivity.this, "Places Error",
                                    "Sorry error occured.",
                                    false);
                        }
                    }else{
                        alert.showAlertDialog(SinglePlaceActivity.this, "Places Error",
                                "Sorry error occured.",
                                false);
                    }


                }
            });

        }

    }

}

这是我的第二个接收活动,即电子邮件活动Email.java

    package info.androidhive.slidingmenu;
    import org.json.JSONObject;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    public class Email extends Activity implements View.OnClickListener {

    EditText personsEmail, rname, radd, rurl, yourmsg;

    String emailAdd, beginning,  stupidAction, hatefulAct, address;
    Button sendEmail;






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.email);

        //String beginning = getIntent().getStringExtra("extraname") ;//returns null if nothing
       // String address = getIntent().getStringExtra("extraname2");
        //String stupidAction = getIntent().getStringExtra("extraname2");

        beginning = getIntent().getStringExtra("nameValue");
        address = getIntent().getStringExtra("addValue");
        stupidAction= getIntent().getStringExtra("urlValue");
       // beginning = intent.getStringExtra("nameValue");
        //address = intent.getStringExtra("addValue");
       // stupidAction = intent.getStringExtra("urlValue");



        initializeVars();

        sendEmail.setOnClickListener(this);
    }

    private void initializeVars() {
        // TODO Auto-generated method stub
        personsEmail = (EditText) findViewById(R.id.etEmails);
        rname = (EditText) findViewById(R.id.etIntro);
        radd = (EditText) findViewById(R.id.etName);
        rurl = (EditText) findViewById(R.id.etThings);
        yourmsg = (EditText) findViewById(R.id.etAction);

        sendEmail = (Button) findViewById(R.id.bSentEmail);
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub



        convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated();
        String emailaddress[] = { emailAdd };
        String message = "Restaurent Name:"+ beginning

                + " Restaurent Address:"+ address

                + "Click for more details:" + stupidAction

                + "Message:" + hatefulAct;

                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT," Restaurent Recommendation");
                emailIntent.setType("plain/text");
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
                startActivity(emailIntent);


    }

    private void convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated() {
        // TODO Auto-generated method stub
        emailAdd = personsEmail.getText().toString();
        beginning = rname.getText().toString();
        address = radd.getText().toString();
        stupidAction = rurl.getText().toString();
        hatefulAct = yourmsg.getText().toString();

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }

}
考希克

如果使用AsyncTask,则不需要onPostExecute(...)内部因此,如果您在onPostExecute(..)中发送的那些值不为null,则删除问题内的线程将得到解决background thread onPostExecute(...)

通过AsyncTask,可以正确,轻松地使用UI线程。此类允许执行后台操作并在UI线程上发布结果,而无需操纵线程和/或处理程序。

尝试使用此代码onPostExecute(String response)代替您的代码

protected void onPostExecute(String response) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        /**
         * Updating parsed Places into LISTVIEW
         * */
        if (placeDetails != null) {
            String status = placeDetails.status;

            // check place deatils status
            // Check for all possible status
            if (status.equals("OK")) {
                if (placeDetails.result != null) {
                    String name = placeDetails.result.name;
                    String address = placeDetails.result.formatted_address;
                    String phone = placeDetails.result.formatted_phone_number;
                    String latitude = Double
                            .toString(placeDetails.result.geometry.location.lat);
                    String longitude = Double
                            .toString(placeDetails.result.geometry.location.lng);

                    Intent in = new Intent(getApplicationContext(), Email.class);
                    in.putExtra("nameValue", name);
                    in.putExtra("addValue", address);
                    in.putExtra("urlValue", phone);

                    startActivity(in);

                    Log.d("Place ", name + address + phone + latitude
                            + longitude);

                }
            } else if (status.equals("ZERO_RESULTS")) {
                alert.showAlertDialog(SinglePlaceActivity.this, "Near Places",
                        "Sorry no place found.", false);
            } else if (status.equals("UNKNOWN_ERROR")) {
                alert.showAlertDialog(SinglePlaceActivity.this, "Places Error",
                        "Sorry unknown error occured.", false);
            } else if (status.equals("OVER_QUERY_LIMIT")) {
                alert.showAlertDialog(SinglePlaceActivity.this, "Places Error",
                        "Sorry query limit to google places is reached", false);
            } else if (status.equals("REQUEST_DENIED")) {
                alert.showAlertDialog(SinglePlaceActivity.this, "Places Error",
                        "Sorry error occured. Request is denied", false);
            } else if (status.equals("INVALID_REQUEST")) {
                alert.showAlertDialog(SinglePlaceActivity.this, "Places Error",
                        "Sorry error occured. Invalid Request", false);
            } else {
                alert.showAlertDialog(SinglePlaceActivity.this, "Places Error",
                        "Sorry error occured.", false);
            }
        } else {
            alert.showAlertDialog(SinglePlaceActivity.this, "Places Error",
                    "Sorry error occured.", false);
        }

    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Intent在第二个活动中使用putExtra和getExtra方法接收空数据

来自分类Dev

如何在第二个活动中使用“后退”按钮关闭应用程序?

来自分类Dev

需要在Swift中使用可可绑定和核心数据使用第二个窗口

来自分类Dev

需要在Swift中使用可可绑定和核心数据来使用第二个窗口

来自分类Dev

从数据集中过滤第二个字段,然后在输出中使用uniq

来自分类Dev

Symfony 3在服务中使用第二个数据库

来自分类Dev

如何在python中使用子类的方法调用第二个父类的方法?

来自分类Dev

在Postgres中使用max和group by获取第二个属性

来自分类Dev

在Postgres中使用max和group by获取第二个属性

来自分类Dev

使用应用,位置和第二个应用更新熊猫数据框

来自分类Dev

使用 putExtra 和 getExtra 发送数据

来自分类Dev

使用第二个视图中的数据初始化第二个视图中的类

来自分类Dev

在第二个查询和嵌套结果中使用第一个mysqli查询中的数据

来自分类Dev

第二个活动是空白的

来自分类Dev

android eclipse如何使用intent在默认导航抽屉中显示第二个窗口

来自分类Dev

对于每个循环,在MVC视图中使用第二个数据库表模型

来自分类Dev

使用android espresso,如何访问第二个活动的视图?

来自分类Dev

NullPointerException使用捆绑软件启动第二个活动

来自分类Dev

使用startActivity调用第二个活动导致崩溃

来自分类Dev

Android Java - 使用意图将值传递给第二个活动

来自分类Dev

查找第二个空格,并在Excel中使用MID和SEARCH复制剩余的文本

来自分类Dev

查找第二个空格,并在Excel中使用MID和SEARCH复制剩余的文本

来自分类Dev

在VueJS 2中使用v-for第二个参数突出显示索引和索引+1

来自分类Dev

VBA:使用.Find方法查找第一个和第二个值出现

来自分类Dev

在第二个查询的值中使用第一个查询的值

来自分类Dev

在第二个查询的值中使用第一个查询的值

来自分类Dev

在第二个查询SQL中使用一个查询的值

来自分类Dev

在第二个 AS 语句中使用第一个 WITH AS 语句的结果

来自分类Dev

在“第二个活动”中捕获或接收ListViewItem的位置或名称

Related 相关文章

  1. 1

    使用Intent在第二个活动中使用putExtra和getExtra方法接收空数据

  2. 2

    如何在第二个活动中使用“后退”按钮关闭应用程序?

  3. 3

    需要在Swift中使用可可绑定和核心数据使用第二个窗口

  4. 4

    需要在Swift中使用可可绑定和核心数据来使用第二个窗口

  5. 5

    从数据集中过滤第二个字段,然后在输出中使用uniq

  6. 6

    Symfony 3在服务中使用第二个数据库

  7. 7

    如何在python中使用子类的方法调用第二个父类的方法?

  8. 8

    在Postgres中使用max和group by获取第二个属性

  9. 9

    在Postgres中使用max和group by获取第二个属性

  10. 10

    使用应用,位置和第二个应用更新熊猫数据框

  11. 11

    使用 putExtra 和 getExtra 发送数据

  12. 12

    使用第二个视图中的数据初始化第二个视图中的类

  13. 13

    在第二个查询和嵌套结果中使用第一个mysqli查询中的数据

  14. 14

    第二个活动是空白的

  15. 15

    android eclipse如何使用intent在默认导航抽屉中显示第二个窗口

  16. 16

    对于每个循环,在MVC视图中使用第二个数据库表模型

  17. 17

    使用android espresso,如何访问第二个活动的视图?

  18. 18

    NullPointerException使用捆绑软件启动第二个活动

  19. 19

    使用startActivity调用第二个活动导致崩溃

  20. 20

    Android Java - 使用意图将值传递给第二个活动

  21. 21

    查找第二个空格,并在Excel中使用MID和SEARCH复制剩余的文本

  22. 22

    查找第二个空格,并在Excel中使用MID和SEARCH复制剩余的文本

  23. 23

    在VueJS 2中使用v-for第二个参数突出显示索引和索引+1

  24. 24

    VBA:使用.Find方法查找第一个和第二个值出现

  25. 25

    在第二个查询的值中使用第一个查询的值

  26. 26

    在第二个查询的值中使用第一个查询的值

  27. 27

    在第二个查询SQL中使用一个查询的值

  28. 28

    在第二个 AS 语句中使用第一个 WITH AS 语句的结果

  29. 29

    在“第二个活动”中捕获或接收ListViewItem的位置或名称

热门标签

归档