Home>

I tried to write up to the second decimal place, but an error occurred.
Is there a solution?

11/22 13:38:17: Launching'app' on MyQ (Pixel2).
$adb shell am start -n "com.example.intentapplication/com.example.intentapplication.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 22745 on device'emulator-5554'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/tentapplicatio: Not late-enabling -Xcheck: jni (already on)
E/tentapplicatio: Unknown bits set in runtime_flags: 0x8000
W/tentapplicatio: Unexpected CPU variant for X86 using defaults: x86
D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
W/libc: Unable to set property "qemu.gles" to "1": connection failed;errno = 13 (Permission denied)
W/RenderThread: type = 1400 audit (0.0: 380): avc: denied {write} for name = "property_service" dev = "tmpfs" ino = 7423 scontext = u: r: untrusted_app: s0: c137, c256, c512, c768 tcontext = u: object_r: property_socket: s0 tclass = sock_file permissive = 0
D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
W/tentapplicatio: Accessing hidden method Landroid/view/View;->computeFitSystemWindows (Landroid/graphics/Rect;Landroid/graphics/Rect;) Z (greylist, reflection, allowed)
W/tentapplicatio: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows () V (greylist, reflection, allowed)
D/HostConnection: HostConnection :: get () New Host Connection established 0xd1279be0, tid 22776
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without ...
D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
D/EGL_emulation: eglCreateContext: 0xdc881860: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xdc881860: ver 2 0 (tinfo 0xdc8c7010)
W/Gralloc3: mapper 3.x is not supported
D/HostConnection: createUnique: call
D/HostConnection: HostConnection :: get () New Host Connection established 0xd127b710, tid 22776
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2
D/eglCodecCommon: allocate: Ask for block of size 0x1000
allocate: ioctl allocate returned offset 0x3ff00a000 size 0x2000
D/EGL_emulation: eglMakeCurrent: 0xdc881860: ver 2 0 (tinfo 0xdc8c7010)
D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
Corresponding source code
package com.example.intentapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.second);
        setTitle ("second Activity");
        final Intent inIntent = getIntent ();
        final double hapValue = inIntent.getDoubleExtra ("Num1", 0)
               /inIntent.getDoubleExtra ("Num2", 1);
        Button buttonBack = (Button) findViewById (R.id.buttonBack);
        buttonBack.setOnClickListener (new View.OnClickListener () {public void onClick (View view) {
                Intent outIntent = new Intent (getApplicationContext (),
                        MainActivity.class);
                outIntent.putExtra ("Hap", String.format ("% .2f", hapValue));
                setResult (RESULT_OK, outIntent);
                finish ();
            }
        });
    }
}
source code:2
package com.example.intentapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        setTitle ("Main Activity");
        Button button = (Button) findViewById (R.id.button);
        button.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View view) {
                EditText editText1 = (EditText) findViewById (R.id.editText1);
                EditText editText2 = (EditText) findViewById (R.id.editText2);
                Intent intent = new Intent (getApplicationContext (), SecondActivity.class);
                intent.putExtra ("Num1", Integer.parseInt (editText1.getText (). ToString ()));
                intent.putExtra ("Num2", Integer.parseInt (editText2.getText (). ToString ()));
                startActivityForResult (intent, 0);
            }
        });
    }
    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        super.onActivityResult (requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            int hap = data.getIntExtra ("Hap", 0);
            Toast.makeText (getApplicationContext (), "Result" + hap, Toast.LENGTH_SHORT) .show ();
        }
    }
}
Corresponding source code xml
<? xml version = "1.0" encoding = "utf-8"?><LinearLayout
    xmlns: android = "http://schemas.android.com/apk/res/android"android: layout_width = "match_parent"
    android: layout_height = "match_parent"
    android: orientation = "vertical"><EditText
        android: id = "@ + id/editText1"
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content"
        android: textSize = "30dp" /><EditText
        android: id = "@ + id/editText2"
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content"
        android: textSize = "30dp" /><Button
        android: id = "@ + id/button"
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content"
        android: text = "division"
        android: textSize = "30dp" /></LinearLayout>
Corresponding source code xml
<? xml version = "1.0" encoding = "utf-8"?><LinearLayout
    xmlns: android = "http://schemas.android.com/apk/res/android"
    android: layout_width = "match_parent"
    android: layout_height = "match_parent"
    android: orientation = "vertical"><Button
        android: id = "@ + id/buttonBack"
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content"
        android: textSize = "30dp"
        android: text = "Comeback" /></LinearLayout>
What I tried

final double hapValue = inIntent.getDoubleExtra ("Num1", 0)
/ inIntent.getDoubleExtra ("Num2", 1);

outIntent.putExtra ("Hap", String.format ("% .2f", hapValue));

I changed the result displayed when switching from int to double and outputting to "% .2f", but an error occurred.
Even if I run this program, the result is only 0.0%.
What I want is to display the answer as 0.52 when I do 12/23.
Please give me a solution.

Supplementary information (FW/tool version, etc.)

CPU AMD Ryzen ™ 7 3700X
Motherboard B450M 박격포
Memory 삼성 32 기가
Graphic card 2070s
Power 마이 크로 닉스 700W
SSD 삼성 전자 970 EVO PLUS NVMe M.2 2280

This computer was bought in Korea and is all Korean products. Except for the CPU.

  • Answer # 1

    Intent (IntentThere seems to be some confusion in handling types when exchanging values ​​via).MainActivityThenEditTextSince the value is taken out with, in the intentStringKeep the value in typeSecondActivityPass toDoubleHandling with a moldSecondActivityLet's organize it so that we can do it within. The display isToastWithMainActivitySince it is done in, you can process it in the format you want to display there.

    Main Activity side

    // MainActivity
    package com.example.intentapplication;
    import androidx.appcompat.app.AppCompatActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate (Bundle savedInstanceState) {
            super.onCreate (savedInstanceState);
            setContentView (R.layout.activity_main);
            setTitle ("Main Activity");
            Button button = (Button) findViewById (R.id.button);
            button.setOnClickListener (new View.OnClickListener () {
                @Override
                public void onClick (View view) {
                    EditText editText1 = (EditText) findViewById (R.id.editText1);
                    EditText editText2 = (EditText) findViewById (R.id.editText2);
                    Intent intent = new Intent (getApplicationContext (), SecondActivity.class);
                   /* Old code
                    intent.putExtra ("Num1", Integer.parseInt (editText1.getText (). ToString ()));
                    intent.putExtra ("Num2", Integer.parseInt (editText2.getText (). ToString ()));
                    * /
                    // Pass the value of getText (). ToString () of EditText as it is
                    intent.putExtra ("Num1", editText1.getText (). ToString ());
                    intent.putExtra ("Num2", editText2.getText (). ToString ());
                    startActivityForResult (intent, 0);
                }
            });
        }
        @Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {
            super.onActivityResult (requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
               /* Old code
                int hap = data.getIntExtra ("Hap", 0);
                Toast.makeText (getApplicationContext (), "Result" + hap, Toast.LENGTH_SHORT) .show ();
                * /
                // Get the value of Double obediently.
                Double hap = data.getDoubleExtra ("Hap", 0);
                // Process to the format you want to display here.
                String s = String.format ("% .2f", hap);
                Toast.makeText (getApplicationContext (), "Result:" + s, Toast.LENGTH_SHORT) .show ();
            }
        }
    }

    Second Activity side

    // SecondActivity
    package com.example.intentapplication;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import androidx.appcompat.app.AppCompatActivity;
    public class SecondActivity extends AppCompatActivity {
        // Make it a field.
        private double hapValue = 0.0;
        @Override
        protected void onCreate (Bundle savedInstanceState) {
            super.onCreate (savedInstanceState);
            setContentView (R.layout.second);
            setTitle ("second Activity");
           /* Old code
            // There is no point in declaring it as final here.
            final Intent inIntent = getIntent ();
            final double hapValue = inIntent.getDoubleExtra ("Num1", 0)/ inIntent.getDoubleExtra ("Num2", 1);
             * /
            Intent inIntent = getIntent ();
            if (inIntent! = null) {
                try {
                    String s;
                    // Get the Intent value from MainActivity as a String.
                    s = inIntent.getStringExtra ("Num1");
                    Double num1 = Double.parseDouble (s);
                    s = inIntent.getStringExtra ("Num2");
                    Double num2 = Double.parseDouble (s);
                    hapValue = num1/num2;
                    Log.d ("SecondActivity:",,
                            String.format ("num1 =% lf, num2 =% lf, hapValue =% lf", num1, num2, hapValue));
                } catch (Exception ex) {
                    // Handle any exceptions that can occur with Double.parseDouble ().
                    // In this example, it is easily caught by Exception.
                    Log.e ("SecondActivity:", ex.getMessage ());
                }
            }
            Button buttonBack = (Button) findViewById (R.id.buttonBack);
            buttonBack.setOnClickListener (new View.OnClickListener () {
                public void onClick (View view) {
                    Intent outIntent = new Intent (getApplicationContext (),
                            MainActivity.class);
                   /* Old code
                    outIntent.putExtra ("Hap", String.format ("% .2f", hapValue));
                    * /
                    // Since it is already the value of Double, pass it as it is
                    outIntent.putExtra ("Hap", hapValue);
                    setResult (RESULT_OK, outIntent);
                    finish ();
                }
            });
        }
    }