Android 网格视图运行时异常

huangapple go评论69阅读模式
英文:

Android GridView Runtime exception

问题

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);

        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);

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

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

            return v;
        }
    }
}

以上是您提供的代码的翻译部分。如果您有任何进一步的问题或需要帮助,请随时提问。

英文:

I am trying to display GET response into the card view in android studio.
I am able to display the normal textual JSON (response) data on the XML layout. But when I try to use Glide to load the image from API and made a few changes to code then it's showing me some Emulator error.

My code:

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 = &quot;DashboardUser&quot;;
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&lt;List&lt;category&gt;&gt; call = APIinterface.getCategory();
call.enqueue(new Callback&lt;List&lt;category&gt;&gt;() {
@Override
public void onResponse(Call&lt;List&lt;category&gt;&gt; call, Response&lt;List&lt;category&gt;&gt; response) {
if (response.isSuccessful()) {
customAdapter = new CustomAdapter(response.body(), DashboardUser.this);
gridview.setAdapter(customAdapter);
}
else {
Toast.makeText(getApplicationContext(), &quot;An error occured&quot;, Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call&lt;List&lt;category&gt;&gt; call, Throwable t) {
Toast.makeText(getApplicationContext(),&quot;An error occured&quot;+t.getLocalizedMessage(),Toast.LENGTH_LONG).show();
}
});
}
public class CustomAdapter extends BaseAdapter {
public List&lt;category&gt; catList;
public Context context;
public CustomAdapter(List&lt;category&gt; 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;
}
}

Build is successful. But when I try to run the project with Nexus5 it gives below error:

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)&#160;
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)&#160;
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)&#160;
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)&#160;
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)&#160;
at android.os.Handler.dispatchMessage(Handler.java:106)&#160;
at android.os.Looper.loop(Looper.java:223)&#160;
at android.app.ActivityThread.main(ActivityThread.java:7656)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)&#160;
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
10:57 PM	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

答案1

得分: 1

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

请改用 GridLayout,而不是 GridView

英文:

Your are trying to cast android.widget.GridLayout to android.widget.GridView.

Use GridLayout instead of GridView.

huangapple
  • 本文由 发表于 2020年9月12日 01:39:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63851874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定