It's a question from Android Studio

Asked 2 years ago, Updated 2 years ago, 56 views

I wrote it in the code below But if you change the shape, the application dies... The error code is E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da D/HostConnection: createUnique: call D/HostConnection: HostConnection::get() New Host Connection established 0xdc7d23c0, tid 16391 D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_sync_buffer_data GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_3_1 E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1 D/EGL_emulation: eglMakeCurrent: 0xdc785420: ver 3 1 (tinfo 0xdc783370) D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 2 W/ple.sampletoas: Accessing hidden method Landroid/os/Trace;->asyncTraceBegin(JLjava/lang/String;I)V (light greylist, reflection) Accessing hidden method Landroid/os/Trace;->asyncTraceEnd(JLjava/lang/String;I)V (light greylist, reflection) Accessing hidden method Landroid/os/Trace;->traceCounter(JLjava/lang/String;I)V (light greylist, reflection) D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.sampletoast, PID: 16360 java.lang.IllegalStateException: Could not execute method for android:onClick at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:446) at android.view.View.performClick(View.java:6597) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) at android.view.View.performClickInternal(View.java:6574) at android.view.View.access$3100(View.java:778) at android.view.View$PerformClick.run(View.java:25885) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) at android.view.View.performClick(View.java:6597)  at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)  at android.view.View.performClickInternal(View.java:6574)  at android.view.View.access$3100(View.java:778)  at android.view.View$PerformClick.run(View.java:25885)  at android.os.Handler.handleCallback(Handler.java:873)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:6669)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.sampletoast.MainActivity.onButton2Clicked(MainActivity.java:58) at java.lang.reflect.Method.invoke(Native Method)  at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)  at android.view.View.performClick(View.java:6597)  at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)  at android.view.View.performClickInternal(View.java:6574)  at android.view.View.access$3100(View.java:778)  at android.view.View$PerformClick.run(View.java:25885)  at android.os.Handler.handleCallback(Handler.java:873)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:6669)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)  I/Process: Sending signal. PID: 16360 SIG: 9

I'd appreciate your help ㅠ<

MainActivity-- package com.example.sampletoast;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity { EditText editText; EditText editText2; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    editText = findViewById(R.id.editText);
    editText2=findViewById(R.id.editText2);

}

public void onButton1Clicked(View v){
    try{
        Toast toastView = Toast.makeText(this is a "toast message with a new location",
                Toast.LENGTH_LONG);
        int xOffset = Integer.parseInt(editText.getText().toString());
        int yOffset = Integer.parseInt(editText2.getText().toString());
        toastView.setGravity(Gravity.TOP|Gravity.TOP,xOffset,yOffset);

        toastView.show();
    }catch(NumberFormatException e){
        e.printStackTrace();
    }
}


public void onButton2Clicked(View v){
    //Objects XML-defined layouts into memory using LayoutInflater objects
    LayoutInflater inflater = getLayoutInflater();

    //Get the toast_layout_root ID of the toastborder.xml file in the layout folder
    View layout = inflater.inflate(R.layout.toastborder,
            (ViewGroup) findViewById(R.id.toast_layout_root));

    //Get text view object with id text from View object
    TextView text = layout.findViewById(R.id.text);

    Toast toast = new Toast(this);
    text.setText ("Shaped Toast");
    toast.setGravity(Gravity.CENTER, 0, -100);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(layout);
    toast.show();

}

public void onButton3Clicked(View v){
    Snackbar.make(v), Snackbar.LENGTH_LONG).show();
}

}

--toastborder <?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/toast"
    android:textSize="32sp"
    android:padding="20dp"/>

--toast <?xml version="1.0" encoding="utf-8"?>

<stroke
    android:color="#ffffff00"
    android:width="4dp"/>
<solid
    android:color="#ff883300"/>
<padding
    android:bottom="20dp"
    android:left="20dp"
    android:right="20dp"
    android:top="20dp"/>
<corners
    android:radius="15dp"/>

android-studio

2022-09-20 12:33

1 Answers

java.lang.NullPointerException: Attempt to invoke virtual method
 'void android.widget.TextView.setText(java.lang.CharSequence)' 
 on a null object reference at 
com.example.sampletoast.MainActivity.onButton2Clicked(MainActivity.java:58)

From here, an exception occurred because the object setText in the onButton2Clicked method was a null object. If you look at the code,

   TextView text = layout.findViewById(R.id.text);

    Toast toast = new Toast(this);
    text.setText ("Shaped Toast");

In onButton2Clicked, setText is only here, so text is null. The result of layout.findViewById(R.id.text) is null.


2022-09-20 12:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.