4

I'm trying the very first tutorial on the android website here

The goal is to enter a text and ship it to a next page to display it.

However, I always get "Unfortunately My First App has stopped".

I noticed that when I comment out setContentView(textView); it works.

Here is my code: (LogCat stopped showing problems for some reason so I can't give the log)

package com.example.myfirstapp; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class DisplayMessageActivity extends ActionBarActivity { @Override protected void onCreate(Bundle 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); super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } } 
1
  • 1
    move this two line to first line of onCreate() method and post logcat error. super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); Commented Apr 4, 2014 at 14:11

4 Answers 4

2

This lines should be right after your onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); 

Try that.

Sign up to request clarification or add additional context in comments.

Comments

1

Please try the below code and post the logcat.

 package com.example.myfirstapp; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class DisplayMessageActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); // Get the message from the intent Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); // Create the text view TextView textView = (TextView) findViewById(R.id.textView); textView.setTextSize(40); textView.setText(message); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } } } 

I think need to declare TextView also in xml. I hope this will help. Thanks!

1 Comment

i think you need setContentView(textView); after set text to TextView, because you create new TextView and don't use that on layout,
1

You cannot call setContentView() before super.onCreate().

Generally, super.onCreate() should be the first line in your overridden onCreate().

Note that you already have setContentView() later that overrides your commented-out setContentView() content view.

Comments

0

The solution to the problem is while creating new android activity for DisplayMessageActivity, make sure its Hierarchical parent is selected as the MainActivity.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.