2014-10-30 89 views
0

我遇到了一個問題,我想將SetDialogFragment回傳給最初調用它的Fragment將參數從DialogFragment傳遞到片段

我已經嘗試過實現一個接口,但我似乎無法讓它從片段中正常工作。

是否有另一種方法來自DialogFragment >>Fragment?或者我需要實現Activity上的接口,然後從那裏移動它?

這個問題似乎是NullPointerException,我非常肯定這是因爲接口需要在活動級別實現,而不是在碎片上實現。點擊對話框的「正面按鈕」時發生崩潰。

DIALOGFRAGMENT

public class CustomPermissionDialog extends DialogFragment implements 
    OnCheckedChangeListener { 

String _permission; 
View convertView; 
AlertDialog.Builder builder; 
Switch alertDelete; 
Set<String> permSet = new TreeSet<String>(); 

public static interface OnCompleteDialogInterface { 
    public abstract void OnCompleteDialog(Set mPermSet); 
} 

private OnCompleteDialogInterface mInterface; 

public CustomPermissionDialog(Context context, String permissionName) { 

    _permission = permissionName; 
    mInterface = (OnCompleteDialogInterface) getActivity(); 
    // TODO Auto-generated constructor stub 
} 

public Dialog onCreateDialog(Bundle savedInstanceState) { 

    LayoutInflater inflater = getActivity().getLayoutInflater(); 
    builder = new AlertDialog.Builder(getActivity()); 

    if (_permission == "Alerts") { 
     convertView = (View) inflater 
       .inflate(
         getResources().getLayout(
           R.layout.alerts_perm_dialog), null); 
     alertDelete = (Switch) convertView 
       .findViewById(R.id.switchAlertDelete); 
     alertDelete.setOnCheckedChangeListener(this); 

    } 
    if (_permission == "Automation") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.automation_perm_dialog), 
       null); 

    } 
    if (_permission == "Books") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.books_perm_dialog), null); 

    } 

    if (_permission == "Codes") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.codes_perm_dialog), null); 

    } 

    if (_permission == "DBS") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.dbs_perm_dialog), null); 

    } 
    if (_permission == "Feedback") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.feedback_perm_dialog), 
       null); 

    } 

    if (_permission == "Groups") { 
     convertView = (View) inflater 
       .inflate(
         getResources().getLayout(
           R.layout.groups_perm_dialog), null); 

    } 

    if (_permission == "Inventory") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.inventory_perm_dialog), 
       null); 

    } 

    if (_permission == "Jobs") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.jobs_perm_dialog), null); 

    } 

    if (_permission == "Locations") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.locations_perm_dialog), 
       null); 

    } 

    if (_permission == "Logs") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.logs_perm_dialog), null); 

    } 

    if (_permission == "Messages") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.messages_perm_dialog), 
       null); 

    } 

    if (_permission == "Services") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.services_perm_dialog), 
       null); 

    } 
    if (_permission == "Settings") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.settings_perm_dialog), 
       null); 

    } 
    if (_permission == "Templates") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.templates_perm_dialog), 
       null); 

    } 
    if (_permission == "Tools") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.tools_perm_dialog), null); 

    } 
    if (_permission == "Updates") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.updates_perm_dialog), 
       null); 

    } 
    if (_permission == "Users") { 
     convertView = (View) inflater.inflate(
       getResources().getLayout(R.layout.users_perm_dialog), null); 

    } 

    // defining the alertdialog 
    builder.setTitle(_permission + " Permissions"); 

    builder.setView(convertView); 
    builder.setPositiveButton(R.string.accept, 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // do something with the new note 
        mInterface.OnCompleteDialog(permSet); 

       } 
      }).setNegativeButton(R.string.cancel, 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // User cancelled the dialog 
       } 
      }); 

    return builder.create(); 
} 

@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    // TODO Auto-generated method stub 
    if (alertDelete.isChecked()) { 
     // The toggle is enabled 
     permSet.add("alert_delete"); 
     Log.e("ALERTDELETE", "CHECKED"); 
    } else { 
     // The toggle is disabled 
     permSet.remove("alert_delete"); 
     Log.e("ALERTDELETE", "UNCHECKED"); 
    } 

} 

} 

INSIDE片段

@Override 
public void OnCompleteDialog(Set mPermSet) { 
    // TODO Auto-generated method stub 
    this.permSet = mPermSet; 
    String tempPermString = permSet.toString(); 
    Log.e("PERMISSIONS", tempPermString); 

} 

堆棧跟蹤

10-30 11:41:30.081: E/AndroidRuntime(16925): FATAL EXCEPTION: main 
10-30 11:41:30.081: E/AndroidRuntime(16925): Process: com.e.main, PID: 16925 
10-30 11:41:30.081: E/AndroidRuntime(16925): java.lang.NullPointerException 
10-30 11:41:30.081: E/AndroidRuntime(16925): at com.e.dialog.CustomPermissionDialog$1.onClick(CustomPermissionDialog.java:171) 
10-30 11:41:30.081: E/AndroidRuntime(16925): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) 
10-30 11:41:30.081: E/AndroidRuntime(16925): at android.os.Handler.dispatchMessage(Handler.java:102) 
10-30 11:41:30.081: E/AndroidRuntime(16925): at android.os.Looper.loop(Looper.java:136) 
10-30 11:41:30.081: E/AndroidRuntime(16925): at android.app.ActivityThread.main(ActivityThread.java:5105) 
10-30 11:41:30.081: E/AndroidRuntime(16925): at java.lang.reflect.Method.invokeNative(Native Method) 
10-30 11:41:30.081: E/AndroidRuntime(16925): at java.lang.reflect.Method.invoke(Method.java:515) 
10-30 11:41:30.081: E/AndroidRuntime(16925): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792) 
10-30 11:41:30.081: E/AndroidRuntime(16925): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608) 
+0

您是否考慮使用應用程序類? – 2014-10-30 15:45:02

+0

我沒有。我創建了一個,但我不確定要採取哪些步驟才能使其按需執行。 – 2014-10-30 15:48:18

+0

當您回到片段時,將所需信息存儲在全局變量中,您可以從該變量中檢索數據 – 2014-10-30 15:51:05

回答

0

你d ialog在構造函數中還沒有活動,因此調用getActivity()時會返回null。將mInterface的初始化移至onCreateDialog()

1

您的空指針是由於您試圖在DialogFragment的構造函數中使用getActivity()來設置mInterfacegetActivity()此時片段的生命週期爲空。

嘗試在onCreate設置,或只投活動的界面,當你把它(假設你的活動是保證實現您的接口)

((OnCompleteDialogInterface) getActivity()).OnCompleteDialog(permSet); 

的另一個問題是,你的字符串比較都是錯誤的。您應該使用

if (_permission.equals("whatever")) 

其次,你應該使用if/else語句來檢查你的_permission字符串,以避免不必要的檢查。

此外,片段應該有空的構造函數。你應該看看如何使用片段

.setArgument(bundle)方法

您的佈局通脹是有點過於複雜

而不是

通過您 _permissions
convertView = (View) inflater.inflate(getResources().getLayout(R.layout.jobs_perm_dialog), null); 

你可以只使用

convertView = inflater.inflate(R.layout.jobs_perm_dialog, null); 
+0

我改變了很多我的代碼。謝謝你的提示和良好的做法。 – 2014-10-30 16:27:35