(Android)显式意图错误

时间:2017-07-11 09:56:21

标签: android android-intent

public class MainActivity extends AppCompatActivity {

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

    Button button1 = new Button(this);
    button1 = (Button) findViewById(R.id.click_me);


    button1.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent nextActivity = new Intent(this, NextActivity.class);
                    startActivity(nextActivity);
                }
            } 
    );

}

嗨,大家好,我在声明/创建一个新的意图实例时遇到错误,

Intent nextActivity = new Intent(this, NextActivity.class);

它说无法解析构造函数Intent .... 有什么问题?

很抱歉这篇长篇文章,希望你们能尽快告诉我。 谢谢!

1 个答案:

答案 0 :(得分:4)

试试这个

button1.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent nextActivity = new Intent(MainActivity.this, NextActivity.class);
                    startActivity(nextActivity);
                }
            } 
    );

并确保您必须在NextActivity.class

中声明AndroidManifest.xml