1

I just wanted to passing value from one activity to another but not able to find Textview value from the view.. I just passing one message from main activity to this activity through a text box and I'm seeing textbox value here but not able to assign that to textView...

here is my code..

public class DisplayMessageActivity extends Activity {

private TextView textView;

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new   PlaceholderFragment()).commit();
    }

    textView = (TextView) findViewById(R.id.displayText);
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    textView.setText(message);


}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.activityapps.DisplayMessageActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/displayText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="TextView" />

Please help me out..

ashast1
  • 15
  • 4
  • Please post the stack trace. Also, is your XML in a file called `activity_display_message.xml` in the layout folder? – Joffrey Apr 15 '14 at 08:36

2 Answers2

2

The TextView does not belong to the Activity layout. Move the initialization of TextView to onCreateView of PlaceholderFragment.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_display_message, container, false);
    TextView textView = (TextView) rootView.findViewById(R.id.displayText);
    return rootView;
}

In Activity

   if (savedInstanceState == null) {
   String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
   Bundle bundle = new Bundle();
   bundle.putString("key", message);
   PlaceholderFragment fragobj = new   PlaceholderFragment();
   fragobj.setArguments(bundle);
        getFragmentManager().beginTransaction()
                .add(R.id.container,  fragobj).commit();
    }

Then

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_display_message, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.displayText);
        String strtext = getArguments().getString("key");
        textView.setText(strtext);
        return rootView;
    }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thanks for the quick response.. I'm table to set the value for textview but how to get intent value from another fragment.. as getIntent() is giving error.. means what would be the code for Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); – ashast1 Apr 15 '14 at 08:52
  • @ashast1 you will have to pass the value from activity to fragment. http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android – Raghunandan Apr 15 '14 at 08:53
0
View view = inflater.inflate(R.layout.fragment_display_message, container, false);
TextView textView = (TextView) view.findViewById(R.id.displayText);
return view; //in onCreate method

you have extended from activity you can use also this

 TextView textView = (TextView) findViewById(R.id.displayText);
  //in onCreate method
Ruben JG
  • 97
  • 7