Android NullPointerException我可以´;不要加载列表视图。失去了两天,请帮帮我
LogCatAndroid NullPointerException我可以´;不要加载列表视图。失去了两天,请帮帮我,android,listview,nullpointerexception,Android,Listview,Nullpointerexception,LogCat 02-20 21:43:55.316: E/salida cursor(1564): recorrido finalizado 02-20 21:43:55.316: D/AndroidRuntime(1564): Shutting down VM 02-20 21:43:55.316: W/dalvikvm(1564): threadid=1: thread exiting with uncaught exception (gro
02-20 21:43:55.316: E/salida cursor(1564): recorrido finalizado
02-20 21:43:55.316: D/AndroidRuntime(1564): Shutting down VM
02-20 21:43:55.316: W/dalvikvm(1564): threadid=1: thread exiting with uncaught exception (group=0x40028a00)
02-20 21:43:55.336: E/AndroidRuntime(1564): FATAL EXCEPTION: main
02-20 21:43:55.336: E/AndroidRuntime(1564): java.lang.RuntimeException: Unable to start activity ComponentInfo{es.garman.waypoints/es.garman.waypoints.ListaLugaresActivity}: java.lang.NullPointerException
02-20 21:43:55.336: E/AndroidRuntime(1564): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
02-20 21:43:55.336: E/AndroidRuntime(1564): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
02-20 21:43:55.336: E/AndroidRuntime(1564): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
02-20 21:43:55.336: E/AndroidRuntime(1564): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
02-20 21:43:55.336: E/AndroidRuntime(1564): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 21:43:55.336: E/AndroidRuntime(1564): at android.os.Looper.loop(Looper.java:143)
02-20 21:43:55.336: E/AndroidRuntime(1564): at android.app.ActivityThread.main(ActivityThread.java:4914)
02-20 21:43:55.336: E/AndroidRuntime(1564): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 21:43:55.336: E/AndroidRuntime(1564): at java.lang.reflect.Method.invoke(Method.java:521)
02-20 21:43:55.336: E/AndroidRuntime(1564): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-20 21:43:55.336: E/AndroidRuntime(1564): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-20 21:43:55.336: E/AndroidRuntime(1564): at dalvik.system.NativeStart.main(Native Method)
02-20 21:43:55.336: E/AndroidRuntime(1564): Caused by: java.lang.NullPointerException
02-20 21:43:55.336: E/AndroidRuntime(1564): at es.garman.waypoints.ListaLugaresActivity.cargarLista(ListaLugaresActivity.java:59)
02-20 21:43:55.336: E/AndroidRuntime(1564): at es.garman.waypoints.ListaLugaresActivity.onCreate(ListaLugaresActivity.java:34)
02-20 21:43:55.336: E/AndroidRuntime(1564): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
02-20 21:43:55.336: E/AndroidRuntime(1564): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
02-20 21:43:55.336: E/AndroidRuntime(1564): ... 11 more
02-20 21:44:30.756: I/Process(1564): Sending signal. PID: 1564 SIG: 9
ListaLugaresActivity.java
public class WaypointRegistro {
private int id;
private String nombre;
private String descripcion;
public WaypointRegistro(){}
public WaypointRegistro(int id, String nombre, String descripcion){
setId(id);
setNombre(nombre);
setDescripcion(descripcion);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String email) {
this.descripcion = email;
}
}
WaypointRegistro.java
public class WaypointRegistro {
private int id;
private String nombre;
private String descripcion;
public WaypointRegistro(){}
public WaypointRegistro(int id, String nombre, String descripcion){
setId(id);
setNombre(nombre);
setDescripcion(descripcion);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String email) {
this.descripcion = email;
}
}
lista_fila.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="4dp">
<TextView
android:id="@+id/idLista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="3dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dp">
<TextView
android:id="@+id/nombreLista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
<TextView
android:id="@+id/descripcionLista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="10dp"
android:layout_marginBottom="10dp"
android:text="@string/txt_tituloLista"
android:textColor="#FF4201"
android:textSize="25sp"
android:textStyle="bold"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="fill_parent"
android:padding="5dp">
</ListView>
<TextView
android:id="@+id/android:empty"
android:text="@string/txt_empty"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
</LinearLayout>
lista_pantalla.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="4dp">
<TextView
android:id="@+id/idLista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="3dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dp">
<TextView
android:id="@+id/nombreLista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
<TextView
android:id="@+id/descripcionLista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="10dp"
android:layout_marginBottom="10dp"
android:text="@string/txt_tituloLista"
android:textColor="#FF4201"
android:textSize="25sp"
android:textStyle="bold"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="fill_parent"
android:padding="5dp">
</ListView>
<TextView
android:id="@+id/android:empty"
android:text="@string/txt_empty"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
</LinearLayout>
列表A
为空
将此添加到您的onCreate()
:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.lista_pantalla);
lista=getListView();//您得到的是NullPointerException,因为lista
为空,只需在设置setAdapter之前初始化lista
:
lista=ListaLugaresActivity.this.getListView();
lista.setAdapter(adaptador);
可能是您没有初始化列表视图您的代码可读性很好,例如:das was icher sage,denn deine variablen namen sind einfach für keinen verständlich。
只要您不懂德语,您就无法理解我,那么您希望这里的人怎么理解您的变量/类名?