问题
I have some difficulties getting AlertDialog
working in fragments. I have six buttons and when each button is clicked AlertDialog
box should display its content. even though the prg is running without any error and tat fragment page is opening with the buttons if i click on a button dialogbox is not opening can anyone help me out? Thanks in advance.
Here is my fragment activity code:
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Fragment3 extends DialogFragment implements OnClickListener {
private static final int ALERT_DIALOG1 = 1;
private static final int ALERT_DIALOG2 = 2;
private static final int ALERT_DIALOG3 = 3;
private static final int ALERT_DIALOG4 = 4;
private static final int ALERT_DIALOG5 = 5;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//setContentView(R.layout.activity_fragment2);
View view = inflater.inflate(R.layout.fragment3layout, container, false);
Button alert1 = (Button) view.findViewById(R.id.Button1);
alert1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
showDialog(ALERT_DIALOG1);
}
});
default:
dialog = null;
}
return dialog;
}
Here is my Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pacificstereoapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
</intent-filter>
</activity>
<activity
android:name="com.example.a.Fragment1"
android:label="@string/title_activity_fragment1" >
</activity>
</application>
</manifest>
回答1:
You are creating dialog, but not really showing it. Call dialog.show()
.
来源:https://stackoverflow.com/questions/14889174/alertdialog-in-fragments