2017-05-12 1 views
1

간단한 사용자 정의 대화 상자 클래스를 만들었습니다.getOwnerActivity() 메서드를 호출하면 항상 null이 반환됩니다.

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
        Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345")); 
      startActivity(intent); 

하지만 난 항상 getOwnerActivity()에서 null 얻을 Intent로 변경 호출 할 때마다 문제는 - 제대로 메소드를 호출하는 방법을 : 더 코드에서 나는 Intent 새로운 실행하려면?

public class AddToQueueDialog extends Dialog implements View.OnClickListener { 

    Activity mActivity; 
    private final String android_id = Settings.Secure.getString(getContext().getContentResolver(), 
      Settings.Secure.ANDROID_ID); 

    public Activity getmActivity() { 
     return mActivity; 
    } 

    public void setmActivity(Activity mActivity) { 
     this.mActivity = mActivity; 
    } 

    public AddToQueueDialog(Context context, WashLocation washLocation) { 
     super(context); 
     setWashLocation(washLocation); 
     setmActivity(getOwnerActivity()); 
    } 

답변

1

을 시도하고 반환하는 활동은 어디서든 호출되지 않습니다 setOwnerActivity (활동 활동)에 설정되어 있습니다. 당신이 getOwnerActivity()가 NULL이 아닌 다른 값을 반환하려는 경우, 당신은

public AddToQueueDialog(Context context, WashLocation washLocation) { 
     super(context); 
     if (context instanceof Activity) { 
      setOwnerActivity((Activity) context); 
     } 
     setWashLocation(washLocation); 
     setmActivity(getOwnerActivity()); 
    } 
+0

작동하는 유일한 해결책 :) – bielas

+0

감사합니다 @bielas happy coading –

0

생성자에서 소유자를 얻을하려고하면 당신은

에서 OnCreate

에 getOwnerActivity()를 호출하지 못할, 안드로이드는 아직 중독되지 않은, 그래서 당신은 아직 소유자가 없습니다.

는 소스 코드를 확인할 경우이 대신

public void onAttachedToWindow() { 
    super.onAttachedToWindow(); 
    // getOwnerActivity() should be defined here if called via showDialog(), so do the related init here 
    Activity owner = getOwnerActivity(); 
    if (owner != null) { 
     // owner activity defined here 
    } 
} 
0

context을 다음과 같이 생성자를 변경해야 그래서 소유 Activity입니다. 생성자는 context으로 호출됩니다. 이것은 Activity을 소유하고 있습니다.

관련 문제