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 Simple Android App Basic Android Programming Adding the OnClick Method

brittany carter
brittany carter
341 Points

App Will not run Says App has closed unexpectedly and will not run.

Here is my code:

package brit3r.funfacts;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class FunFactActivity extends AppCompatActivity { // Declare our view variables private TextView mFactTextView; private Button mShowFactButton;

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

    // Assign the views from the layout file to the corresponding variables
    mFactTextView = (TextView) findViewById(R.id.factTextView);
    mShowFactButton = (Button) findViewById(R.id.showFactButton);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // The button was clicked, so update the fact TextView with a new factS
            String fact = "Ostriches can run faster than horses.";
            mFactTextView.setText(fact);

        }
    };
    mShowFactButton.setOnClickListener(listener);

}

}

2 Answers

Sang Tran
Sang Tran
6,918 Points

Hey, I don't see an import for RelativeLayout. Try adding import android.widget.RelativeLayout;

brittany carter
brittany carter
341 Points

Thank you! I will try that as soon as I can!

brittany carter
brittany carter
341 Points

This is still not working for me. Where should the import for Relative Layout be? I do not see this in your code and I tried adding this and it still does not work. When I try to run and open on the AVD it says that the app is not installed but I do see the app on the AVD

brittany carter
brittany carter
341 Points

This is still not working for me. Where should the import for Relative Layout be? I do not see this in your code and I tried adding this and it still does not work. When I try to run and open on the AVD it says that the app is not installed but I do see the app on the AVD

Sang Tran
Sang Tran
6,918 Points

Your FunFactsActivity.java file should have these imports

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView;

About you not being about to install your app but still able to run it

You should add category LAUNCHER and action MAIN to your AndroidManifest.xml Like this if they're not present

    <activity android:name=".FunFactsActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>