NullPointerException on TextEdit
I have a method that pops up an AlertDialog that asks the user for an IP address of a server to connect to for a game. The AlertDialog is as follows:
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 开发者_JAVA技巧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();
}
and servertextentry looks like this:
<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>
When I type in an IP and click the Connect button, I get a NullPointerException on the String host=edit.getText().toString();
line. How would I get this to work?
精彩评论