首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android 自定义dialog,老报错! 请高手指教,该如何解决

2012-03-21 
android 自定义dialog,老报错! 请高手指教有关于android 自定义对话框, 怎么才能拿到dialog界面上的值?现

android 自定义dialog,老报错! 请高手指教

有关于android 自定义对话框, 怎么才能拿到dialog界面上的值? 现在是启动dialog画面后,在text里面输入值后,单击确认按钮,就报错!.解决了马上给分,请高手看小弟的代码:


Java code
public class MainActivity extends Activity {        private Button btn=null;        private EditText etxt=null;        private TextView txt=null;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        btn=(Button)findViewById(R.id.button1);        btn.setOnClickListener(new OnClickListener(){                                        @Override                        public void onClick(View arg0) {                                // TODO Auto-generated method stub                                showDialog(1);                        }});        }        @Override        protected Dialog onCreateDialog(int id) {                // TODO Auto-generated method stub                Dialog dialog=null;                if(id==1)                {                        LayoutInflater inflater=LayoutInflater.from(this);                        View layout=inflater.inflate(R.layout.dialog, null);                        Builder builder2=new Builder(this);                        builder2.setTitle("DialogTest");                        builder2.setView(layout);                                                DialogInterface.OnClickListener btnListener2=new DialogInterface.OnClickListener(){                                @Override                                public void onClick(DialogInterface arg0, int arg1) {                                        // TODO Auto-generated method stub                                                etxt=(EditText)findViewById(R.id.eidt);                                        txt=(TextView)findViewById(R.id.tv);                                                                                txt.setText(etxt.getText().toString());                                }};                        builder2.setPositiveButton("Kill", btnListener2);                                                dialog = builder2.create();                                }                return dialog;        }    }

main.xml
XML code
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    android:id="@+id/tv"    /><Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout>



dialog.xml
XML code
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >        <EditText android:id="@+id/eidt" android:layout_width="fill_parent" android:layout_height="wrap_content"/></LinearLayout>


------解决方案--------------------


调试一下,看看

 etxt=(EditText)findViewById(R.id.eidt);
txt=(TextView)findViewById(R.id.tv);
的返回值是否是null
[解决办法]
etxt=(EditText)findViewById(R.id.eidt);
这句话错啦!
修改:
etext=(EditText)layout.findViewById(R.id.eidt);

这样应该可以啦!
楼主试试吧!

热点排行