OnClickListener在Android的工作室 [英] OnClickListener in Android Studio

查看:299
本文介绍了OnClickListener在Android的工作室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发和应用程序中使用全新的Andr​​oid工作室,但我一直对我的OnClickListeners接收沾边。主要是它告诉我,它不能解析符号setOnClickListener,它也不能解析视图V

 包com.sigmachi.derbydays;

进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;

公共类MainActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
    }


    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }

    Button按钮=(按钮)findViewById(R.id.standingsButton);
    button.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            startActivity(新意图(MainActivity.this,StandingsActivity.class));
        }
    });
 

这是code类中的

 信息:编制完成,11错误和0警告4秒
信息:11错误
信息:0警告
/Users/angelo/AndroidStudioProjects/SigmaChiDerbyDaysProject/SigmaChiDerbyDays/src/main/java/com/sigmachi/derbydays/MainActivity.java
    错误:错误:行(28)摇篮:其中,标识和GT;预期
    错误:错误:行(28)摇篮:非法启动类型
    错误:错误:行(28)摇篮:')'预期
    错误:错误:行(28)摇篮:';'预期
    错误:错误:行(28)摇篮:无效的方法声明;所需的返回类型
    错误:错误:行(30)摇篮:非法启动类型
    错误:错误:行(30)摇篮:';'预期
    错误:错误:行(30)摇篮:')'预期
    错误:错误:行(30)摇篮:没个说法
    错误:错误:行(30)摇篮:';'预期
    错误:错误:行(33)摇篮:非法启动类型
 

这些都是我收到这使得完全没有意义的错误。 28号线在开始的时候我做的 button.setOnClickListener

编辑:现在,我收到了强制关闭的时候我preSS按钮

这是类应该打开,裸类唯一的改变是布局打开

 包com.sigmachi.derbydays;

进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;

公共类StandingsActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.standings_layout);
    }


    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }

}
 

解决方案

 按钮按钮=(按钮)findViewById(R.id.standingsButton);
button.setOnClickListener(新View.OnClickListener(){
    公共无效的onClick(视图v){
        startActivity(新意图(MainActivity.this,StandingsActivity.class));
    }
});
 

这code是不以任何方式。如果你想使用它,它必须像的OnCreate方法中的()

 保护无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_main);

Button按钮=(按钮)findViewById(R.id.standingsButton);
        button.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                startActivity(新意图(MainActivity.this,StandingsActivity.class));
            }
        });
        }
 

(遗憾的压痕)

I'm attempting to develop and app using the new Android Studio, but I keep receiving major errors on my OnClickListeners. Mainly it is telling me that it cannot resolve symbol "setOnClickListener" and it also cannot resolve "View v"

package com.sigmachi.derbydays;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    Button button= (Button) findViewById(R.id.standingsButton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this,StandingsActivity.class));
        }
    });

That is the code in the class

Information:Compilation completed with 11 errors and 0 warnings in 4 sec
Information:11 errors
Information:0 warnings
/Users/angelo/AndroidStudioProjects/SigmaChiDerbyDaysProject/SigmaChiDerbyDays/src/main/java/com/sigmachi/derbydays/MainActivity.java
    Error:Error:line (28)Gradle: <identifier> expected
    Error:Error:line (28)Gradle: illegal start of type
    Error:Error:line (28)Gradle: ')' expected
    Error:Error:line (28)Gradle: ';' expected
    Error:Error:line (28)Gradle: invalid method declaration; return type required
    Error:Error:line (30)Gradle: illegal start of type
    Error:Error:line (30)Gradle: ';' expected
    Error:Error:line (30)Gradle: ')' expected
    Error:Error:line (30)Gradle: not a statement
    Error:Error:line (30)Gradle: ';' expected
    Error:Error:line (33)Gradle: illegal start of type

Those are the errors I am receiving which makes absolutely no sense. Line 28 starts at when I do button.setOnClickListener

EDIT: Now I receive a force close when I press the button

This is the class it should open, a bare class with the only change being the layout to open

package com.sigmachi.derbydays;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class StandingsActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

解决方案

Button button= (Button) findViewById(R.id.standingsButton);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this,StandingsActivity.class));
    }
});

This code is not in any method. If you want to use it, it must be within a method like OnCreate()

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

Button button= (Button) findViewById(R.id.standingsButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,StandingsActivity.class));
            }
        });
        }

(sorry for the indentation)

这篇关于OnClickListener在Android的工作室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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