使用getIntent从另一个类获取时,String为null

时间:2015-01-24 07:24:36

标签: java android android-intent android-activity

现在我有一个带有edittext行的活动屏幕,我从中获取用户输入。当我点击按钮上的创建事件按钮时,事件文本应该填充到我在另一个活动上的新闻源中。

enter image description here

以下是CreateEvent活动/屏幕代码部分:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_event);

        CreateEvent_Button = (Button)findViewById(R.id.create_event_button);

        WhatEvent_Text   = (EditText)findViewById(R.id.what_event);
        WhenEventTime_Text   = (EditText)findViewById(R.id.time_event);
        WhenEventDate_Text   = (EditText)findViewById(R.id.date_event);
        WhereEvent_Text   = (EditText)findViewById(R.id.where_event);


        CreateEvent_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                 WhatEvent_String = WhatEvent_Text.getText().toString();
                 WhenEventTime_String = WhenEventTime_Text.getText().toString();
                 WhenEventDate_String = WhenEventDate_Text.getText().toString();
                 WhereEvent_String = WhereEvent_Text.getText().toString();



                Log.e("What: ", WhatEvent_String);
                Log.e("When_Time: ", WhenEventTime_String);
                Log.e("When_Date: ", WhenEventDate_String);
                Log.e("Where_Event: ", WhereEvent_String);


                Intent intent = new Intent(CreateEvent.this,MainActivity.class);
                intent.putExtra("WhatEvent_String", WhatEvent_String);
                intent.putExtra("WhenEventTime_String", WhenEventTime_String);
                intent.putExtra("WhenEventDate_String", WhenEventDate_String);
                intent.putExtra("WhereEvent_String", WhereEvent_String);

                startActivity(intent);

                MainActivity  main= new MainActivity();

                //make sure you call method from other class correctly


                main.addEvent();



            }
        });
    }

按下创建事件按钮时,它会在MainActivity屏幕/活动中创建一个事件;但是,用户输入为空。我不确定为什么会这样,因为我相信我正确使用了intent方法。

这是我的MainActivity屏幕。

enter image description here

以下是我的MainActivity中的onCreate方法中的getIntent方法

 Intent intent = getIntent();
    if (null != intent) {
        test = intent.getStringExtra("WhatEvent_String");
    }

但是,当我调用addEvent方法时:

protected  void addEvent() {
        // Instantiate a new "row" view.
       // final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.list_row, mContainerView, false);

        // Set the text in the new row to a random country.


        // Because mContainerView has android:animateLayoutChanges set to true,
        // adding this view is automatically animated.
        //mContainerView.addView(newView, 0);




        HitupEvent hitupEvent = new HitupEvent();



        hitupEvent.setTitle("Rohit "+ "wants to "+test  );

        hitupEvent.setThumbnailUrl(roro_photo);


        //hitupEvent.setRating(30);
        //hitupEvent.setYear(1995);

        //hitupEvent.setThumbnailUrl(null);

        ArrayList<String> singleAddress = new ArrayList<String>();

        singleAddress.add("17 Fake Street");
        singleAddress.add("Phoney town");
        singleAddress.add("Makebelieveland");


        hitupEvent.setGenre(singleAddress);



        hitupEventList.add(hitupEvent);

        adapter.notifyDataSetChanged();

    }

事件输入文本为null。我不确定为什么会这样。

我试图解决它,但没有运气,

How to send string from one activity to another?

Pass a String from one Activity to another Activity in Android

关于如何解决这个问题的任何想法?!

谢谢!

1 个答案:

答案 0 :(得分:1)

请勿使用Intent获取您String的{​​{1}}

putExtra()

而是使用 Intent intent = getIntent(); // don't use this if (null != intent) { test = intent.getStringExtra("WhatEvent_String"); } ,一个示例代码段

Bundle

修改

我刚才注意到你从错误的地方调用方法 Bundle extra = getIntent().getExtras(); if(extra!=null){ test= extra.getString("WhatEvent_String"); } !请使用main.addEvent()方法中的MainActivity进行调用。

删除这些行

onCreate()

并在 MainActivity main= new MainActivity(); //make sure you call method from other class correctly main.addEvent();

MainActivity