anon
anon

Reputation:

String is null while obtaining from another class using getIntent

Right now I am have a activity screen with edittext lines that I am obtaining user input from. When I hit the create event button on the button, the event text should populate into a news feed that I have on another activity.

enter image description here

Here is the portion CreateEvent activity/screen code:

@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();



            }
        });
    }

When the create event button is pressed, it creates an event into the MainActivity screen/activity; however, the user input is null. I am not sure why this is happening because I believe I am using the intent methods correctly.

Here is my MainActivity screen.

enter image description here

Here is the getIntent method in my onCreate method in my MainActivity

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

However, when I call my addEvent method:

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

    }

The event input text is null. I am not sure why this is happening.

I tried to resolve it but no luck,

How to send string from one activity to another?

Pass a String from one Activity to another Activity in Android

Any idea as for how to resolve this?!

Thanks!

Upvotes: 1

Views: 101

Answers (1)

Sharp Edge
Sharp Edge

Reputation: 4192

Don't use Intent to get the String you put through putExtra()

 Intent intent = getIntent(); // don't use this
 if (null != intent) {
    test = intent.getStringExtra("WhatEvent_String");
}

Instead Use Bundle, an example snippet

    Bundle extra = getIntent().getExtras();
      if(extra!=null){
        test= extra.getString("WhatEvent_String");
    }

EDIT

I noticed just now that you're calling the method main.addEvent() from the wrong place ! Call it in the MainActivity in the onCreate() method.

Remove these lines

           MainActivity  main= new MainActivity();

            //make sure you call method from other class correctly


            main.addEvent();

and in your MainActivity

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

    Bundle extra = getIntent().getExtras();
         if(extra!=null){
            test= extra.getString("WhatEvent_String");
      }
    addEvent(); // call your addEvent() method here
}

Upvotes: 1

Related Questions