android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.RelativeLayout android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.RelativeLayout android android

android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.RelativeLayout


All you need to do is Assign Id to Relative Layout it should be like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/rl_Container"android:layout_width="match_parent"android:layout_height="match_parent"><TextView    android:id="@+id/content"    android:layout_width="match_parent"    android:layout_height="match_parent"></TextView></RelativeLayout>

then just make a slight modification to your activity

public class DisplayMessageActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_display_message);    Intent intent = getIntent();    String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);    TextView textView = new TextView(this);    textView.setTextSize(40);    textView.setText(message);    RelativeLayout layout = (RelativeLayout) findViewById(R.id.rl_Container);    layout.addView(textView);}}


RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);

causing the problem.Because You are defining RelativeLayout with id of TextView.

Assign

android:id="@+id/content"

to RelativeLayout. Not to TextView in XML


Change your layout to this, the id content should go to the RelativeLayout and not the TextView.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/content"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.flover.hellloworld.DisplayMessageActivity">   <TextView   android:layout_width="wrap_content"   android:layout_height="wrap_content"/></RelativeLayout>