-1

This is my activity in which I have code for Dialog... What I want to do is that when a user press button a dialog must appear to ask that if the user want to save that link in database or not.

public class Add_ink extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.websearch);

    Button btn = (Button)  findViewById(R.id.add_link);
    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
               showDialog(0);           
        }
    });
    }
    @Override
    protected Dialog onCreateDialog(int id){
         switch (id){
         case 0:
        return new AlertDialog.Builder(this)
         .setIcon(R.drawable.help_ov)
         .setTitle("This is a dialog with simple text")
         .setPositiveButton("OK",new DialogInterface.OnClickListener() {


            public void onClick(DialogInterface dialog, int whichButton) {
                Toast.makeText(getBaseContext(), "OK Clicked", Toast.LENGTH_SHORT).show();              
            }
        })
        .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {


            public void onClick(DialogInterface dialog, int whichButton) {
                Toast.makeText(getBaseContext(), "Cancel Clicked", Toast.LENGTH_SHORT).show();

            }
        })


        .create(); 
         }
        return null;


    }   
}
aymeric
  • 3,877
  • 2
  • 28
  • 42
  • Add `logcat` to show error. We'll help you figure it out problem using `logcat`. – Trung Nguyen Sep 16 '12 at 16:18
  • This is My logCat .....Could not find a method onClick(View) in the activity class comsats.fyp.activity.WebSearchActivity for onClick handler on view class android.widget.Button with id 'add_link' – Android Developer Sep 16 '12 at 16:48

1 Answers1

0
Hi The Solutions is 


/*Code*/

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.websearch);

Button btn = (Button)  findViewById(R.id.add_link);
btn.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
           showDialog();           
    }
});

public void showDialog()
{
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Do you really want to finish ?? You haven't selected any security settings option to activate your app.");  

    builder.setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(
                                    @SuppressWarnings("unused") final DialogInterface dialog,
                                    @SuppressWarnings("unused") final int id) {

                     Toast.makeText(getBaseContext(), "OK Clicked", Toast.LENGTH_SHORT).show();         
dialog.cancel();        
                            }
                        })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog,
                            @SuppressWarnings("unused") final int id) {

Toast.makeText(getBaseContext(), "Cancel Clicked", Toast.LENGTH_SHORT).show();
                        dialog.cancel();

                    }
                });
        final AlertDialog alert = builder.create();
        alert.show();


}
Rishabh Agrawal
  • 861
  • 2
  • 15
  • 25
  • Adding information how to solve his problem is appreciated. Don't turn SO into code provider place. – Trung Nguyen Sep 16 '12 at 16:17
  • thanks Yul for your suggestion.but his call showDialog(0); in his code & there is not as such meethod. – Rishabh Agrawal Sep 16 '12 at 16:20
  • Rishabh.CreatioSoft there is still error of force close .. And there should be Prenthesis after public void onClick(View v) { showDialog(); } }); – Android Developer Sep 16 '12 at 16:34
  • Kindly recheck your button id in your layout & activity.If it is same delete bin & gen file and again run your project. – Rishabh Agrawal Sep 16 '12 at 17:02
  • Yes i have checked that all is fine .. Getting this error in logcat Could not find a method onClick(View) in the activity class comsats.fyp.activity.WebSearchActivity for onClick handler on view class android.widget.Button with id 'add_link' and when i add simply onClick method in that file then no error but nothing in response as Dilog .. – Android Developer Sep 16 '12 at 17:13
  • Remove this import comsats.fyp.R; from your activity class & then clean your project.Its working fine.do not import .R refrence in same package. – Rishabh Agrawal Sep 16 '12 at 17:27