导航到第二个屏幕时,我的代码无法识别我的班级

时间:2018-07-16 13:57:18

标签: android

我真的被困在这里。我试图使用按钮导航到第二个活动,但是每当我尝试将类的名称解析为Intent方法时,Android Studio都会引发错误。

Intent方法toWeightsScreen中,它不允许我在类中进行解析。

有人可以告诉我我要弄什么。android studio snapshot

package leith.comstephen.facebook.httpswww.fitnessapp5;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button navToWeightsScreen = (Button)findViewById(R.id.Firstweights);
        navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toWeightsScreen = new Intent(this, cut.class)
                startActivity(toWeightsScreen);
            }
        });
    }


}

2 个答案:

答案 0 :(得分:0)

navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toWeightsScreen = new Intent(getActivity(), cut.class);
                startActivity(toWeightsScreen);
            }
        });

答案 1 :(得分:0)

意图声明中的this引用按钮的setOnClickListener。 您应指定实际活动。参见下面的代码

navToWeightsScreen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    // Changed cut.class to Cut.class to follow coding conventions
                    Intent toWeightsScreen = new Intent(MainActivity.this, Cut.class);
                    startActivity(toWeightsScreen);
                }
            });