0

i create an activity with fragment; then add an OnClickListener() for a button ;every thing work fine. but when i try to add an OnClickListener() for positivebutton to alertdialog eclipse give error before i can run program it's piece of code that have error and i do not now why :(

        AlertDialog.Builder exitDialog=new AlertDialog.Builder(getActivity());

        exitDialog.setTitle("Alert");
        exitDialog.setMessage("Exit Program");
        exitDialog.setPositiveButton("Yes", new OnClickListener()
        {

            @Override
            public void onClick(DialogInterface arg0, int arg1)
            {
                // TODO Auto-generated method stub
                System.exit(1);
            }
        });
        exitDialog.setNegativeButton("NO", null);
        exitDialog.show();

and it's whole code of my activity

package com.TB.mylistprojct;

import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class ActFooter extends Fragment
{
View            EMyView         =null;
Button          BtnExit         =null;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    InitialUI();
}

@Override
public View onCreateView(LayoutInflater Inflater,ViewGroup Container,Bundle SavedInstanceState)
{

    View MyView=Inflater.inflate(R.layout.actfooter, Container,false);
    EMyView=MyView;
    return MyView;
}

public void InitialUI()
{
    BtnExit=(Button)EMyView.findViewById(R.id.Btn_exit);
    BtnExit.setOnClickListener(BtnExit_OnClick);


}

public OnClickListener BtnExit_OnClick=new OnClickListener()
{

    @Override
    public void onClick(View arg0)
    {
        // TODO Auto-generated method stub
        AlertDialog.Builder exitDialog=new AlertDialog.Builder(getActivity());

        exitDialog.setTitle("Alert");
        exitDialog.setMessage("Exit Program");
        exitDialog.setPositiveButton("Yes", new OnClickListener()
        {

            @Override
            public void onClick(DialogInterface arg0, int arg1)
            {
                System.exit(1);
            }
        });
        exitDialog.setNegativeButton("NO", null);
        exitDialog.show();


    }
};


}

anybody can help about this error

HamidTB
  • 1,381
  • 3
  • 11
  • 15
  • 1
    Move this `InitialUI();` to `onCreateView` – Raghunandan Jan 02 '14 at 13:35
  • 1
    also why do you need `System.exit(1);`. You may want to read http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon – Raghunandan Jan 02 '14 at 13:39
  • @HamidTB combine the Raghunanadan comment and Aashir answer... – Gopal Gopi Jan 02 '14 at 13:39
  • Must sure that your **OnClickListener** imported as a **import android.content.DialogInterface.OnClickListener** – Piyush Jan 02 '14 at 13:47
  • @Piyush Gupta android.content.DialogInterface.OnClickListener conflicts with import android.view.View.OnClickListener:| and you can not import them together – HamidTB Jan 02 '14 at 14:46

2 Answers2

0

Replace new OnClickListener() in your alert dialogs positive button click with: new DialogInterface.OnClickListener()

Aashir
  • 2,621
  • 4
  • 21
  • 37
-1

Please call InitialUI Method in onActivityCreated instead of onCreate Method.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    InitialUI();
}

May be it'll helpful to you. Yeah and replace OnClickListerner with DialogInterface.OnClickListener

Prat
  • 143
  • 3
  • 17