2015-01-10 4 views
0

내 액티비티 LoginActivity에 서브 클래스 checkLoginTask이 있습니다 (이 액티비티는 사용자 로그인 용 임). 이 서브 클래스는 AsynTask를 확장하는 클래스의 onPostExecute()에서 호출됩니다.정적 메서드에서 Activity를 어떻게 파기 할 수 있습니까?

LoginActivty 인 경우 theLoginOk == "ok" 일 경우 활동을 종료하고 MainActivity 활동을 시작하고 싶습니다. finish()을 사용했지만 오류가 발생했습니다. Non-Static method "finish()" cannot be referenced from a static context

시도했지만 final Activity activity = this; 시도했지만 작동하지 않습니다.

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

가 어떻게이 문제를 해결할 수있는 내 Avtivity의 방법이다?

+0

전체 활동 코드를 게시 하시겠습니까? – Rohit5k2

+0

이 방법이 정적 인 이유는 무엇입니까? 정적 인 경우 AsyncTask에서 다른 곳으로 호출 할 수 있으므로 상황이 잘못되어 콜백 인터페이스를 사용하여 로그인이 완료되었음을 Activity에 알려야합니다. –

+0

실제로는 지침을 사용하여 상위 컨텍스트로 메시지를 보냅니다. 예를 들어'startActictyForResult'를 사용하고'onReceive' 콜백을 통해 'Activity'를 마칠 수 있습니다. – mrres1

답변

0

여기 정적 메서드가 필요하지 않습니다. 액티비티 클래스에서 정적 메서드를 호출하는 연습을 원한다면 정적 메서드를 만들고 액티비티 재정의 메서드에서 호출하는 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); 

를 호출합니다.

0

전화를 걸려면 실제 활동 개체가 필요하므로 시도 할 때 finish()이 작동하지 않았습니다. 정적 메서드는 객체에서 실행되지 않습니다. 그렇기 때문에 그들은 this을 사용할 수 없습니다.

액티비티 객체 참조를 정적 메서드에 전달한 다음 finish()을 전달해야합니다. 이미 Context context으로 전달한 경우 먼저 활동으로 전송하십시오. 예 :

//if your context is actually activity reference, use line below 
//if not, add another Activity argument to the method 
//if you cant get to the activity to pass it as argument, save it in a static reference somewhere, during ocCreate for example, and you can access it globally 
Activity activity = (Activity) context; 

//correct way to use finish() 
activity.finish(); 
관련 문제