无法解析Android中的符号setOnClickListener [英] cannot resolve symbol setOnClickListener in android

查看:219
本文介绍了无法解析Android中的符号setOnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此程序时,它反复说无法解析符号setOnClickListener".任何人都可以帮助我解决此问题.

When i run this program, it repeatedly saying that "cannot resolve symbol setOnClickListener".anyone please help me to solve this problem.

import android.support.v7.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the content of the activity to use the activity_main.xml layout file
    setContentView(R.layout.activity_main);
}
// Find the View that shows the numbers category
TextView numbers = (TextView) findViewById(R.id.numbers);

 // Set a click listener on that View
numbers.setOnClickListener(new View.OnClickListener() {
    // The code in this method will be executed when the numbers View is clicked on.
    @Override
    public void onClick(View view) {
        Intent numbersIntent = new Intent(MainActivity.this, Numbers.class);
        startActivity(numbersIntent);
    }
}

}

推荐答案

您的代码应位于 onCreate 方法中,请尝试:

Your code should be inside the onCreate method, try :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

    // Find the View that shows the numbers category
    TextView numbers = (TextView) findViewById(R.id.numbers);

        // Set a click listener on that View
        numbers.setOnClickListener(new View.OnClickListener() {
        // The code in this method will be executed when the numbers View is clicked on.

        @Override
        public void onClick(View view) {
            Intent numbersIntent = new Intent(MainActivity.this, Numbers.class);
            startActivity(numbersIntent);
        }
    }
}

详细了解活动及其生命周期:活动生命周期

Read more about Activities and their lifecycle : Activity Lifecycle

这篇关于无法解析Android中的符号setOnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆