Skip to content Skip to sidebar Skip to footer

Android Helloworld App Crashes On Implementation Of Second Activity

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' button.

Solution 1:

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. :)

Solution 2:

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:

publicclassDisplayMessageActivityextendsActionBarActivity {

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intentintent= getIntent();
    Stringmessage= intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    setContentView(R.layout.activity_display_message);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, PlaceholderFragment.newInstance(message)).commit();
    }

}

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display_message, menu);
    returntrue;
}

@OverridepublicbooleanonOptionsItemSelected(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.intid= item.getItemId();
    if (id == R.id.action_settings) {
        returntrue;
    }
    returnsuper.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */publicstaticclassPlaceholderFragmentextendsFragment {

    privatestaticfinalStringARG_MESSAGE="ARG_MESSAGE";

    publicstatic PlaceholderFragment newInstance(String message) {
        PlaceholderFragmentfragment=newPlaceholderFragment();
        Bundleargs=newBundle();
        args.putString(ARG_MESSAGE, message);
        fragment.setArguments(args);

        return fragment;
    }

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewrootView= inflater.inflate(R.layout.fragment_display_message,
                container, false);
        finalStringmessage= getArguments().getString(ARG_MESSAGE);
        finalTextViewtv= (TextView) rootView.findViewById( id of your textview );
        tv.setText(message);
        return rootView;
    }
}

Solution 3:

According to this line:

No view found forid 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.

Post a Comment for "Android Helloworld App Crashes On Implementation Of Second Activity"