设置onclicklistener时出错}

时间:2013-11-03 17:21:47

标签: java android eclipse

我正在尝试为eclipse中的imagebutton设置onclicklistener。点击后,该应用应该会转到默认的联系人应用。这是我的代码,但我收到了“}”括号的错误,我似乎无法弄清楚问题是什么。有人可以帮忙吗?

public class First extends Activity {

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

    private void addButtonListener() {
        // TODO Auto-generated method stub

        //finding your image button
        ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton2);

        btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

               Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
               startActivityForResult(intent, 1); 

            }); //!!!!THE ERROR APPEARS UNDER THE } BRACKET ON THIS LINE!!!
    }

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

3 个答案:

答案 0 :(得分:1)

这是一个简单的语法错误。您错放了最后括号。此外,您已在方法内声明了一个方法。下面的代码块应该在方法之外。

正确应该是:

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

private void addButtonListener() {
    // TODO Auto-generated method stub

    //finding your image button
    ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton2);

    btn1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

           Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
           startActivityForResult(intent, 1); 

        } // `);` moved from here to the line below
    });
} // This little thingy was waaaaaay too far down. This is the end for one method

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

答案 1 :(得分:0)

试试这个:

private void addButtonListener() {
// TODO Auto-generated method stub

    //finding your image button
    ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton2);

    btn1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

           Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
           startActivityForResult(intent, 1); 
        }
    });
}

答案 2 :(得分:0)

改变这个:

private void addButtonListener() {
// TODO Auto-generated method stub

//finding your image button
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton2);

btn1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

       Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
       startActivityForResult(intent, 1); 

        }); //!!!!THE ERROR APPEARS UNDER THE } BRACKET ON THIS LINE!!!
}

对此:

private void addButtonListener() {
    // TODO Auto-generated method stub

    //finding your image button
    ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton2);

    btn1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

           Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
           startActivityForResult(intent, 1); 
        }
    }); //!!!!THE ERROR APPEARS UNDER THE } BRACKET ON THIS LINE!!!
}