我正在尝试创建一个对话框,其中有一些edittexts和两个单选按钮。但由于java.lang.NullPointerException,应用程序强行崩溃。对此有任何帮助将非常有帮助。我尝试了弹出菜单而不是单选按钮但仍然是java.lang.NullPointerException错误。
custom_dialog
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/ET_Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Name"
android:layout_marginTop="10dp" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/ET_Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress"
android:hint="Email"
android:layout_marginTop="10dp" />
<EditText
android:id="@+id/ET_Mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
android:hint="Mobile"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/POP_Country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Country"
android:layout_marginTop="10dp" />
<EditText
android:id="@+id/ET_City"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:maxLines="1"
android:hint="City"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/POP_Pack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Package"
android:layout_marginTop="10dp" />
<EditText
android:id="@+id/ET_Info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:hint="Additional Information"
android:layout_marginTop="10dp" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp" >
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_toLeftOf="@+id/view"
android:text="Submit" />
<View
android:id="@+id/view"
android:layout_width="3dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_alignTop="@+id/submit"
android:layout_toRightOf="@+id/view"
android:text="Reset" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.dialogwithspinner.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="170dp"
android:text="Button"
android:onClick="Function" />
</RelativeLayout>
MainActivity
package com.example.dialogwithspinner;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
String[] countries = { "Cupcake", "Donut", "Eclair", "Froyo",
"Gingerbread", "HoneyComb", "IceCream Sandwich", "Jellybean",
"Cote D'ivoire" };
//RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//rl = (RelativeLayout) findViewById(R.id.RL);
final Button btn = (Button)findViewById(R.id.POP_Country);
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setSingleChoiceItems(countries, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
Button POP_Country = (Button)findViewById(R.id.POP_Country);
POP_Country.setText(countries[which]);
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ad.show();
}
});
} //onCreate ends
public void Function(View v){
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Enquiry Form");
dialog.setCancelable(true);
Button submit = (Button) dialog.findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Submitted", Toast.LENGTH_SHORT).show();
}
});
Button reset = (Button) dialog.findViewById(R.id.reset);
reset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText ET_Name = (EditText)findViewById(R.id.ET_Name);
EditText ET_Email = (EditText)findViewById(R.id.ET_Email);
EditText ET_Mobile = (EditText)findViewById(R.id.ET_Mobile);
EditText ET_City = (EditText)findViewById(R.id.ET_City);
EditText ET_Info = (EditText)findViewById(R.id.ET_Info);
Button POP_Country = (Button)findViewById(R.id.POP_Country);
Button POP_Pack = (Button)findViewById(R.id.POP_Pack);
ET_Name.setText("");
ET_Email.setText("");
ET_Mobile.setText("");
ET_City.setText("");
ET_Info.setText("");
POP_Country.setText("");
POP_Pack.setText("");
}
});
dialog.show();
}
/*public void Country_List(View view){
ad.show();
} */
} //Activity ends