对话框崩溃应用 [英] Dialog crashing the app
本文介绍了对话框崩溃应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧只知道,我复制粘贴从Android网站code,所以我不觉得有什么不对。
问题是,当我preSS的按钮,它crashes.And它从对话框code崩溃,因为我没有任何东西在那里。
在code是:
MainDialog.xml:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / layout_root
机器人:方向=横向
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:填充=10dp
>
< ImageView的
机器人:ID =@ + ID /图像
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:layout_marginRight =10dp
/>
< TextView的机器人:ID =@ + ID /文
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:文字颜色=#FFF
/> < / LinearLayout中>
和上Button1的情况是pressed:
上下文mContext = getApplicationContext();
对话的对话=新的对话框(mContext); dialog.setContentView(R.layout.maindialog);
dialog.setTitle(自定义对话框); TextView的文本=(TextView的)dialog.findViewById(R.id.text);
text.setText(你好,这是一个自定义对话框!);
ImageView的图像=(ImageView的)dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
dialog.show();
解决方案
您使用活动
背景和呼叫添加到 dialog.show( );
:
上下文mContext =这一点; //假设你从一个活动内调用此
对话的对话=新的对话框(mContext); dialog.setContentView(R.layout.maindialog);
dialog.setTitle(自定义对话框); TextView的文本=(TextView的)dialog.findViewById(R.id.text);
text.setText(你好,这是一个自定义对话框!);
ImageView的图像=(ImageView的)dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
dialog.show();
Ok just to know,I copy-pasted the code from the android website,so I don't think there is something wrong.
The problem is that when I press the button it crashes.And it crashes from the dialog code,because I don't have anything else there.
The code is:
MainDialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
And on the case that button1 is pressed:
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.maindialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
dialog.show();
解决方案
You use the Activity
context and add a call to dialog.show();
:
Context mContext = this; //Assumes you are calling this from within an activity
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.maindialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
dialog.show();
这篇关于对话框崩溃应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文