简体   繁体   中英

Run Hello World on Android Phone

as the problem I have above mentioned (during this tenure my phone is connected to the system and I prefer my phone instead of emulator) the installation of the app on my phone get complete but the app that had installed when I try to open the app my phone's screen blink that means the app open but very soon it disappers so why it happens and for java programming language I use android studio.... and their my code is

package com.example.filename.java;

class A
{
    public static void main(String []args)
    {
        System.out.println("Hello World");
    }
}

that was my code and another thing is that when I run the pre-written code of android studio the app opens and runs completely and the hello world appears. so is there problem in my code or anything else.

Android doesn't run on a JVM and System.out is only shown in logs.

For a Hello World application in android, you need to edit one xml file assuming you started a blank project in android and your main activity(MainActivity.java) is set as launcher activity in Android Manifest and setContentView is called on the xml file(activity_main.xml).

Go to the activity_main.xml file and it should come with a template having a Constraint Layout and a TextView in it(As of Android Studio Version 3.5.3). The text view has a property "text" to which you can assign "Hello World". When you run this on your emulator or android device it should show Hello World.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM