2013-07-09 2 views
0

PhoneListener 서비스 클래스에서 대화 상자를 팝업하려고했습니다. DialogBox Activity Class가 첨부되어 있습니다. 통화 상태가 변경되면 대화 상자가 나타납니다. 정적에서 캐스팅을 시도했지만 외관상 정적을 전혀 이해하지 못합니다. 나는 결코 AlerDialg.Builder를위한 활동이나 컨텍스트를 얻는 것처럼 보이지 않습니다. 여기서비스 클래스에서 활동으로 캐스팅 할 때 널 포인터 예외가 발생했습니다.

DialogBox.onCreateDialog2(1); 

대화 상자 코드 : 여기에 PHoneListener 클래스 내 전화가

public abstract class DialogBox extends Activity { 

static abstract interface DialogBoxPopUp { 
    void onCreateDialog(int id); 

    void onCreateDialog2(int id); 
} 

    Dialog dialog = null; 
    int DIALOG_X = 1; 
    int DIALOG_Y = 2; 
    int DIALOG_Z = 3; 

    private static Activity activity = null; 
    private static final String LOGTAG = "DialogBoxPopUp"; 

    AlertDialog alertDialog;   

    public Dialog onCreateDialog(int id) { 

     switch(id) { 
     case 1: 
      // do the work to define the X Dialog 

      AlertDialog.Builder builder=new AlertDialog.Builder(activity.getParent()); 
      PMLog.d(LOGTAG, "Got to PopUp, have an activity?"); 

       builder 
        .setTitle("Privus Mobile") 
        .setMessage("Lookup this number?") 
        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() 
        { 
         public void onClick(DialogInterface dialog, int which) 
         { 
          onYes(); 
         } 
        }) 
        .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() 
        { 
         public void onClick(DialogInterface dialog, int which) 
         { 
          onNo(); 
         } 
        }) 
        .setOnCancelListener(new DialogInterface.OnCancelListener() 
        { 
         public void onCancel(DialogInterface dialog) 
         { 
          onNo(); 
         } 
        }) 

        .show(); 
       PMLog.d(LOGTAG, "Got to show");   

      break; 

     default: 
      dialog = null; 
     } 
     return dialog; 
    } 


    public static void onYes() { 

     PrivusPhoneStateListener.lookupCallerId(); 
    } 

    public static void onNo() { 

     return; 
    } 


    public static Dialog onCreateDialog2(int id) { 

      ((DialogBox) activity.getApplicationContext()).onCreateDialog(id); 

     return null; 
    }  

}

나는에 NullPointerException이 얻을 ((대화 상자) activity.getApplicationContext()) .onCreateDialog (id);

ID는 전달되지만 null 활동이 발생합니다. 네, 개발 코드에 대해 잘 아는 사람이 아니므로 확실한 것이 빠졌습니다. 어떤 도움이라도 대단히 감사하겠습니다.

답변

1

먼저 정적으로 선언 된 항목에는 개체의 특정 인스턴스에 대한 정보가 없습니다. 그것은 객체의 유형이 아니므로 당신은 그것을 "캐스팅"할 수 없습니다. 정적 메소드에서 활동 인스턴스의 내용에 액세스해야하는 경우 인스턴스를 메소드에 전달하십시오.

둘째 : 귀하의 정적 인터페이스 정의가이 클래스에서 사용되지 않고 제거 될 수 있습니다. 이 클래스가 실제로 해당 인터페이스를 구현하게하려면 클래스 선언에 public 클래스 DialogBox extends Activity가 DialogBoxPopUp를 구현하도록 지정해야합니다.

셋째 : 클래스 (DialogBox)가 액티비티 객체를 확장하므로, 일반적으로 컨텍스트를 얻는 곳입니다.

넷째 :이 클래스는 추상으로 선언하면 안됩니다.

"activity"변수를 삭제하십시오.이 변수를 null로 초기화하고 다른 어떤 것으로도 초기화하지 않으므로 컨텍스트가 생기지 않습니다.

하지만 내가 원하는 것 : 대화 상자를 만드는 데 도움이되는 클래스입니다. 그런 경우에는, 당신은 방법이 정적 만들 수 있습니다,하지만 당신은 정적 메소드에 유효한 컨텍스트를 통과해야합니다 (내가 실행하거나 컴파일하지 않은, 그래서 의사로 취급) : 다음

public class MyDialogBox{ 
    private MyDialogBox(){} //private constructor so this class can't be instantiated 
    public static void ShowDialog(Context c, OnClickListener onYesClick, 
            OnClickListener onNoClick, OnCancelListener onCancel){ 
     AlertDialog.Builder builder=new AlertDialog.Builder(c); 
     builder 
      .setTitle("Privus Mobile") 
      .setMessage("Lookup this number?") 
      .setPositiveButton(R.string.yes, onYesClick) 
      .setNegativeButton(R.string.no, onNoClick) 
      .setOnCancelListener(onCancel) 
      .show(); 
    } 
} 

, 위의 방법을 호출하는 활동에서 :

public class MyActivity extends Activity { 

    //normal implementation code 

    public void SomethingHappenedShowMyDialog(){ 
     MyDialogBox.ShowDialog(
      this, //"this" refers to this activity, and activity extends a context 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        MyActivity.this.onYes(); //call the defined method 
       } 
      }, 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        //or just define it here 
        Toast.makeText(MyActivity.this, "No Clicked", Toast.LENGTH_SHORT).show(); 
       } 
      }, 
      new DialogInterface.OnCancelListener(){ 
       public void onCancel(DialogInterface dialog){ 
        //do something 
       } 
      }); 
    } 

    public void onYes(){ 
     //do something 
    } 
} 
1

활동은 널 (null)로 초기화되며 절대로 값이 지정되지 않습니다. 이것이 NullPointerException의 출처입니다.

관련 문제