0

I started learning from the tutorials on android.developers and I had a question:

In their "Starting another activity" Tutorial they create a method sendMessage in the MainActivity class in order to send the content of an EditText in MainActivity to a ViewText in a second activity.

The content is sent by using the Intent's setExtramethod.

They display the content using this code:

public class DisplayMessageActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);

// Set the text view as the activity layout
setContentView(textView);
}

   ....
}

I was wondering, why would they need to create a new TextView and not use the one given in the xml file of the activity by default (the "Hello World" one).

So I tried to do it by myself and my app crashes and I wanted to know what am I doing wrong and if this is the reason why they didn't do it the way I was thinking of doing it.

What I did is this:

    Fragment_display_message.xml
<LinearLayout 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:orientation="horizontal" >

    <TextView
        android:id="@+id/TVmessage"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="center"/>
</LinearLayout>

and they way I try to display the message in the DisplayMessageActivity :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView text = (TextView) findViewById(R.id.TVmessage);
    text.setText(message);

}

also sendMessage is done this way:

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

In summery my question is: If the way I do it is possible and i'm just missing something to make it work, is there a reason why they didn't do it this way? Why they didn't use the Activity's layout and just created a TextView in the code and set it as the content of the activity ?

Thanks for reading my question !

Edit:

public class DisplayMessageActivity extends ActionBarActivity {
public static Intent intent;
public static TextView text;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
    intent=getIntent();
    text=(TextView) findViewById(R.id.TVmessage);

}

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_display_message,
                container, false);
            String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        text.setText(message);
        return rootView;
    }

}
  • post your error log ..and point out at what line the error occurs – Rachita Nanda May 22 '14 at 11:48
  • The view is in the fragment layout and not in the activity layout. http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – laalto May 22 '14 at 11:58
  • I've read your solution and I have a few questions, How should I find my TextView within the fragment class ? I can't use findViewById since it's not static So i've tried setting a static TextView and using findViewById in onCreat and then using setText in onCreatView but it just gives me a NPE –  May 23 '14 at 15:42

1 Answers1

0

Its because you change the content by adding the placeholder fragment and the TextView goes out of scope. If you remove the setting of the fragment it should work.

Another issue may be that the message variable is actually null when retrieved from the bundle. Double check that too.

KMLong
  • 8,345
  • 2
  • 16
  • 19