Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Starting the App Adding the Sign Up Screen

The problem at the start is really bad!

So, I am following tutorial completely and I can't run the application due to runtime errors that I will post here. First let me explain the problem. In the tutorial that this post is tagged to, almost near the end of the video he opens LoginActivity.java, but his file is a bit different than my. He didn't have this if statement. However, even without that it doesn't work. Anyone have any idea??? Additional to this what seems, that makes problem are the files included in project by default which are not included regularly fragment_main, fragment_login... Actually, every .xml file for activity has its own corresponding file for fragment. What are these and what can I do with them? @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); /the code bellow is the one, that we need to write, all the way down to the start of if statement/ mSignUpTextView=(TextView)findViewById(R.id.signUpText); mSignUpTextView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent=new Intent(LoginActivity.this, SignUpActivity.class);
            startActivity(intent);

        }
    });
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

}

ERRORS

If you want to see error log, please click here:

http://tinypic.com/r/34dh9nr/8

Additional Information

Please don't tell me that I should try another Android device, I tried several emulators, and I tried physical devices. There are two of us friends, doing the same thing on different laptops (OSX Mavericks(the last one)), we are using ADT on Eclipse, not Android Studio, and both of stuck on the same thing, even though we are following tutorial independently.

Thanks in advance,

I hope that someone will be able to solve this. For any additional information, please reply!

5 Answers

Actually, I found the solution for the problem and I am going to paste the code from MainActivity.java and to point out what I changed in order for my apps to work. There are comments in the last part of the code, which is a part that I worked with, all the other parts are self-generated and I copied the whole code, just to be consistent.

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .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.main, 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 {    //all the things should go in this class

        public PlaceholderFragment() { // this is just a constructor
            super();
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            /*In the following two lines I am declaring TextView and using object of type View that I created in line above
               and calling .findViewById from that object. Note that it has to be casted to the type you need.*/
            TextView answerLabel=(TextView)rootView.findViewById(R.id.textView1);
            Button getAnswerButton=(Button)rootView.findViewById(R.id.button1);

            return rootView;

        }

     }

}

As you can see, the code has some additional things, but the important thing is that the code should go in the class PlaceholderFragment. If there are more problems regarding this, feel free to ask.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Thanks for sharing! This all hopefully makes sense after you get through the stage that deals with Fragments directly.

Sage Elliott
Sage Elliott
30,003 Points

I am having what I assume is the same issue. Every time I add the onclicklistener lines of code my app will crash immediately 100% of the time. Ive tried a couple IDEs because I was using inteliJ, but it happens to me in eclipse as well. I've also tried putting the code above and below the "extra" code added and it still did not fix my issue. I have the same problem with other youtube android tutorials. I'm guessing there was some large update to the android layout after all the videos were made.

In short, I would also like to read what people think may fix this issue. one day I shall not be such a newbie...

I used fragment_main all the time, if I haven't I would probably have a problem at the beginning.

Yes, actually my previous answer is not complete, I used all the xml files that are fragments and I didn't touch activities until this problem happened.

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

I am quite sorry about this problem. The latest build of the Android tools now include Fragments be default for all activities, no matter what. This was just introduced a few weeks ago. I am recording fixes to videos that are affected next week, but it's been rather frustrating for students!

As of right now, your best solution is probably to just download the project files and use those instead of your own, or remove the Fragments manually. I hope to have better solutions in place as soon as possible.