在外部java类中启动新活动

时间:2015-11-17 07:34:15

标签: android

我有两个弹出框活动。我想在用户点击弹出菜单avtivity2中的菜单1时启动。我使用下面的代码但不工作。我如何在外部java类上使用此代码共进午餐

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活动类

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));
            }
        });
    }

弹出窗口开始但是当点击菜单1应用程序错误并关闭时。

1 个答案:

答案 0 :(得分:2)

将您的听众更改为:

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

实际上,您不需要mContext类中的字段function,因为活动本身就是上下文。