请帮帮我,我想在我点击ListView时动作会显示ImageDialog,但是当我运行应用程序时,出现如下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bongkorr/com.hospifinder.A1}: java.lang.NullPointerException
这是我的.java文件:
public class Informasi extends Activity {
protected static final ItemDetails ItemDetails = null;
protected Object Object;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.informasi);
ArrayList<ItemDetails> image_details = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.listV_main);
lv1.setAdapter(new ItemListBaseAdapter(this, image_details));
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent i = null;
if (position == 0){
i = new Intent(Informasi.this,com.hospifinder.A1.class);
}
else if(position == 1){
i = new Intent(Informasi.this,com.hospifinder.A1A.class);
}
else if(position == 2){
i = new Intent(Informasi.this,com.hospifinder.A2.class);
}
else if(position == 3){
i = new Intent(Informasi.this,com.hospifinder.A3.class);
}
else if(position == 4){
i = new Intent(Informasi.this,com.hospifinder.A4.class);
}
else if(position == 5){
i = new Intent(Informasi.this,com.hospifinder.A5.class);
}
else if(position == 6){
i = new Intent(Informasi.this,com.hospifinder.A6.class);
}
else if(position == 7){
i = new Intent(Informasi.this,com.hospifinder.A7.class);
}
else if(position == 8){
i = new Intent(Informasi.this,com.hospifinder.A7A.class);
}
else if(position == 9){
i = new Intent(Informasi.this,com.hospifinder.A8.class);
}
else if(position == 10){
i = new Intent(Informasi.this,com.hospifinder.A9.class);
}
else if(position == 11){
i = new Intent(Informasi.this,com.hospifinder.A10.class);
}
else if(position == 12){
i = new Intent(Informasi.this,com.hospifinder.A11.class);
}
else if(position == 13){
i = new Intent(Informasi.this,com.hospifinder.A12.class);
}
else if(i !=null)
startActivity(i);
}
});
}
private ArrayList<ItemDetails> GetSearchResults(){
ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();
ItemDetails item_details = new ItemDetails();
item_details.setName("RS SARI ASIH KARAWACI");
item_details.setItemDescription("Jl. Imam Bonjol No.38 (Pos Polisi) Kota Tangerang ");
item_details.setImageNumber(1); //1a
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RS SARI ASIH CILEDUG");
item_details.setItemDescription("Jl. HOS. Cokroaminoto No.38 (Depan Mall Ramayana) Ciledung Kota Tangerang");
item_details.setImageNumber(2); //1
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RSU KAB TANGERANG");
item_details.setItemDescription("Jl. Jend. Ahmad Yani No. 9 Tangerang");
item_details.setImageNumber(3); //2
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RS USADA INSANI");
item_details.setItemDescription("Jl. KH. Hasyim Ashari No. 24 Cipondoh-Tangerang");
item_details.setImageNumber(4); //3
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RS AN-NISA");
item_details.setItemDescription("Jl. Gatot Subroto No. 96 Cibodas sari Km 3 Cibodas, Tangerang ");
item_details.setImageNumber(5); //4
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RS DINDA");
item_details.setItemDescription("Jl. Galuh No.2, Jatiuwung, Tangerang ");
item_details.setImageNumber(6); //5
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RS BHAKTI ASIH");
item_details.setItemDescription("Jl. Raden Saleh No.10 Karang Tengah Ciledug, Kota Tangerang");
item_details.setImageNumber(7); //6
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RS ISLAM SARI ASIH AR-RAHMAH");
item_details.setItemDescription("Jalan Ks Tubun No. 44Kwang Jaya, Karawaci, Tangerang");
item_details.setImageNumber(8); //7
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RSK SITANALA");
item_details.setItemDescription("Jl. Dr Sitanala, Tangerang");
item_details.setImageNumber(9); //7a
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RSU HERMINA TANGERANG");
item_details.setItemDescription("Jl. KS Tubun No.10 Pasar Baru, Tangerang ");
item_details.setImageNumber(10); //8
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RS. ARIYA MEDIKA");
item_details.setItemDescription("Jl. Industri Raya I Blok A. No. 1-11, Jatake - Kodya Tangerang");
item_details.setImageNumber(11); //9
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RS MELATI");
item_details.setItemDescription("Jl. Merdeka No.92, Kota Tangerang");
item_details.setImageNumber(12); //10
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RS AWAL BROSS");
item_details.setItemDescription("Jl. MH. Thamrin No.3 Kebon Nanas, Cikokol, Tangerang");
item_details.setImageNumber(13); //11
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RSUD Kota Tangerang");
item_details.setItemDescription("Jl. Pulau Putri Raya, Kel. Kelapa Indah Kec. Tangerang");
item_details.setImageNumber(14); //12
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RUMAH SAKIT SELARAS");
item_details.setItemDescription("Jl. Raya Lapan Rumpin, Suradita, Cisauk Suradita, Tangerang");
item_details.setImageNumber(15); //13
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RSIA SELARAS");
item_details.setItemDescription("JL. Raya Serang, Km 185, Bojong, Cikupa, Talaga, Tangerang");
item_details.setImageNumber(16); //14
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("CIPUTRA HOSPITAL");
item_details.setItemDescription("Mekar Bakti, Panongan,Tangerang, Banten");
item_details.setImageNumber(17); //15
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RS MITRA HUSADA");
item_details.setItemDescription("Jl. Kp. Melayu Barat, No. 11A, Teluk Naga, Kec. Tangerang");
item_details.setImageNumber(18); //16
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RUMAH SAKIT MULIA INSANI");
item_details.setItemDescription("Jalan Raya Serang KM 16,8, Cikupa, Banten");
item_details.setImageNumber(19); //17
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RS PARAMITA TANGERANG");
item_details.setItemDescription("JL Raya Serang, Km. 28, 5 Kavling FL No. 8, Balaraja, Kawasan Industri CCM");
item_details.setImageNumber(20); //18
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RUMAH SAKIT QADR");
item_details.setItemDescription("Komplek Islamic Village, Karawaci, Kec. Tangerang, Banten");
item_details.setImageNumber(21); //19
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("SILOAM HOSPITALS LIPPO VILLAGE");
item_details.setItemDescription("Jl. Siloam No. 6, Lippo Karawaci 1600, Kec. Tangerang, Banten");
item_details.setImageNumber(22); //20
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RSUD BALARAJA");
item_details.setItemDescription("Jl. Rumah Sakit No. 88, Desa Tobat, Balaraja, Banten");
item_details.setImageNumber(23);//21
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("EKA HOSPITAL CENTRAL BUSINESS DISTRICT (CBD)");
item_details.setItemDescription("Lot IX BSD City Serpong");
item_details.setImageNumber(24);//22
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RUMAH SAKIT OMNI INTERNATIONAL");
item_details.setItemDescription("Jl. Alam Sutera Boulevard Pondok Jagung, Tangerang");
item_details.setImageNumber(25);//23
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RUMAH SAKIT INTERNATIONAL BINTARO");
item_details.setItemDescription("Jl. MH. Thamrin Blok B3 No. 1 Sektor 7 Bintaro Jaya");
item_details.setImageNumber(26);//24
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RSIA ICHSAN MEDICAL CENTER");
item_details.setItemDescription("Jl. Jombang Raya No 56 Bintaro Sektor IX");
item_details.setImageNumber(27);//25
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RUMAH SAKIT MEDIKA");
item_details.setItemDescription("BSD City, Tangerang");
item_details.setImageNumber(28);//26
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RUMAH SAKIT ISLAM ASSHOBIRIN");
item_details.setItemDescription("Jl. Raya Serpong KM.11 Pondok Jagung");
item_details.setImageNumber(29);//27
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RSIA BUAH HATI");
item_details.setItemDescription("Jl. Aria Putra Ciputat");
item_details.setImageNumber(30);//28
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RSIA BUAH HATI");
item_details.setItemDescription("Jl. Siliwangi Pamulang");
item_details.setImageNumber(31);//29
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RUMAH SAKIT BHINEKA BAKTI HUSADA");
item_details.setItemDescription("Jl. Cabe Raya No.17/34, Pamulang, Tangerang Selatan, Banten");
item_details.setImageNumber(32);//30
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RUMAH SAKIT HERMINA");
item_details.setItemDescription("Jl.Kertamukti Ciputat Timur");
item_details.setImageNumber(33);//31
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RUMAH SAKIT SYARIF HIDAYATULLAH");
item_details.setItemDescription("Jl. Ir. H. Juanda No.95 Kec. Ciputat, Tangerang");
item_details.setImageNumber(34);//32
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("RUMAH SAKIT SAKIT SARI ASIH CIPUTAT");
item_details.setItemDescription("Jl. Otista Sasak Tinggi No. 3, Ciputat");
item_details.setImageNumber(35);//33
results.add(item_details);
return results;
}
}
public class A1 extends Activity {
private ListView lv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.informasi);
lv = (ListView) findViewById(R.id.lv_data);
arrayDialog.add("Dialog");
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, arrayDialog));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Dialog dialog = new Dialog(A1.this);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(A1.this);
//setting custom layout to dialog
LayoutInflater inflater = A1.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom_alert_dialog, null);
dialogBuilder.setView(dialogView);
//adding text dynamically
TextView txt = (TextView) dialogView.findViewById(R.id.textView);
txt.setText("Put your dialogView text here.");
ImageView image = (ImageView)dialogView.findViewById(R.id.image);
image.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_dialog_info));
//adding button click event
Button dismissButton = (Button) dialogView.findViewById(R.id.button);
dismissButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignRight="@+id/button"
android:contentDescription="@drawable/a1"
android:src="@drawable/a1" />
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true"
android:text="Dismiss" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="@+id/button"
android:layout_toRightOf="@+id/image"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#0033CC"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/listV_main"/>
</LinearLayout>
我的代码有问题吗?是否因为ListView如此有影响而且出现错误?
答案 0 :(得分:0)
A1.java
中的您需要使用setOnItemClickListener
而不是setOnClickListener
,因为您正在尝试将onClickListener设置为listView,因为listView应该附加到adapterView,它会抛出一个例外。列表视图没有附加适配器。
单击listView就像点击一个没用的整个View。而是应该处理listView的各个项目的点击。
尝试这样的事情:
suggestionsList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do your stuff to show the dialog
}
});
OnItemClickListener()
应为android.widget.AdaptorView.OnItemClickListener
对于alertDialog,请按照此处的代码进行操作:http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout
修改
我为AlertDialog尝试了这个代码,它适用于我
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(A1.this);
// setting custom layout to dialog
LayoutInflater inflater = A1.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog, null);
dialogBuilder.setView(dialogView);
// adding text dynamically
TextView txt = (TextView) dialogView.findViewById(R.id.textView);
txt.setText("Put your dialogView text here.");
ImageView image = (ImageView) dialogView.findViewById(R.id.image);
image.setImageDrawable(getResources()
.getDrawable(android.R.drawable.ic_dialog_info));
// adding button click event
Button dismissButton = (Button) dialogView.findViewById(R.id.button);
dismissButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog = dialogBuilder.create();
alertDialog.show();
alertDialog
是类中的成员变量。
AlertDialog alertDialog;