-2

我正在尝试为 Eclipse 中的图像按钮设置 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;
     }
} 
4

3 回答 3

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;
}
于 2013-11-03T17:23:56.907 回答
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); 
        }
    });
}
于 2013-11-03T17:24:50.290 回答
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!!!
}
于 2013-11-03T17:25:43.397 回答