我有一个DialogFragment创建一个listview对话框,在列表项上单击我想显示一个警告对话框,但是当我创建对话框时,它给了我一个NullPointerException我从未见过的错误
08-05 11:40:42.315: E/AndroidRuntime(4693): java.lang.NullPointerException
08-05 11:40:42.315: E/AndroidRuntime(4693): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
08-05 11:40:42.315: E/AndroidRuntime(4693): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
08-05 11:40:42.315: E/AndroidRuntime(4693): at com.tyczj.bowling.dialogs.SeasonType$1$1.onClick(SeasonType.java:60)
08-05 11:40:42.315: E/AndroidRuntime(4693): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
08-05 11:40:42.315: E/AndroidRuntime(4693): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 11:40:42.315: E/AndroidRuntime(4693): at android.os.Looper.loop(Looper.java:137)
08-05 11:40:42.315: E/AndroidRuntime(4693): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-05 11:40:42.315: E/AndroidRuntime(4693): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 11:40:42.315: E/AndroidRuntime(4693): at java.lang.reflect.Method.invoke(Method.java:511)
08-05 11:40:42.315: E/AndroidRuntime(4693): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-05 11:40:42.315: E/AndroidRuntime(4693): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-05 11:40:42.315: E/AndroidRuntime(4693): at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
这是我的对话框 onCreate
@Override
public Dialog onCreateDialog(Bundle state){
final Dialog d = …Run Code Online (Sandbox Code Playgroud) 我正在打开Dialog一个Activity.对话框打开后,我打电话
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Run Code Online (Sandbox Code Playgroud)
问题是,当我通过点击取消按钮或单击对话框外部关闭对话框时,键盘切换到文本键盘并且不会消失工具我单击硬件返回按钮.如何在关闭对话框时关闭键盘,并将焦点返回到上一个窗口?
我正在通过扩展Dialog并使用setContentView()创建自定义Dialog.问题是,当我尝试显示对话框时,它只显示背景阴影而不是我的自定义布局.我已经设法通过将它包装在RelativeLayout中来显示自定义布局,但我想知道是否有更好的解决方案或我做错了什么.使用constraintLayout版本1.0.2.谢谢!
这是我的自定义对话框:
class EventsFilterDialog(context: Context): Dialog(context) {
init {
setContentView(R.layout.dialog_events_filter)
setCancelable(true)
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的R.layout.dialog_events_filter的简化版本,它具有完全相同的结果:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/headerTv"
android:text="@string/events_filters"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold"
android:gravity="center"
android:background="@color/colorPrimaryLight"
android:padding="16dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<View
android:id="@+id/middleView"
android:layout_width="0dp"
android:layout_height="256dp"
app:layout_constraintTop_toBottomOf="@+id/headerTv"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/closeTv"
android:text="@string/close"
android:textColor="@color/white"
android:textAllCaps="true"
android:textStyle="bold"
android:gravity="center"
android:background="@color/colorPrimaryLight"
android:layout_width="0dp"
android:layout_height="48dp"
app:layout_constraintTop_toBottomOf="@+id/middleView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/applyTv" />
<TextView
android:id="@+id/applyTv"
android:text="@string/apply_filter"
android:textColor="@color/white"
android:textAllCaps="true"
android:textStyle="bold"
android:gravity="center"
android:background="@color/colorAccent"
android:layout_width="0dp"
android:layout_height="48dp"
app:layout_constraintTop_toBottomOf="@+id/middleView"
app:layout_constraintLeft_toRightOf="@+id/closeTv"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud) 我正在构建一个使用a的自定义对话框DialogFragment.我注意到非常奇怪的行为,各种ViewGroup用作对话框布局的根.我认为这是由于系统窗口之间的一些奇怪的交互以及它如何显示对话框.在这个特定的实例中,我使用a ConstraintLayout作为布局的根视图.
显示时,对话框将延伸到屏幕边缘,Layout Inspector显示测量宽度超过16,000,000.甚至更奇怪的是ConstraintLayout定义填充,仍然可以在屏幕上看到.
下面是对话框的类:
public class AgreementDialog extends DialogFragment {
// Bindings..
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_agreement, container);
ButterKnife.bind(this, view);
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
这是布局,dialog_agreement:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff"
android:padding="@dimen/margin_large"
android:layout_margin="@dimen/margin_xlarge"
>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_standard"
android:text="this is some text. "
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/checkbox"
/>
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_large"
android:text="123456789"
app:layout_constraintBottom_toTopOf="@+id/negative" …Run Code Online (Sandbox Code Playgroud) android android-dialog android-dialogfragment android-constraintlayout
我需要在Android应用程序中显示自定义对话框.标准AlertDialog设计是不可接受的.Android文档说:
提示:如果需要自定义对话框,则可以将"活动"显示为对话框,而不是使用"对话框API".只需创建一个活动并将其主题设置为清单元素中的Theme.Holo.Dialog:
而已.活动现在显示在对话窗口而不是全屏.
听起来很有希望.我做到了,但透明度不起作用!背景总是灰色的:

这是清单中的一个desc:
<activity
android:name=".MyDialogActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Dialog.NoActionBar" >
</activity>
Run Code Online (Sandbox Code Playgroud)
这是我活动的根源:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#00000000"
android:layout_width="400dp"
android:layout_height="wrap_content" >
// Content ... //
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我也尝试过android:background="@null"- 效果是一样的.如果我使用android:background="#ff0000"那么它是红色的(应该是).但是我该如何让它变得透明?
更新
我结束了
<style name="MyThemeDialogCustom" parent="android:Theme.Holo.Dialog.NoActionBar" >
<item name="android:windowBackground">@color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
</style>
Run Code Online (Sandbox Code Playgroud) 我有一个DialogDragment,我可以用以下两种方式之一:
1)通过点击其OnItemClickListener中的ListView项
2)通过激活ListView的上下文菜单并选择菜单项
做#1在所有生命周期事件下都能正常工作,但是如果我通过#2调用它并暂停活动(通过回家)并通过任务切换器恢复它,则不再显示对话框.片段在那里,我可以旋转设备并显示对话框.
我进行了实验,如果我把DialogFragment的显示放到Handler中,延迟时间至少为1/2秒,那就可以了.
以下代码段失败 - 它显示对话框,但暂停/恢复隐藏它:
public boolean onContextItemSelected(android.view.MenuItem item) {
boolean consumed = false;
switch (item.getItemId()) {
case R.id.menu_item:
showMyDialogFragment();
consumed = true;
break;
}
return consumed;
}
Run Code Online (Sandbox Code Playgroud)
所以下面的代码段可行.暂停/恢复再次正确显示对话框:
public boolean onContextItemSelected(android.view.MenuItem item) {
boolean consumed = false;
switch (item.getItemId()) {
case R.id.menu_item:
new Handler().postDelayed(new Runnable() {
public void run() {
showMyDialogFragment();
}
}, 300);
consumed = true;
break;
}
return consumed;
}
Run Code Online (Sandbox Code Playgroud)
用0ms或250ms延迟替换300ms秒延迟会导致它再次被破坏.这可重复100%的时间.
这显然是一个可怕的黑客,通过可能取决于设备速度的常数变得更糟.
有谁知道为什么会这样做和/或提供更好的解决方案?我在这个问题上花了好几个小时,这是我能想到的最好的.
在第一次启动应用程序时,我想提示用户(通过对话框)选择他们的城市,然后这将保存他们的偏好并将正确的数据加载到列表视图中.但是,我注意到可以通过后退按钮关闭对话框,或者按对话框外的任何位置,然后触发数据库的空值.
无论如何我可以强制对话框保持打开状态,直到用户选择了一个选项?
我已经实现了.setCancelable(false),这似乎不起作用.我的代码如下,DialogSetup是我正在使用的内部类.
任何帮助/想法将不胜感激.
public class MainActivity extends ListActivity {
private Cursor lines;
private MetroSleepDb db;
private ListAdapter adapter;
public static final String PREFS_NAME = "METROSLEEP_PREFS";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
chooseCityDialog();
}
public void setAdapters() {
db = new MetroSleepDb(this);
lines = db.getLines(); // you would not typically call this on the main thread
//ListView listView = (ListView) findViewById(R.id.li);
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
lines,
new String[] {"line_id"},
new int[] {android.R.id.text1}, 0);
getListView().setAdapter(adapter); …Run Code Online (Sandbox Code Playgroud) android android-fragments android-dialog android-dialogfragment
当我在Android 4.4及更高版本上运行时,我正在我的应用中使用沉浸式模式.(http://developer.android.com/training/system-ui/immersive.html)
我的活动确实以全屏显示,我通过使用按下音量键setOnSystemUiVisibilityChangeListener.我也有类似的代码将对话框放入沉浸式模式.
但是,当显示对话框时,导航.酒吧跳到屏幕上,然后立即撤退.当对话被解雇时,情况更糟 - 导航.酒吧跳跃并调整背后的活动.
以下是我的支持沉浸式模式的课程.它只是在每个Activity的onResume上调用,并且在构建每个对话框时也会调用一个单独的函数.
我错过了任何旗帜或回调,还是已知的Android问题?
public class ImmersiveModeHelper {
public ImmersiveModeHelper(Activity activity)
{
mActivity = activity;
}
@SuppressLint("NewApi")
public void supportFullScreenImmersiveMode()
{
MyLog.d("ImmersiveModeHelper: supportFullScreenImmersiveMode: ");
// Support full-screen immersive mode on Android 4.4 and up
if (Build.VERSION.SDK_INT >= 19)
{
// Get the needed flags by reflection and use them
try
{
final int immersiveFlag = View.class.getField("SYSTEM_UI_FLAG_IMMERSIVE_STICKY")
.getInt(null);
final int hideNavigationFlag = View.class
.getField("SYSTEM_UI_FLAG_HIDE_NAVIGATION").getInt(null);
final int fullScreenFlag = View.class.getField("SYSTEM_UI_FLAG_FULLSCREEN").getInt(
null);
// Set the …Run Code Online (Sandbox Code Playgroud) android android-ui android-dialog android-activity android-4.4-kitkat
我正在尝试找到一个示例代码来实现具有全宽按钮的材质对话框,如下面的屏幕截图所示.
有人可以通过分享有关如何复制此设计的示例代码来提供帮助吗?

我的应用程序显示进度或AlertDialog用户非常常见.如果用户将应用程序放入后台然后稍后返回,我希望Dialog仍然显示该应用程序.有没有办法让Android处理这个?我想它要么不关闭对话框,要么在Activity恢复时自动重新打开它.
到目前为止它看起来像没有.我没有找到大量关于此的结果(大多数人遇到方向更改的问题,我的应用程序不允许),但很少有人询问进入后台.我已经尝试了DialogFragment常规的每个排列Dialog,但是当按下主页按钮并且从任务管理器打开应用程序时,它们都会消失.
我甚至没有任何代码可以展示,因为它们都处于在线各种示例的测试阶段.我怀疑我必须自己管理这个,通过签入onResume()是否应该显示的东西.如果是这种情况我可以忍受它,但我想知道肯定.