微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android GridView运行时异常

如何解决Android GridView运行时异常

我正在尝试将GET响应显示到android studio中的卡片视图中。 我能够在XML布局上显示普通的文本JSON(响应)数据。但是,当我尝试使用Glide从API加载图像并对代码进行一些更改时,它向我显示了一些模拟器错误

我的代码

package demo.com;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DashboardUser extends AppCompatActivity {
    private static final String TAG = "DashboardUser";
    GridView gridview;


    public TextView categoryList;
    InterfaceAPI APIinterface;
    CustomAdapter customAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard_user);
        gridview = findViewById(R.id.mainGrid);

        APIinterface = RetrofitClientInstance.getRetrofitClientInstance().create(InterfaceAPI.class);

        //background thread
        //network call
        Call<List<category>> call = APIinterface.getCategory();

        call.enqueue(new Callback<List<category>>() {
            @Override
            public void onResponse(Call<List<category>> call,Response<List<category>> response) {
                if (response.isSuccessful()) {

                    customAdapter = new CustomAdapter(response.body(),DashboardUser.this);
                    gridview.setAdapter(customAdapter);
                     }
                else {
                    Toast.makeText(getApplicationContext(),"An error occured",Toast.LENGTH_LONG).show();

                        }

            }


            @Override
            public void onFailure(Call<List<category>> call,Throwable t) {
                Toast.makeText(getApplicationContext(),"An error occured"+t.getLocalizedMessage(),Toast.LENGTH_LONG).show();


            }
        });


    }

    public class CustomAdapter extends BaseAdapter {

        public List<category> catList;
        public Context context;

        public CustomAdapter(List<category> catList,Context context) {
            this.catList = catList;
            this.context = context;
        }


    @Override
    public int getCount() {
        return catList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i,View view,ViewGroup viewGroup) {
        View v = LayoutInflater.from(context).inflate(R.layout.activity_dashboard_user,null);

        //find view
        TextView name=v.findViewById(R.id.txtDoctor);
        ImageView img=v.findViewById(R.id.imgDoctor);

        //set data
            name.setText(catList.get(i).getCatName());
            //set image
        Glide.with(context)
                .load(catList.get(i).getCatUrl())
                .into(img);

        return v;
    }

    }

构建成功。但是,当我尝试使用Nexus5运行项目时,出现以下错误

D/HostConnection: HostConnection::get() New Host Connection established 0xea2d2bb0,tid 5890
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_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer 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/EGL_emulation: eglCreateContext: 0xea0e26e0: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xea0e26e0: ver 2 0 (tinfo 0xea42dd10) (first time)
I/Gralloc4: mapper 4.x is not supported
D/HostConnection: createUnique: call
D/HostConnection: HostConnection::get() New Host Connection established 0xea2d2070,tid 5890
D/goldfish-address-space: allocate: Ask for block of size 0x100
    allocate: ioctl allocate returned offset 0x3fd968000 size 0x2000
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_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 
I/Choreographer: Skipped 65 frames!  The application may be doing too much work on its main thread.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: demo.com,PID: 5808
    java.lang.RuntimeException: Unable to start activity ComponentInfo{demo.com/demo.com.DashboardUser}: java.lang.classCastException: android.widget.GridLayout cannot be cast to android.widget.GridView
        at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        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:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.classCastException: android.widget.GridLayout cannot be cast to android.widget.GridView
        at demo.com.DashboardUser.onCreate(DashboardUser.java:38)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        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:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
I/Process: Sending signal. PID: 5808 SIG: 9
---------------------------
Emulator: emulator: ERROR: SDKCtl multi-touch: Invalid packet signature 50545448 for packet type 808465440,size 825110831

10:57 PM    Emulator: emulator: ERROR: SDKCtl multi-touch: Invalid packet signature 50545448 for packet type 808465440,size 825110831

解决方法

您正在尝试将android.widget.GridLayout强制转换为android.widget.GridView

使用GridLayout代替GridView

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。