简体   繁体   English

从另一个工作活动复制的onClickListener不工作

[英]onClickListener not working that was copied from another working activity

I have created a new activity, added it to my manifest file and copy and pasted code from another fully functioning activity yet my buttons do not work when I click on them. 我创建了一个新活动,将其添加到清单文件中,并从另一个功能正常的活动中复制并粘贴了代码,但是单击这些按钮后我的按钮不起作用。 Here is my activity: 这是我的活动:

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;    

public class test extends Activity {

private Button btnChangeDate;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.salesticketoilui);
    mainProgram();
    }

public void mainProgram() {
    btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
    btnChangeDate.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        } // end onClick
    }); // end setOnClickListener

    Button buttonExit = (Button)findViewById(R.id.buttonExit);
    buttonExit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
             exitActivity();          
        } // end onClick
    }); // end OnClickListener

    // setup button listener for saving data and exit to main
    Button buttonSaveExit = (Button) findViewById(R.id.buttonSaveExit);
    buttonSaveExit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            saveExit();
        } // end onClick
    }); // end OnClickListener

} // end MainProgram ()

    public void saveExit() {
        // does stuff
    }

    public void exitActivity () {
        // does stuff
    }

} // end class 

any thoughts? 有什么想法吗?

Based on the code you have shown, it doesn't appear that you ever call the method mainProgram so your click listeners will never actually get setup. 根据显示的代码,似乎没有调用过mainProgram方法,因此您的点击侦听器将永远不会进行设置。 Either call mainProgram from onCreate or just put that code directly into onCreate . onCreate调用mainProgram或直接将代码放入onCreate

我相信onClickListeners需要进入您的onCreate方法。

Listen to Scott 听斯科特

and looks like your missing the @Override 看起来像你错过了@Override

new View.OnClickListener() {
    @Override
    public void onClick(View view) {
         exitActivity();          
    } // end onClick
}

Make sure your java settings are to 1.6 to avoid code completion missing this. 确保您的Java设置为1.6,以避免代码完成遗漏此内容。

I had the same problem copying OnClickListener with ImageButtons from a class to another class, and renaming then with bulk copy/paste. 我在将具有ImageButtons的OnClickListener从一个类复制到另一个类时遇到相同的问题,然后使用批量复制/粘贴重命名。 To make it work, I had to create new buttons in my layout and declare the events manually. 为了使其工作,我必须在布局中创建新按钮并手动声明事件。 Weird! 奇怪的!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM