【发布时间】:2018-04-19 16:54:36
【问题描述】:
我尝试开发一个小的 android 应用程序并显示一个对话框警报,其中只有一个编辑文本来填充自定义 作为下一个链接
Creating a custom layout 我有这个代码,但它不起作用
public class TraceDialogFragment extends DialogFragment {
TraceDialogListener mListener;
public interface TraceDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.activity_trace, null))
.setIcon(R.drawable.ic_action_locate)
.setTitle(R.string.trace_title)
// Add action buttons
.setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogPositiveClick(TraceDialogFragment.this);
}
})
.setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogNegativeClick(TraceDialogFragment.this);
}
});
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
mListener = (TraceDialogListener) context;
}catch (ClassCastException e){
throw new ClassCastException(context.toString() + " must implement TraceDialogListener");
}
}
}
但代码在循环中运行
LayoutInflater inflater = getActivity().getLayoutInflater();
不明白
在我的课堂主要活动中
我有这个代码
class MainActivity extends AppCompatActivity implements TraceDialogFragment.TraceDialogListener
DialogFragment traceDialog = new TraceDialogFragment();
traceDialog.show(getSupportFragmentManager(),"TraceDialogFragment");
这是我的styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
我的 logcat 是下一个
11-07 11:10:50.015 23570-23570/E/AndroidRuntime: FATAL EXCEPTION: main
Process: , PID: 23570
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:359)
感谢您的帮助,
【问题讨论】:
-
代码乍一看还不错。 “循环运行”是什么意思?应用程序会崩溃吗?如果是这样,请发布您的 logcat 输出。
-
我有这个日志 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this Activity。
-
这个公共类MainActivity扩展了AppCompatActivity实现了TraceDialogFragment.TraceDialogListener
-
如果我调试它停留在这 2 行然后返回去
标签: android android-layout android-fragments