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 with Java Basic Android Programming Making a Button Do Something

Basel Kanaan
Basel Kanaan
4,458 Points

hi i have a problem with the following code

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //assign the view from the layout file to the corresponding variables
        factTextView =  findViewById(R.id.textView) ;
    } 

i cant run the code as the video mentioned (R.id.factTextView) ,,, aslo i didnt understand some stuff in this video for example using the "R" class, ik it from java course but idk should i go and continue java intermediate level(to understand more coding) or continue with android

6 Answers

Hi Basel,

What have you called your TextView object in the layout file, the xml piece? What is its ID?

Steve.

Basel Kanaan
Basel Kanaan
4,458 Points

hi, here is the XML code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#51b46d"
    tools:context=".FunFactsActivity"
    android:padding="70dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Did You Know?"
        android:textColor="#E4186318"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a test application to know what type of programmer is me"
        android:textColorLink="@android:color/background_light"
        android:textSize="24sp"
        tools:layout_editor_absoluteX="2dp"
        tools:layout_editor_absoluteY="296dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="518dp"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:text="Show another Fun Fact"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="500dp" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="left" />

</androidx.constraintlayout.widget.ConstraintLayout>

also im having a probelm to run it on my smartphone it says

Error while executing: am start -n "com.example.funfacts/com.example.funfacts.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.funfacts/.MainActivity }
Error type 3
Error: Activity class {com.example.funfacts/com.example.funfacts.MainActivity} does not exist.

Error while Launching activity 

Can you post the content of the LogCat to show the error when you launch the application, please? That will show the exact cause that the application won't run.

Steve.

Basel Kanaan
Basel Kanaan
4,458 Points

yes this is the error on the catlog

2020-03-05 23:44:43.584 16619-16619/? E/libtrusty: tipc_connect: cannot open tipc device "/dev/trusty-ipc-dev0": No such file or directory 2020-03-05 23:44:43.584 16619-16619/? E/storageproxyd: failed (-2) to connect to storage server

Hmmm ... not sure about that. What happens when you run the app? Does the emulator show anything or does it just stop with an error message?

In Logcat, You'll have to scroll back until you find something like FATAL EXCEPTION followed by lots of lines of red text. The rest of the log is just looking at everything that is going on in the emulator or on the device. The message you have posted looks unrelated to your application.

Basel Kanaan
Basel Kanaan
4,458 Points

hi so i rewrite the application again and had same issue, ithink this might help

   --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bestanimequote, PID: 4940
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bestanimequote/com.example.bestanimequote.MainActivity}: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.RelativeLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3344)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3488)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2049)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7506)
        at java.lang.re
I/Process: Sending signal. PID: 4940 SIG: 9

Your app is trying to cast a ConstraintLayout to a RelativeLayout for whatever reason. Might be qorth downloading the project files and copying over the XML.