我正在尝试为我的ListView创建自定义视图,但尝试打开它时应用程序崩溃了。我阅读了很多页面,但没有找到解决方案。请帮帮我!
这是我的自定义适配器:
public class AdaptadorCatalogo extends BaseAdapter {
protected Activity activity;
protected ArrayList<LineaCatalogo> items;
public AdaptadorCatalogo(Activity activity, ArrayList<LineaCatalogo> items) {
this.activity = activity;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position){
return 0;
}
@Override
public View getView(int position, View contentView, ViewGroup parent) {
View vi=contentView;
if(contentView == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.listitem_catalogo, null);
}
LineaCatalogo item = items.get(position);
TextView primeraLinea = (TextView) vi.findViewById(R.id.lblPrimeraLinea);
primeraLinea.setText(item.getPrimeraLinea());
TextView segundaLinea = (TextView) vi.findViewById(R.id.lblSegundaLinea);
segundaLinea.setText(item.getSegundaLinea());
TextView magnitud = (TextView) vi.findViewById(R.id.lblMagnitud);
magnitud.setText(item.getMagnitud());
return vi;
}
}
这是我的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".listaSismos" >
<RelativeLayout
android:id="@+id/general"
style="@style/AppCentral"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="50dp" >
<TextView
android:id="@+id/titulo_catalogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="@string/esperando_info" />
<ListView
android:id="@+id/listaDatos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:layout_marginLeft="30dp" />
</RelativeLayout>
</RelativeLayout>
这是我的活动:
public class listaSismos extends Activity {
private TextView tituloCatalogo;
private ListView listaDatos;
private String opcion;
private String codigoUsuario;
private String latitud;
private String longitud;
private String idioma;
private int difHoraria;
private ArrayList<LineaCatalogo> Catalogo;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.lista_sismos);
tituloCatalogo = (TextView)findViewById(R.id.titulo_catalogo);
listaDatos = (ListView)findViewById(R.id.listaDatos);
Bundle bundle = getIntent().getExtras();
opcion = bundle.getString("lista");
codigoUsuario = bundle.getString("codigoUsuario");
latitud = bundle.getString("latitud");
longitud = bundle.getString("longitud");
idioma = getResources().getConfiguration().locale.toString();
TimeZone defaultTZ = TimeZone.getDefault();
difHoraria = defaultTZ.getRawOffset() / 1000;
ListarCatalogo conexion = new ListarCatalogo();
conexion.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class ListarCatalogo extends AsyncTask<String,Integer,Boolean> {
private int contador;
protected Boolean doInBackground(String... params) {
boolean resul = true;
HttpClient httpClient = new DefaultHttpClient();
HttpGet del =
new HttpGet(my_app_server_url);
del.setHeader("content-type", "application/json");
try
{
HttpResponse resp = httpClient.execute(del);
String respStr = EntityUtils.toString(resp.getEntity());
JSONArray respJSON = new JSONArray(respStr);
ArrayList<LineaCatalogo> Catalogo = new ArrayList<LineaCatalogo>();
contador = respJSON.length();
for(int cont=0; cont<contador; cont++)
{
JSONObject obj = respJSON.getJSONObject(cont);
String tituloS = obj.getString("P");
String subtituloS = obj.getString("S");
String magnitudS = obj.getString("M");
Catalogo.add(new LineaCatalogo(tituloS, subtituloS, magnitudS));
}
}
catch(Exception ex)
{
resul = false;
}
return resul;
}
protected void onPostExecute(Boolean result) {
tituloCatalogo.setText(R.string.titulo_catalogo);
if (result)
{
//Rellenamos la lista con los resultados
AdaptadorCatalogo adaptador = new AdaptadorCatalogo(listaSismos.this, Catalogo);
listaDatos.setAdapter(adaptador);
tituloCatalogo.setText("OK");
} else {
tituloCatalogo.setText(R.string.no_datos);
}
}
}
}
如果我评论“listaDatos.setAdapter(adaptador);”这一行,该应用程序运行正常。如果没有,我得到这个LogCat:
12-06 09:34:32.443: W/dalvikvm(903): threadid=1: thread exiting with uncaught exception (group=0x41465700)
12-06 09:34:32.559: E/AndroidRuntime(903): FATAL EXCEPTION: main
12-06 09:34:32.559: E/AndroidRuntime(903): java.lang.NullPointerException
12-06 09:34:32.559: E/AndroidRuntime(903): at com.tapsistemas.avcanquake.AdaptadorCatalogo.getCount(AdaptadorCatalogo.java:23)
12-06 09:34:32.559: E/AndroidRuntime(903): at android.widget.ListView.setAdapter(ListView.java:463)
12-06 09:34:32.559: E/AndroidRuntime(903): at com.tapsistemas.avcanquake.listaSismos$ListarCatalogo.onPostExecute(listaSismos.java:124)
12-06 09:34:32.559: E/AndroidRuntime(903): at com.tapsistemas.avcanquake.listaSismos$ListarCatalogo.onPostExecute(listaSismos.java:1)
12-06 09:34:32.559: E/AndroidRuntime(903): at android.os.AsyncTask.finish(AsyncTask.java:631)
12-06 09:34:32.559: E/AndroidRuntime(903): at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-06 09:34:32.559: E/AndroidRuntime(903): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-06 09:34:32.559: E/AndroidRuntime(903): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 09:34:32.559: E/AndroidRuntime(903): at android.os.Looper.loop(Looper.java:137)
12-06 09:34:32.559: E/AndroidRuntime(903): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-06 09:34:32.559: E/AndroidRuntime(903): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 09:34:32.559: E/AndroidRuntime(903): at java.lang.reflect.Method.invoke(Method.java:525)
12-06 09:34:32.559: E/AndroidRuntime(903): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-06 09:34:32.559: E/AndroidRuntime(903): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-06 09:34:32.559: E/AndroidRuntime(903): at dalvik.system.NativeStart.main(Native Method)
我仍在工作一周寻找解决方案,但我无法做到。请帮我。我的英语很差,对不起,我是加那利群岛的西班牙人。
答案 0 :(得分:2)
声明并初始化为
private ArrayList<LineaCatalogo> Catalogo= new ArrayList<LineaCatalogo>();
@Override
public void onCreate(Bundle savedInstanceState){
删除
ArrayList<LineaCatalogo> Catalogo = new ArrayList<LineaCatalogo>();
// becomes local to doInbackground as it is declared and initialized there.
doInbackground
中的。
在您的情况下,Catalogo
为空。
答案 1 :(得分:1)
您使用
重新定义列表ArrayList<LineaCatalogo> Catalogo = new ArrayList<LineaCatalogo>();
在doInBacground中
仅使用
Catalogo = new ArrayList<LineaCatalogo>();