DialogFragment在Android中编辑了show()方法

时间:2014-09-01 09:05:07

标签: android android-dialogfragment

我正在使用DialogFragments,我希望使用其他参数更改show(fragmentmanager,string)方法。我尝试在dialogfragment类中实现新的show方法,并用它调用原始的show()方法,但它给出了异常。

MainActivity上课就在这里;

public class MainActivity extends Activity implements CustomDialogMethods {
CustomDialog cd = new CustomDialog();   
FragmentManager fm = getFragmentManager();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onClick(View v) {

    cd.Editshow();

}

Editshow方法调用dialogfragment编辑的show方法,

public class CustomDialog extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    bla..
    bla..
    bla..


    return dialog;
}


public void Editshow(){

    FragmentManager fm = getFragmentManager();

    show(fm, "inform");     
}

EditShow在这里调用原始的show方法..

它给出错误如何解决?

谢谢..

logCat错误:

09-01 11:54:32.199: E/AndroidRuntime(26617): FATAL EXCEPTION: main
09-01 11:54:32.199: E/AndroidRuntime(26617): Process: com.example.dialog2, PID: 26617
09-01 11:54:32.199: E/AndroidRuntime(26617): java.lang.IllegalStateException: Could not execute method of the activity
09-01 11:54:32.199: E/AndroidRuntime(26617):    at android.view.View$1.onClick(View.java:3823)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at android.view.View.performClick(View.java:4438)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at android.view.View$PerformClick.run(View.java:18422)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at android.os.Handler.handleCallback(Handler.java:733)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at android.os.Looper.loop(Looper.java:136)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at android.app.ActivityThread.main(ActivityThread.java:5001)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at java.lang.reflect.Method.invokeNative(Native Method)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at java.lang.reflect.Method.invoke(Method.java:515)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at dalvik.system.NativeStart.main(Native Method)
09-01 11:54:32.199: E/AndroidRuntime(26617): Caused by: java.lang.reflect.InvocationTargetException
09-01 11:54:32.199: E/AndroidRuntime(26617):    at java.lang.reflect.Method.invokeNative(Native Method)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at java.lang.reflect.Method.invoke(Method.java:515)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at android.view.View$1.onClick(View.java:3818)
09-01 11:54:32.199: E/AndroidRuntime(26617):    ... 11 more
09-01 11:54:32.199: E/AndroidRuntime(26617): Caused by: java.lang.NullPointerException
09-01 11:54:32.199: E/AndroidRuntime(26617):    at com.example.dialog2.CustomDialog.showw(CustomDialog.java:118)
09-01 11:54:32.199: E/AndroidRuntime(26617):    at com.example.dialog2.MainActivity.onClick(MainActivity.java:32)


  MainActivity(){
  FragmentManager fm = getFragmentManager();
  cd.Editshow(this,fm);
  }

但它无法使用

  CustomDialog(){

  editShow(){
  FragmentManager fm = getFragmentManager();
  show(fm,"tag");
   }
   }

如何在CustomDialog类中使用MainActivity的FragmentManager?

1 个答案:

答案 0 :(得分:0)

You Can Use this Simple Form.....for dialog fragment

 public class MainActivity extends FragmentActivity implements CustomDialogListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ShowDailog();
}

private void ShowDailog() {
    FragmentManager fm = getSupportFragmentManager();
    CustomDialog dialog = new CustomDialog();
    dialog.show(fm, "fragment_edit_name");
}


    @Override
public void onFinishCustomDialog(String inputText) {
    Toast.makeText(this, "Hi, " + inputText, Toast.LENGTH_SHORT).show();
}
}

// CustomDialog类

public class CustomDialog extends DialogFragment implements OnEditorActionListener {

public interface CustomDialogListener {
    void onFinishCustomDialog(String inputText);
}

private EditText mEditText;

public CustomDialog() {
    // Empty constructor required for DialogFragment
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_edit_name, container);
    mEditText = (EditText) view.findViewById(R.id.txt_your_name);
    getDialog().setTitle("Hello");

    // Show soft keyboard automatically
    mEditText.requestFocus();
    getDialog().getWindow().setSoftInputMode(
            LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    mEditText.setOnEditorActionListener(this);

    return view;
}

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (EditorInfo.IME_ACTION_DONE == actionId) {
        // Return input text to activity
        CustomDialogListener activity = (CustomDialogListener) getActivity();
        activity.onFinishCustomDialog(mEditText.getText().toString());
        this.dismiss();
        return true;
    }
    return false;
}
}