我正在尝试在我的代码中实现一个简单的Dialog.但它不起作用.我搜索了每个可用的教程,包括官方的开发者指南,但没有任何作用.我从logcat得到的错误是我得到一个nullPointerException,我猜这是在getActivity上.有帮助吗?
这就是我所拥有的:这是我的自定义对话类.
公共类SaveDialog扩展DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Save Password");
builder.setView(getContentView());
Dialog dialog = builder.create();
dialog.show();
return dialog;
}
private View getContentView() {
LayoutInflater inflater = getActivity().getLayoutInflater();
return inflater.inflate(R.layout.dialog, null);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我发生onclick的主要活动
private void savePassword() {
SaveDialog savePasswordDialog = new SaveDialog();
savePasswordDialog.show(savePasswordDialog.getSupportFragmentManager(), "tag");
}
Run Code Online (Sandbox Code Playgroud)
每次我启动onClick,应用程序崩溃.最重要的是,目前我正在尝试使用getSupportFragmentManager,但它说它未定义.
在创建自定义Progress DialogFragment时,我有点困惑.一切正常,但由于我不希望用户"撤回"直到DialogFragment被解除,我正在尝试捕获KeyEvent并"禁用".
虽然这很好用:
@Override
public ProgressDialog onCreateDialog(Bundle savedInstanceState) {
final ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setMessage(getString(R.string.loading_text));
dialog.setIndeterminate(true);
dialog.setCancelable(false);
// Disable the back button
OnKeyListener keyListener = new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if( keyCode == KeyEvent.KEYCODE_BACK){
return true;
}
return false;
}
};
dialog.setOnKeyListener(keyListener);
return dialog;
}
Run Code Online (Sandbox Code Playgroud)
使用onCreateDialog不会让我正确地膨胀片段,因此,自定义我的片段的外观和感觉.另一方面,onCreateDialog完美地捕获了按键事件.切换到onCreateView时:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the XML view for the help dialog fragment
View view = inflater.inflate(R.layout.progress_dialog_fragment, container);
TextView …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序可以在DialogFragment. 我已经添加MediaController到VideoView但是有两个问题:
MediaController隐藏在DialogFragment.DialogFragment可见时更改屏幕方向会导致 Activity 存在泄漏窗口的异常对于第一个,我尝试使用linearLayoutParent.bringChildToFront(mediaControls)which 不起作用。我不知道如何处理第二个。
帮帮我。:)
我正在尝试使用其中的listview创建一个dialogfragment,并使用此问题中接受的答案来执行此操作
如何在DialogFragment中显示现有的ListFragment
但是Error inflating class fragment当我尝试打开片段对话框并且应用程序崩溃时,我得到了一个
下面是dialog_fragment_with_list_fragment布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding = "10dp"
class="com.OptimusApps.stayhealthy.AndroidXMLParsingActivity" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
并且它不是导致它失败的androidxmlparsingactivity片段,我用其他片段尝试过它们也没有用
下面是我的对话框片段类
public class BodyDialogue extends DialogFragment {
int mNum;
/**
* Create a new instance of MyDialogFragment, providing "num"
* as an argument.
*/
static BodyDialogue newInstance(int num) {
BodyDialogue f = new BodyDialogue();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args); …Run Code Online (Sandbox Code Playgroud) java android android-fragments android-dialogfragment android-fragmentactivity
我要做的是更改我编写的自定义DialogFragment的默认背景.通常,我会通过更改XML布局文件来执行此操作,但是,对于DialogFragment,这些按钮在布局文件中不存在.
本质上,我试图访问setPositiveButton,setNegativeButton和setNeutral按钮以修改它们.或者,我接下来尝试通过id获取它们,但是,因为它们没有在布局文件中定义,所以我没有相应的id.我已经找到了很多如何修改其余布局的例子,但我找不到任何正/中性/负面按钮可以修改的地方.
理想情况下,我可以在下面的代码块中执行此操作:
.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
...
}
})
Run Code Online (Sandbox Code Playgroud)
提前致谢.
美好的一天.道歉令人困惑的标题.
我正在构建一个Android应用程序,我有一个对话框片段.对话框片段具有设置的宽度.但是,问题是当应用程序在具有不同屏幕大小的设备上运行时,对话框片段未正确居中.
最初,我所拥有的是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500dp"
android:layout_height="wrap_content">
<-- code goes here -->
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
如您所见,我有一个具有定义宽度的relativeLayout.因为我知道你不能layout_weight在相对布局中使用,所以我做的是将线性布局中的父相对布局包裹起来:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:weightSum="1">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为当我在具有较小屏幕尺寸的设备上运行应用程序时,会切断对话框片段.
如何将对话框片段的宽度设置为屏幕大小的百分比?这是可能的还是我不得不求助于以编程方式设置它?
非常感谢任何帮助,谢谢.
android android-layout android-dialogfragment android-percent-library
NullPointerException每当我打电话时,我总会得到:
getApplicationContext().getResources().getStringArray(R.array.days);
Run Code Online (Sandbox Code Playgroud)
我DialogFragment在我的电话里打电话给我Activity.我也尝试过使用getActivity(),但这对我也不起作用.有没有人对这个问题有任何想法?
android nullpointerexception android-fragments android-dialogfragment android-resources
从我的Recylerview Adapter,每个项目中有多个按钮,我想要的是显示DialogFragment as confirmation单击它们的时间并在单击时执行不同的方法/操作positiveButton。
问题是我不知道如何拥有或如何在positiveButton单击时实现回调,并且可以根据每个项目上要单击的按钮执行不同的方法或操作。
e.g. 在我的适配器类上
@Override
public void onBindViewHolder(final PageOnlineAdapter.TheViewHolder holder, final int position) {
holder.btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content
newFragment.show(((AppCompatActivity)contextView).getSupportFragmentManager(), TAG);
/*
if positiveButton is clicked
=>execute method/action here
*/
}
);
holder.btn_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = ConfirmationDialog.newInstance(TITLE,CONTENT); //Initialize DialogFragment with a specific Title and Content …Run Code Online (Sandbox Code Playgroud) 我有这个适配器,但有这样的问题:
无法解析方法“getSupportFragmentManager()”
Fragment 是一个 DialogFragment 并且该类扩展了 AppCompatDialogFragment 我可以以其他方式启动这个 Fragment 吗?或者有解决办法吗?
public class PostAdapterProfile extends ArrayAdapter<Post> {
private int resource;
private LayoutInflater inflater;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mDatabaseReferences;
SharedPreferences prefs;
private String uuid;
private boolean verifyStar;
private User currentUserPost;
public PostAdapterProfile(@NonNull Context context, int resourceId, ArrayList<Post> objects) {
super(context, resourceId, objects);
resource = resourceId;
inflater = LayoutInflater.from(context);
}
public View getView(int position, View v, ViewGroup parent) {
if (v == null) {
Log.d("DEBUG", "Inflating view");
v = inflater.inflate(R.layout.list_element, null);
} …Run Code Online (Sandbox Code Playgroud) 我在活动上有一个按钮,通过调用此方法,我可以在该按钮上打开自定义对话框
public void openHcoDialog(View v) {
HcoDialog hcoDiag = new HcoDialog();
// Supply cityCode input as an argument.
Bundle args = new Bundle();
args.putString("cityCode", cityCode);
hcoDiag.setArguments(args);
hcoDiag.show(getSupportFragmentManager(), "hco dialog");
}
Run Code Online (Sandbox Code Playgroud)
在这个 HcoDialog 类中,我将扩展 DialogFragment 扩展为
public class HcoDialog extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.hco_dialog, null);
builder.setView(view);
builder.setCancelable(true);
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Toast.makeText(getActivity(), "Toast 1", Toast.LENGTH_LONG).show();
progressDialog.dismiss();
} …Run Code Online (Sandbox Code Playgroud)