我是 Android 的新手,并且在工作中得到了一堆客户的源代码来修复。我试图创建的 DialogFragment 不会保持打开状态。当我单步执行它时,它会打开,然后在调用 newFragement.show() 时立即再次关闭。
我的onCreateDialog一直告诉我我当前的targetSdk是8,但是我已经多次来回更改为11,甚至重新启动了Android Studio,但仍然告诉我它是错误的。
到目前为止,我发现的与此相关的所有其他帖子都没有回答我的问题。我无法让 FragmentActivity 工作。这是我的代码:
ShowUpgradeDialogFragment 类:
public class ShowUpgradeDialogFragment extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_builder_message)
.setPositiveButton(R.string.dialog_builder_positive, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// upgrade
}
})
.setNegativeButton(R.string.dialog_builder_negative, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});//*/
System.out.println ("hello");
// Create the AlertDialog object and return it
AlertDialog dialog = builder.create();
return dialog;
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ila.tax_shield"
android:versionCode="18" android:versionName="3.2.4">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18"/>
<fragment android:name=".ShowUpgradeDialogFragment"
android:label="@string/app_name"
android:windowIsFloating="true"
android:background="@android:color/transparent"
android:screenOrientation="portrait">
</fragment>
// remaining manifest
</manifest>
我的 onCreate 方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DialogFragment newFragment = new ShowUpgradeDialogFragment();
newFragment.show(getFragmentManager(), "dialog");
// code
finish() // wrong and was removed, which solved my bug
}
display_update_message.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.ila.tax_shield.fragments.ShowUpgradeDialogFragment"
android:id="@+id/display_update_message"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="2dp"
tools:layout="@layout/display_update_message" />
</LinearLayout>
它不喜欢'android:name="com.ila.tax_shield.fragments.ShowUpgradeDialogFragment"',我不明白为什么
在此先感谢您的帮助!