TomCB
TomCB

Reputation: 4063

setContentView causes crash

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

    }

Upvotes: 4

Views: 9664

Answers (4)

rmurthy88
rmurthy88

Reputation: 1

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

Upvotes: 0

Jagdish
Jagdish

Reputation: 2499

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!

Upvotes: 1

mimetist
mimetist

Reputation: 171

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

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

Try that.

Upvotes: 2

laalto
laalto

Reputation: 152827

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.

Upvotes: 1

Related Questions