Sorry to make another topic on this subjcet but I’ve already read several of them and still don’t know why I’m getting this error. I followed the “Building Your First App” tutorial on http://developer.android.com/training/basics/firstapp/index.html and then tried to modify my application to give a layout to DisplayMessageActivity Class. I’ve created a file called “activity_displaymessage.xml” and implemented it to the class.
I’m new to android and I’m trying to understand how this all works. What am I doing wrong?
Here is my code:
MainActivity.java
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.sampleandroid.MESSAGE";
.
.
.
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);
}
}
DisplayMessageActivity.java
public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textview = (TextView) findViewById(R.id.text_view);
textview.setText(message);
setContentView(textview);
}
}
activity_displaymessage.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:weightSum="2">
<TextView android:id="@+id/text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="30sp"/>
</LinearLayout>
You have two
setContentView.Your layout fileactivity_displaymessagehave already child viewR.id.text_viewthen why are you set it as setContentView().setContentView(R.layout.activity_displaymessage);andsetContentView(textview);Remove this line
setContentView(textview);