-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 に答える