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