TextEdit上的NullPointerException

时间:2011-08-05 23:02:10

标签: android nullpointerexception alertdialog

我有一个方法会弹出一个AlertDialog,询问用户要连接到游戏的服务器的IP地址。 AlertDialog如下:

public void onConnectPressed(View v)
{
    LayoutInflater factory = LayoutInflater.from(this);
    final View view = factory.inflate(R.layout.servertextentry, null);

    AlertDialog.Builder d=new AlertDialog.Builder(this);
    d.setTitle("Target IP")
    .setView(view)
    .setPositiveButton("Connect", new OnClickListener()
    {
        public void onClick(DialogInterface arg0, int arg1)
        {
            EditText edit=(EditText) view.findViewById(R.id.ip_edit);
            String host=edit.getText().toString();
            new ClientThread(host).start();
        }
    })
    .setNegativeButton("Nevermind.", new OnClickListener()
    {
        public void onClick(DialogInterface arg0, int arg1)
        {
            arg0.dismiss();
        }
    });
    d.show();
}

和servertextentry看起来像这样:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
  <TextView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:text="Server IP"
    android:gravity="left"
    android:textAppearance="?android:attr/textAppearanceMedium"/> 
  <EditText
    android:id="@+id/ip_edit"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:scrollHorizontally="true"
    android:autoText="false" 
    android:capitalize="none"
    android:gravity="fill_horizontal"
    android:textAppearance="?android:attr/textAppearanceMedium"/> 
</LinearLayout>

当我输入IP并单击“连接”按钮时,我在String host=edit.getText().toString();行上收到NullPointerException。我怎么能让这个工作?

0 个答案:

没有答案