首页 > 解决方案 > 在cardview上添加onclicklistener

问题描述

我对android studio中的java编码很陌生,有人可以帮忙吗?点击工作,但它会回到主要活动。它不去想要的活动。

这是我的主要活动java

package com.example.dashboard;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    public CardView draftCard, cardsCard, todoCard, moneyCard, linkedinCard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        //definingcards
        draftCard = findViewById(R.id.draftCard);
        cardsCard = findViewById(R.id.cardsCard);
        moneyCard = findViewById(R.id.moneyCard);
        todoCard = findViewById(R.id.todoCard);
        linkedinCard = findViewById(R.id.linkedinCard);

        //addingclicklistener
        draftCard.setOnClickListener(this);
        cardsCard.setOnClickListener(this);
        moneyCard.setOnClickListener(this);
        todoCard.setOnClickListener(this);
        linkedinCard.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        Intent i = getIntent();

        switch (v.getId()) {
            case R.id.draftCard:
                new Intent(this, draftcard.class);
                startActivity(i);
                break;
            case R.id.cardsCard:
                new Intent(this, cardscard.class);
                startActivity(i);
                break;
            case R.id.moneyCard:
                new Intent(this, moneycard.class);
                startActivity(i);
                break;
            case R.id.todoCard:
                new Intent(this, todocard.class);
                startActivity(i);
                break;
            case R.id.linkedinCard:
                new Intent(this, linkedincard.class);
                startActivity(i);
                break;
            default:break;
        }

    }
}

真的很感激,因为我几乎不认为它为什么不起作用,因为我认为一切都很好。

这是我的堆栈跟踪

2021-01-27 22:30:34.983 6206-6206/? I/ample.dashboar: Not late-enabling -Xcheck:jni (already on)
2021-01-27 22:30:35.017 6206-6206/? W/ample.dashboar: Unexpected CPU variant for X86 using defaults: x86
2021-01-27 22:30:35.285 6206-6206/com.example.dashboard W/ample.dashboar: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2021-01-27 22:30:35.290 6206-6206/com.example.dashboard W/ample.dashboar: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2021-01-27 22:30:35.407 6206-6206/com.example.dashboard W/ample.dashboar: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking)
2021-01-27 22:30:35.436 6206-6206/com.example.dashboard D/OpenGLRenderer: HWUI GL Pipeline
2021-01-27 22:30:35.499 6206-6250/com.example.dashboard D/HostConnection: HostConnection::get() New Host Connection established 0xe4f1e230, tid 6250
2021-01-27 22:30:35.506 6206-6250/com.example.dashboard D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer 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_2 
2021-01-27 22:30:35.517 6206-6250/com.example.dashboard I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2021-01-27 22:30:35.517 6206-6250/com.example.dashboard I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2021-01-27 22:30:35.517 6206-6250/com.example.dashboard I/OpenGLRenderer: Initialized EGL, version 1.4
2021-01-27 22:30:35.518 6206-6250/com.example.dashboard D/OpenGLRenderer: Swap behavior 1
2021-01-27 22:30:35.520 6206-6250/com.example.dashboard W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2021-01-27 22:30:35.520 6206-6250/com.example.dashboard D/OpenGLRenderer: Swap behavior 0
2021-01-27 22:30:35.576 6206-6250/com.example.dashboard D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2021-01-27 22:30:35.576 6206-6250/com.example.dashboard D/EGL_emulation: eglCreateContext: 0xe4f05120: maj 2 min 0 rcv 2
2021-01-27 22:30:35.643 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:30:35.685 6206-6250/com.example.dashboard D/HostConnection: createUnique: call
2021-01-27 22:30:35.685 6206-6250/com.example.dashboard D/HostConnection: HostConnection::get() New Host Connection established 0xe4f1e4b0, tid 6250
2021-01-27 22:30:35.688 6206-6250/com.example.dashboard D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer 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_2 
2021-01-27 22:30:35.689 6206-6250/com.example.dashboard E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1
2021-01-27 22:30:35.698 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:30:38.339 6206-6206/com.example.dashboard W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@fa80e4f
2021-01-27 22:30:38.555 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:30:38.676 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:30:38.696 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:30:38.758 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:30:39.141 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:30:40.043 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:30:47.748 6206-6206/com.example.dashboard W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@c41b27d
2021-01-27 22:30:47.930 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:31:01.288 6206-6206/com.example.dashboard W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@4ae335d
2021-01-27 22:31:01.460 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:31:07.108 6206-6206/com.example.dashboard W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@5aca43d
2021-01-27 22:31:07.264 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:31:10.552 6206-6206/com.example.dashboard W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@ef5051d
2021-01-27 22:31:10.702 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:31:13.642 6206-6206/com.example.dashboard W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@c3f55fd
2021-01-27 22:31:13.812 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:31:14.911 6206-6206/com.example.dashboard W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@54396dd
2021-01-27 22:31:15.090 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:31:15.326 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:31:15.655 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:31:16.577 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)
2021-01-27 22:31:18.463 6206-6206/com.example.dashboard W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@c23d1b2
2021-01-27 22:31:18.590 6206-6250/com.example.dashboard D/EGL_emulation: eglMakeCurrent: 0xe4f05120: ver 2 0 (tinfo 0xe4f03660)

标签: javaandroidandroid-studio

解决方案


当您调用 getIntent() 时,您将获得用于调用当前活动的意图的引用。

在您的 switch 案例中,您有一个未使用的新 Intent 的实例化。

所以,解决方案是:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.draftCard:
            Intent i = new Intent(this, draftcard.class);
            startActivity(i);
            break;
        case R.id.cardsCard:
            Intent i = new Intent(this, cardscard.class);
            startActivity(i);
            break;
        case R.id.moneyCard:
            Intent i = new Intent(this, moneycard.class);
            startActivity(i);
            break;
        case R.id.todoCard:
            Intent i = new Intent(this, todocard.class);
            startActivity(i);
            break;
        case R.id.linkedinCard:
            Intent i = new Intent(this, linkedincard.class);
            startActivity(i);
            break;
        default:break;
    }

}

推荐阅读