0

I have the following code written but it crashes when I run it. I am trying to use an AlertDialog to allow the background image of an ImageButton to be changed based on the offered selection in the AlertDialog:

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

public class PageTwoFragment extends Fragment {

    int i = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    final Bundle savedInstanceState) {
final ViewGroup rootView = (ViewGroup) inflater.inflate(
        R.layout.page2_layout, container, false);

final ImageButton pp_btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);
final ImageButton m1_ts_btn = (ImageButton) rootView.findViewById(R.id.m1_ts_btn);
final Context context = getActivity();

pp_btn1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        i +=1;
        if (i % 2 == 0) {
            pp_btn1.setImageResource(R.drawable.pause);
        } else {
            pp_btn1.setImageResource(R.drawable.play);
        }
    }
});

    m1_ts_btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        LayoutInflater inflater = getLayoutInflater(savedInstanceState);
        View view = inflater.inflate(R.layout.ts_alert, (ViewGroup) rootView.findViewById(R.id.RelativeLayout1)); 

        alertDialogBuilder.setTitle("My Title");

        ImageButton ts_1_4 = (ImageButton) view.findViewById(R.id.m1_ts_btn);
        alertDialogBuilder.setView(view);
        alertDialogBuilder.show();
        ts_1_4.setImageResource(R.drawable.image_2);

    }
});   
return rootView;

}
}

I currently only have the one image to show but once I get this working I will then add more.

Many thanks

EDIT

My error logs are as follows:

09-07 22:31:09.770: E/AndroidRuntime(18083): FATAL EXCEPTION: main
09-07 22:31:09.770: E/AndroidRuntime(18083): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3618)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.view.ViewGroup.addView(ViewGroup.java:3489)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.view.ViewGroup.addView(ViewGroup.java:3465)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at com.android.internal.app.AlertController.setupView(AlertController.java:402)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at com.android.internal.app.AlertController.installContent(AlertController.java:242)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.app.AlertDialog.onCreate(AlertDialog.java:336)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.app.Dialog.dispatchOnCreate(Dialog.java:351)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.app.Dialog.show(Dialog.java:256)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at com.example.example.PageTwoFragment$6.onClick(PageTwoFragment.java:110)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.view.View.performClick(View.java:4211)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.view.View$PerformClick.run(View.java:17267)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.os.Handler.handleCallback(Handler.java:615)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.os.Looper.loop(Looper.java:137)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at android.app.ActivityThread.main(ActivityThread.java:4898)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at java.lang.reflect.Method.invokeNative(Native Method)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at java.lang.reflect.Method.invoke(Method.java:511)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
09-07 22:31:09.770: E/AndroidRuntime(18083):    at dalvik.system.NativeStart.main(Native Method)

Thanks

4

2 回答 2

1

以下行是问题的根源:

View view = inflater.inflate(R.layout.ts_alert, (ViewGroup) rootView.findViewById(R.id.RelativeLayout1));

您必须将其更改为:

View view = inflater.inflate(R.layout.ts_alert, (ViewGroup) rootView.findViewById(R.id.RelativeLayout1), false);

否则,您的膨胀视图将附加到R.id.RelativeLayout1并生成IllegalStateException,这是错误的,因为您稍后会将其附加到另一个父级:

alertDialogBuilder.setView(view);

编辑:

如果您想向对话框添加按钮,而不是在布局中声明它们,请使用标准方法setPositiveButton()setNegativeButton()setNeutralButton()

于 2013-09-07T21:44:26.160 回答
0

做了更多的研究后,我通过以下方式实现了我想要的:

final ImageButton m1_ts_btn = (ImageButton) rootView.findViewById(R.id.m1_ts_btn);

m1_ts_btn.setOnClickListener(new OnClickListener() {             
@Override
    public void onClick(View v) {
        myDialog = new Dialog(getActivity());
        myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        myDialog.setContentView(R.layout.mydialog);
        myDialog.setCancelable(true);
        final ImageButton btn_ts_1_4 = (ImageButton) myDialog.findViewById(R.id.dialog_1_4);
        final ImageButton btn_ts_2_4 = (ImageButton) myDialog.findViewById(R.id.dialog_2_4);
        final ImageButton btn_ts_3_4 = (ImageButton) myDialog.findViewById(R.id.dialog_3_4);
        final ImageButton btn_ts_4_4 = (ImageButton) myDialog.findViewById(R.id.dialog_4_4);

        btn_ts_1_4.setOnClickListener(new OnClickListener() {
        @Override
            public void onClick(View v) {
            m1_ts_btn.setImageResource(R.drawable.ts_1_4);
            m2_ts_btn.setImageResource(R.drawable.ts_1_4);
            m3_ts_btn.setImageResource(R.drawable.ts_1_4);
            m4_ts_btn.setImageResource(R.drawable.ts_1_4);
            m5_ts_btn.setImageResource(R.drawable.ts_1_4);
            myDialog.dismiss();
            }
        });
于 2013-09-08T15:33:28.830 回答