I am attempting to complete the Android HelloWorld App using Eclipse and the ADT. However, the app always crashes when I implement the second activity and press the \"Send\" but
In displayMessageActivity, you set the content view to a textView, but it should be activity_display_message.xml instead because that's where your container is defined. So that's why the fragment manager can't find any container to add the fragment. You can pass the text to the fragment so that it will handle setting the text to the textView.
Try something like:
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, PlaceholderFragment.newInstance(message)).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
private static final String ARG_MESSAGE = "ARG_MESSAGE";
public static PlaceholderFragment newInstance(String message) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putString(ARG_MESSAGE, message);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
final String message = getArguments().getString(ARG_MESSAGE);
final TextView tv = (TextView) rootView.findViewById( id of your textview );
tv.setText(message);
return rootView;
}
}
According to this line:
No view found for id 0x7f05003c (com.example.myfirstapp:id/container) for fragment PlaceholderFragment{4137cd30 #0 id=0x7f05003c}
You never declare your R.id.container
item. And it's right - you never do declare it in your XML layout. I'm not exactly sure what you're trying to do with that, but you should add that to your XML layout, or just remove it (it doesn't really look like you're using it).
It doesn't really look like the fragment you declare is needed. As it's just a simple activity, a fragment shouldn't really be needed.
I had similar problem. When I rechecked the tutorial from which you are also learning i.e. developer.android, I found that in the onCreate method of DisplayMessageActivity class the following codes of lines are causing disruptions,(I have commented it)
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, PlaceholderFragment.newInstance(message)).commit();
}
Please delete these lines. If you carefully observe the tutorial there they present the code of onCreate at last to cross check, there these line are missing. :)