2013-11-03 71 views
-2

我想爲eclipse中的imagebutton設置onclicklistener。點擊後,應用程序應該導致默認的聯繫人應用程序。這是我有的代碼,但我得到一個「}」括號錯誤,我似乎無法弄清楚什麼是問題。誰能幫忙?設置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

你OPE ned {twicw但只關閉一次。 – LeeNeverGup

回答

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; 
} 
+0

非常感謝!我是新來的,我一直試圖從昨晚開始排除這種情況!......現在只有一件事了......聯繫人列表出現了,但是選中它之後就會回到我的應用程序..所以我想我必須做一些研究,然後研究onActivityResult現在 – user2490199

+0

@ user2490199是的,先做一些自己的研究,然後如果你完全沒有找到解決方案,那麼在這裏發佈一個**新**問題並展示**解決方案你已經嘗試** –

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); 
     } 
    }); 
} 
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!!! 
} 
相關問題