简体   繁体   English

为什么我的OnClickListener无法正常工作? 为什么Eclipse中的此代码片段会给出“期望的ClassHeader”?

[英]Why can't I get my OnClickListener to work? Why would this code fragment in Eclipse give a “ClassHeader expected instead”?

package com.example.helloandroid;

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


//Create an anonymous implementation of OnClickListener
private OnClickListener buttonPress = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
        // setContentView(R.layout.panic);
    }
}



public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button myPanicButton = (Button)findViewById(R.id.PanicButton);
    setContentView(R.layout.main);
    myPanicButton.setOnClickListener(buttonPress);
    }
};

You cannot declare variables outside of your class, as you have done here. 您不能像在此所做的那样在类之外声明变量。 Instead, you should declare it either inside the class or inside a method: 相反,您应该在类内部或方法内部声明它:

public class HelloAndroid extends Activity {
    //Create an anonymous implementation of OnClickListener
    private OnClickListener buttonPress = new OnClickListener() {
        public void onClick(View v) {
          // do something when the button is clicked
            // setContentView(R.layout.panic);
        }
    }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button myPanicButton = (Button)findViewById(R.id.PanicButton);
        setContentView(R.layout.main);
        myPanicButton.setOnClickListener(buttonPress);
    }
};

or 要么

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button myPanicButton = (Button)findViewById(R.id.PanicButton);
        setContentView(R.layout.main);

        //Create an anonymous implementation of OnClickListener
        OnClickListener buttonPress = new OnClickListener() {
            public void onClick(View v) {
              // do something when the button is clicked
                // setContentView(R.layout.panic);
            }
        }
        myPanicButton.setOnClickListener(buttonPress);
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我得到“ <identifier> 预期“对于我的代码,我不明白为什么我的代码不起作用 - I get “<identifier> expected” For my code and i don't understand why my code isn't working 如果我使用多线程,为什么代码片段不能正常工作? - Why code fragment doesn't work correctly if I use multithreading? 为什么我不能从我的活动中获取数据到我的片段? - Why can't I get data from my activity to my fragment? 我的项目中的 Next 和 Previous 按钮没有按预期工作,我的代码有问题,但我不明白为什么 - The Next and Previous buttons in my project are not working as expected, My code is faulty, but I can't understand why 当我运行我的代码(在 Eclipse 中)时,为什么我没有得到任何 output? - Why don't I get any output when I run my code (in Eclipse)? 我不知道为什么“while”循环阻塞了我的 onClickListener - I don't know why the "while" loop is blocking my onClickListener 为什么我的代码有时不能按预期工作? - Why will my code sometimes not work as expected? 为什么我不能将我的 ArrayList 发送到另一个片段? - Why can't i send my ArrayList to another fragment? Javamail-为什么我无法使它正常工作? - Javamail - Why can't I get it to work? OnClickListener不能隐式工作。 为什么? - OnClickListener doesn't work implicitly. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM