DialogFragment的简单示例 - 示例代码对我不起作用

时间:2014-09-11 17:05:40

标签: android android-fragments android-dialogfragment

请原谅我的无知,这是我作为Android开发人员工作的第一周。虽然我有几年的Java,但我在Android世界中是一个完整的菜鸟。

我只是想创建一个对话框并显示它。通过研究,我发现这是首选方法:

https://developer.android.com/guide/topics/ui/dialogs.html

所以,我走了那条路,一起拍了一个基本的例子,跑步时我得到一个例外:

  

09-11 11:57:28.914:E / AndroidRuntime(1217):致命异常:主要   09-11 11:57:28.914:E / AndroidRuntime(1217):进程:com.myapp.mobile,   PID:1217 09-11 11:57:28.914:E / AndroidRuntime(1217):   java.lang.RuntimeException:无法启动活动   ComponentInfo {} com.myapp.mobile/com.myapp.mobile.MainActivity:   java.lang.IllegalStateException:活动已被销毁09-11   11:57:28.914:E / AndroidRuntime(1217):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)   09-11 11:57:28.914:E / AndroidRuntime(1217):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)   09-11 11:57:28.914:E / AndroidRuntime(1217):at   android.app.ActivityThread.access $ 800(ActivityThread.java:135)09-11   11:57:28.914:E / AndroidRuntime(1217):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)   09-11 11:57:28.914:E / AndroidRuntime(1217):at   android.os.Handler.dispatchMessage(Handler.java:102)09-11   11:57:28.914:E / AndroidRuntime(1217):at   android.os.Looper.loop(Looper.java:136)09-11 11:57:28.914:   E / AndroidRuntime(1217):at   android.app.ActivityThread.main(ActivityThread.java:5017)09-11   11:57:28.914:E / AndroidRuntime(1217):at   java.lang.reflect.Method.invokeNative(Native Method)09-11   11:57:28.914:E / AndroidRuntime(1217):at   java.lang.reflect.Method.invoke(Method.java:515)09-11 11:57:28.914:   E / AndroidRuntime(1217):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)   09-11 11:57:28.914:E / AndroidRuntime(1217):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)09-11   11:57:28.914:E / AndroidRuntime(1217):at   dalvik.system.NativeStart.main(Native Method)09-11 11:57:28.914:   E / AndroidRuntime(1217):引起:java.lang.IllegalStateException:   活动已被破坏09-11 11:57:28.914:   E / AndroidRuntime(1217):at   android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1345)   09-11 11:57:28.914:E / AndroidRuntime(1217):at   android.app.BackStackRecord.commitInternal(BackStackRecord.java:597)   09-11 11:57:28.914:E / AndroidRuntime(1217):at   android.app.BackStackRecord.commit(BackStackRecord.java:575)09-11   11:57:28.914:E / AndroidRuntime(1217):at   android.app.DialogFragment.show(DialogFragment.java:230)09-11   11:57:28.914:E / AndroidRuntime(1217):at   com.myapp.mobile.dialog.ActivationDialog.show(ActivationDialog.java:25)   09-11 11:57:28.914:E / AndroidRuntime(1217):at   com.myapp.mobile.activate.Activation.ActivationCheck(Activation.java:11)   09-11 11:57:28.914:E / AndroidRuntime(1217):at   com.myapp.mobile.MainActivity.onCreate(MainActivity.java:20)09-11   11:57:28.914:E / AndroidRuntime(1217):at   android.app.Activity.performCreate(Activity.java:5231)09-11   11:57:28.914:E / AndroidRuntime(1217):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)   09-11 11:57:28.914:E / AndroidRuntime(1217):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)   09-11 11:57:28.914:E / AndroidRuntime(1217):... 11更多

来自MainActivity的电话:

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

    //check user activation
    ActivationDialog dialog = new ActivationDialog();
    dialog.show();
}

ActivationDialog类:

public class ActivationDialog extends FragmentActivity implements BaseDialogFragment.BaseDialogListener
{
    public ActivationDialog()
    {
    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog)
    {
    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog)
    {
    }

    public void show()
    {
        BaseDialogFragment dialogFrag = new BaseDialogFragment();
        dialogFrag.show(this.getFragmentManager(), "activationDialog");
    }
}

基类:

public class BaseDialogFragment extends DialogFragment
{
    public interface BaseDialogListener
    {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    }

    BaseDialogListener dialogListener;

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);

        try
        {
            dialogListener = (BaseDialogListener)activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setMessage(R.string.dialog_base_message).setPositiveButton(R.string.dialog_base_ok, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
            }
        }).setNegativeButton(R.string.dialog_base_cancel, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
            }
        });

        return builder.create();
    }
}

2 个答案:

答案 0 :(得分:5)

我通过反复试验找到了更好的方法,并通过示例进行了捕捉。我认为。我不知道这样做是否有缺点,但这是有效的。我现在要尝试添加控件和事件,但这回答了我原来的问题。

自定义DialogFragment:

public class MyDialogFragment extends DialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        return new AlertDialog.Builder(getActivity())
            .setTitle("Dialog Title")
            .setPositiveButton("OK", null)
            .create();
    }
}

显示对话框:

public class MainActivity extends ActionBarActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyDialogFragment myDiag = new MyDialogFragment();
        myDiag.show(getFragmentManager(), "Diag");
    }
}

答案 1 :(得分:2)

你没有在MainActivity中创建过片段。 FragmentManager fm = getSupportFragmentManager(); 很好的例子 http://www.androidbegin.com/tutorial/android-dialogfragment-tutorial/

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends FragmentActivity {

Button dfragbutton;
Button alertdfragbutton;
FragmentManager fm = getSupportFragmentManager();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);

    // Locate the button in activity_main.xml
    dfragbutton = (Button) findViewById(R.id.dfragbutton);
    alertdfragbutton = (Button) findViewById(R.id.alertdfragbutton);

    // Capture button clicks
    dfragbutton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            DFragment dFragment = new DFragment();
            // Show DialogFragment
            dFragment.show(fm, "Dialog Fragment");
        }
    });

    // Capture button clicks
    alertdfragbutton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            AlertDFragment alertdFragment = new AlertDFragment();
            // Show Alert DialogFragment
            alertdFragment.show(fm, "Alert Dialog Fragment");
        }
    });
 }
}