Android SecondActivity.class文件未链接到以红色显示的意图对象

Android SecondActivity.class文件未链接到以红色显示的意图对象,android,android-intent,Android,Android Intent,单击按钮时,应转到第二个活动,其中使用textView打印hello world只需帮助解决错误,而不是使用句点,您需要使用逗号 package com.example.admin.assignment2; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import androi

单击按钮时,应转到第二个活动,其中使用textView打印hello world只需帮助解决错误,而不是使用句点,您需要使用逗号

package com.example.admin.assignment2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button secbtn, google;

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

        secbtn = (Button) findViewById(R.id.secondbutton);
        google = (Button) findViewById(R.id.firstbutton);

        secbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent newIntent = new Intent(getApplicationContext().SecondActivity.class);//not taking the SecondActivity.class showing in Red color
                startActivity(newIntent);
            }
        });
    }
}
当您想要启动新活动时,Intent需要两个参数。你用的是圆点。而不是逗号

而不是:

改为:


请查看官方教程以了解此信息。

在意图中,替换。到,在上下文和活动类之间

    Intent newIntent = new Intent(getApplicationContext(), SecondActivity.class);

而不是。您需要使用,因此更改声明意图的行

Intent newIntent = new Intent(getApplicationContext(), SecondActivity.class);

Intent是用于启动活动的类。它有一些默认构造函数。其中之一就是

Intent newIntent=new Intent(getApplicationContext(),SecondActivity.class);


请尝试MainActivity而不是getApplicationContext

MainActivity.this指当前活动上下文中的活动,其中getApplicationContext指应用程序类

试一试

Intent newIntent=newintentmainactivity.this,SecondActivity.class; startActivitynewIntent

Intent newIntent = new Intent(getApplicationContext(), SecondActivity.class);
Intent newIntent=new Intent(getApplicationContext(),SecondActivity.class);
 Intent(Context packageContext, Class<?> class)
 //Create an intent for a specific component.
Intent newIntent = new Intent(getApplicationContext(),SecondActivity.class);
startActivity(newIntent);
Intent newIntent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(newIntent);