我的活动checkLoginTask
中有一个子类LoginActivity
(此活动用于用户登录)。从扩展AsynTask的类的onPostExecute()中调用此子类。
我想破坏活动LoginActivty
如果theLoginOk == "ok"
并开始活动MainActivity
。我使用了finish()
,但出现了错误Non-Static method "finish()" cannot be referenced from a static context
我尝试了final Activity activity = this;
,但是没有用。
这是我的Avtivity LoingPage
中的方法
public static void checkLoginTrue(JSONObject jsonObject, Context context){
if(jsonObject != null) {
Intent intent = new Intent(context, MainActivity.class);
try {
JSONObject student = jsonObject.getJSONObject("status");
String theId = student.getString("id");
String theLoginOk = student.getString("login");
Log.i("JSON login", theLoginOk);
if (theLoginOk.equals("ok")) {
intent.putExtra("id", theId);
intent.putExtra("login", theLoginOk);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} else {
// something
}
} catch (JSONException e) {
Log.w("error", e.getMessage());
}
}
}
我该如何解决?
最佳答案
这里不需要静态方法。如果要练习在活动类中调用静态方法,请在该类中创建onr Util类,以创建静态方法并从活动覆盖方法中调用。
喜欢
public class Utill
{
public static void checkLoginTrue(JSONObject jsonObject, Context context, Class<? extends Activity> myClass){
if(jsonObject != null) {
Intent intent = new Intent(context, myClass);
try {
JSONObject student = jsonObject.getJSONObject("status");
String theId = student.getString("id");
String theLoginOk = student.getString("login");
Log.i("JSON login", theLoginOk);
if (theLoginOk.equals("ok")) {
intent.putExtra("id", theId);
intent.putExtra("login", theLoginOk);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} else {
// something
}
} catch (JSONException e) {
Log.w("error", e.getMessage());
}
}
}
}
然后打电话
Utils.checkLoginTrue(jsonObject, this, MainActivity.class);
在您的活动中,任何覆盖非静态和/或静态方法。