bnnoor
bnnoor

Reputation: 696

start new activity in external java class

i have two activity with popup box . i want when user click in menu 1 in popup menu avtivity2 start . i use below code but not work . how i can lunch with this code on external java class

function.java class

public class function{
    Context mContext;
    public Activity activity;


    public function(Context context,Activity _act){
        this.mContext = context;
        this.activity = _act;
    }

    public void modal(View li) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
        final View layout = inflater.inflate(R.layout.popup_layout, null, false);
        final PopupWindow pw = new PopupWindow(layout, 400,650, true);
        pw.showAtLocation(li, Gravity.CENTER, 0, 0);

        //Get font

        Typeface koodakfont = Typeface.createFromAsset(mContext.getAssets(),"font/Yekan.ttf");

        TextView menu1 = (TextView) layout.findViewById(R.id.modal_menu1);
        TextView menu2 = (TextView) layout.findViewById(R.id.modal_menu2);
        TextView menu3 = (TextView) layout.findViewById(R.id.modal_menu3);
        TextView search = (TextView) layout.findViewById(R.id.search);


        // Set font
        menu1.setTypeface(koodakfont);
        menu2.setTypeface(koodakfont);
        menu3.setTypeface(koodakfont);
        search.setTypeface(koodakfont);


        menu3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pw.dismiss();
            }
        });
        menu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intt = new Intent(MainActivity2Activity.class);
                mContext.startActivity(intt);
            }
        });

    }
}

MainActivity.java activity class

final function func = new function(getApplicationContext(),this);

        TextView mytext = (TextView) findViewById(R.id.modaling);
        mytext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                func.modal(findViewById(R.id.main2));
            }
        });
    }

popup start but when click on menu 1 app error and close .

Upvotes: 0

Views: 87

Answers (1)

Héctor
Héctor

Reputation: 26044

Change your listener to this:

menu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intt = new Intent(activity, MainActivity2Activity.class);
                activity.startActivity(intt);
            }
        });

Actually, you don't need the field mContext in function class, since the activity itself is the context.

Upvotes: 2

Related Questions