如何从静态方法执行非静态方法

时间:2014-09-04 03:31:32

标签: java android static-methods execution

我是android / java开发的新手,所以我遇到了一些问题,这是其中之一。 我想在getTickets中使用recevieResults中返回的结果。我试图让getTickets静态但它有

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);

因此静态方法不允许使用getApplicationContext。

public static void receiveResults(String result2) {

usersXML = result2; 


}

public void getTickets() {

//this method users usersXML from above and needs to execute after receiving the results

}

1 个答案:

答案 0 :(得分:0)

如果您有课程:

public class ViewTicket extends Activity {
    public static <result> recieveResults(Context context, String result2) {
        ViewTicket ticket = (ViewTicket) context;

        return <result>
    }

    public void getTickets(<result> result) {
        // user <result> as you wish.
        SharedPreference pref = this.getSharedPreferences("MyPref", 0);
    }


}

调用静态方法并将对象传递给它。

ViewTicket myTicket = new ViewTicket();
<result> = ViewTicket.recieveResults(myTicket, "<someString>");

myTicket.getTickets(<result>);