Cardview多项活动

问题描述 投票:-2回答:2

我在Android studio 3.0.1上工作。我在android cardview上工作了一个项目。我在android cardview上看到了一个视频教程。我仔细编写代码。但是当我运行代码时,它看到了一些错误。

enter image description here

这是我的MainActivity.java中的相应代码

private void setSingleEvent(GridLayout mainGrid) {
    //Loop all child item of Main Grid
    for (int i = 0; i < mainGrid.getChildCount(); i++) {
        //You can see , all child item is CardView , so we just cast object to CardView
        CardView cardView = (CardView) mainGrid.getChildAt(i);
        final int finalI = i;
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (finalI == 0) //Teachers Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Teachers.class);
                    startActivity(intent);
                }

                else if (finalI == 1) //Students Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Students.class);
                    startActivity(intent);
                }
                else if (finalI == 2) //Students Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Students.class);
                    startActivity(intent);
                }
                else if (finalI == 3) //Notices Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Notices.class);
                    startActivity(intent);
                }
                else if (finalI == 4) //Results Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Results.class);
                    startActivity(intent);
                }
                else if (finalI == 5) //Phones Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Phones.class);
                    startActivity(intent);
                }
                else if (finalI == 6) //Blood Activities
                {
                    Intent intent = new Intent( packageContext: MainActivity.this, Bloods.class);
                    startActivity(intent);
                }
                else {
                    Toast.makeText(MainActivity.this, "Activvvv", Toast.LENGTH_SHORT).show();
                }


            }
        });
    }
}

Full Source code on here

java android
2个回答
0
投票

删除红色的packageContext。最终的功能应如下所示。

private void setSingleEvent(GridLayout mainGrid) {
    //Loop all child item of Main Grid
    for (int i = 0; i < mainGrid.getChildCount(); i++) {
        //You can see , all child item is CardView , so we just cast object to CardView
        CardView cardView = (CardView) mainGrid.getChildAt(i);
        final int finalI = i;
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (finalI == 0) //Teachers Activities
                {
                    Intent intent = new Intent(MainActivity.this, Teachers.class);
                    startActivity(intent);
                }

                else if (finalI == 1) //Students Activities
                {
                    Intent intent = new Intent(MainActivity.this, Students.class);
                    startActivity(intent);
                }
                else if (finalI == 2) //Students Activities
                {
                    Intent intent = new Intent(MainActivity.this, Students.class);
                    startActivity(intent);
                }
                else if (finalI == 3) //Notices Activities
                {
                    Intent intent = new Intent(MainActivity.this, Notices.class);
                    startActivity(intent);
                }
                else if (finalI == 4) //Results Activities
                {
                    Intent intent = new Intent(MainActivity.this, Results.class);
                    startActivity(intent);
                }
                else if (finalI == 5) //Phones Activities
                {
                    Intent intent = new Intent(MainActivity.this, Phones.class);
                    startActivity(intent);
                }
                else if (finalI == 6) //Blood Activities
                {
                    Intent intent = new Intent(MainActivity.this, Bloods.class);
                    startActivity(intent);
                }
                else {
                    Toast.makeText(MainActivity.this, "Activvvv", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

0
投票

Android Studio抱怨因为以下语句在Java中语法错误:

Intent intent = new Intent( packageContext: MainActivity.this, Notices.class);

Android Studio在方法参数之前显示参数名称(如“packageContext:”),以便为Android提供一些Kotlin外观和感觉。这对于初学者来说可能会让人感到困惑。声明的正确形式是:

Intent intent = new Intent(MainActivity.this, Notices.class);
© www.soinside.com 2019 - 2024. All rights reserved.