我正在尝试使用这种规范调用或查看 ListView:
这是我的代码。这是名为 EducacionFragment 的片段,在这里我想将布局称为“eventos”:
public class EducacionFragment extends ListFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.eventos_layout, container, false);
//((MainActivity) getActivity()).getSupportActionBar().setTitle("Eventos");
//Elementos_Eventos elementos_eventos = new Elementos_Eventos();
ListView listview =(ListView)view.findViewById(R.id.listView2);
Adaptador_Eventos adaptador = new Adaptador_Eventos(getActivity());
//adaptador = new Adaptador_Eventos(getActivity());
listview.setAdapter(adaptador);
return view;
}
}'
这是我的名为 Elementos_Eventos 的类,我从数据库中获取所有信息,并放入一个列表中:
public class Elementos_Eventos {
//URL to get JSON Array
private static String url = "http://ucwm.co.nf/Evento_app.php";
//JSON Node Names
private static final String TAG_EVENTOS = "eventos";
private static final String TAG_ID = "idEvento";
private static final String TAG_NAME = "Nombre";
private static final String TAG_DESCRIP = "Descripcion";
private static final String TAG_FECHAI = "FechaInicio";
private static final String TAG_HORA = "Hora";
private static final String TAG_FECHAF = "FechaFinal";
private static final String TAG_LUGAR = "Lugar";
private static JSONArray eventos = null;
public static List<Elemento_Eventos> listaElementos = elementos();
public Elementos_Eventos() {
listaElementos = elementos();
}
static Elemento_Eventos elemento(int id) {
return listaElementos.get(id);
}
public static ArrayList<Elemento_Eventos> elementos() {
JSONParser jParser = new JSONParser();
JSONObject json;
// Getting JSON from URL
json = jParser.getJSONFromUrl(url);
ArrayList<Elemento_Eventos> elementos;
elementos = null;
try {
// Getting JSON Array from URL
eventos = json.getJSONArray(TAG_EVENTOS);
elementos = new ArrayList<Elemento_Eventos>();
for (int i = 0; i < eventos.length(); i++) {
JSONObject c = eventos.getJSONObject(i);
// Storing JSON item in a Variable
String id = c.getString(TAG_ID);
String nombre = c.getString(TAG_NAME);
String descripcion = c.getString(TAG_DESCRIP);
String fechaInicio = c.getString(TAG_FECHAI);
String hora = c.getString(TAG_HORA);
String fechaFinal = c.getString(TAG_FECHAF);
String lugar = c.getString(TAG_LUGAR);
elementos.add(new Elemento_Eventos(id, nombre,descripcion, fechaInicio, hora, fechaFinal, lugar));
}
}
catch (JSONException e) {
e.printStackTrace();
}
return elementos;
}
public List<String> listaElementos()
{
ArrayList<String> todos = new ArrayList<String>();
for (Elemento_Eventos e:listaElementos)
todos.add(e.getNombre());
return todos;
}
public static int size() {
return listaElementos.size();
}
这是我的名为 Elemento_Eventos 的类,我在其中一一声明了一个元素:
public class Elemento_Eventos {
private String id;
private String nombre;
private String descripcion;
private String feInicio;
private String hora;
private String feFinal;
private String lugar;
public Elemento_Eventos(String id,String nombre, String descripcion, String feInicio, String hora, String feFinal, String lugar){
setId(id);
setNombre(nombre);
setDescripcion(descripcion);
setFeInicio(feInicio);
setHora(hora);
setFeFinal(feFinal);
setLugar(lugar);
}
//*********INICIO DE LOS SETS******************
public void setId(String id){
this.id=id;
}
public void setNombre(String nombre){
this.nombre=nombre;
}
public void setDescripcion(String descripcion){
this.descripcion=descripcion;
}
public void setFeInicio(String feInicio){
this.feInicio=feInicio;
}
public void setHora(String hora){
this.hora=hora;
}
public void setFeFinal(String feFinal){
this.feFinal=feFinal;
}
public void setLugar(String lugar){
this.lugar=lugar;
}
//*********FINAL DE LOS SETS******************
//*********INICIO DE LOS GETS******************
public String getId(){
return id;
}
public String getNombre(){
return nombre;
}
public String getDescripcion(){
return descripcion;
}
public String getFeInicio(){
return feInicio;
}
public String getHora(){
return hora;
}
public String getFeFinal(){
return feFinal;
}
public String getLugar(){
return lugar;
}
//*********FINAL DE LOS SETS******************
}
最后是我的类 Adaptador_Eventos,我将所有这些信息放在 textViews 中:
public class Adaptador_Eventos extends BaseAdapter{
private final Activity actividad;
public Adaptador_Eventos(Activity actividad) {
super();
this.actividad = actividad;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Elemento_Eventos elemento = Elementos_Eventos.elemento(position);
LayoutInflater inflater = actividad.getLayoutInflater();
View view = inflater.inflate(R.layout.elemento_eventos, null,true);
TextView nombreDelEvento, lugar, fecha, descripcion;
ImageView logo_tipo;
nombreDelEvento = (TextView) view.findViewById(R.id.nom_evento);
lugar = (TextView) view.findViewById(R.id.lugar_evento);
fecha = (TextView) view.findViewById(R.id.fecha_evento);
descripcion = (TextView) view.findViewById(R.id.desc_evento);
logo_tipo = (ImageView) view.findViewById(R.id.imageView);
nombreDelEvento.setText(elemento.getNombre());
lugar.setText(elemento.getLugar());
fecha.setText(elemento.getFeInicio());
descripcion.setText(elemento.getDescripcion());
int id = R.drawable.kcc;
logo_tipo.setImageResource(id);
logo_tipo.setScaleType(ImageView.ScaleType.FIT_END);
return view;
}
@Override
public int getCount() {
return Elementos_Eventos.size();
}
@Override
public Object getItem(int position) {
return Elementos_Eventos.elemento(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
当我调用片段 Educacion 时,它会使我的应用程序因消息而崩溃
不幸的是,Unidas Contigo AC 已停止
这是出现的堆栈跟踪:
11-12 05:10:00.279 7414-7414/? D/dalvikvm? Late-enabling CheckJNI
11-12 05:10:01.059 7414-7414/com.example.juanisaac.unidascontigoac W/dalvikvm? VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
11-12 05:10:01.095 7414-7414/com.example.juanisaac.unidascontigoac I/dalvikvm? Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
11-12 05:10:01.107 7414-7414/com.example.juanisaac.unidascontigoac W/dalvikvm? VFY: unable to resolve interface method 15449: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
11-12 05:10:01.119 7414-7414/com.example.juanisaac.unidascontigoac D/dalvikvm? VFY: replacing opcode 0x72 at 0x0002
11-12 05:10:01.123 7414-7414/com.example.juanisaac.unidascontigoac I/dalvikvm? Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
11-12 05:10:01.139 7414-7414/com.example.juanisaac.unidascontigoac W/dalvikvm? VFY: unable to resolve interface method 15453: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
11-12 05:10:01.139 7414-7414/com.example.juanisaac.unidascontigoac D/dalvikvm? VFY: replacing opcode 0x72 at 0x0002
11-12 05:10:01.407 7414-7414/com.example.juanisaac.unidascontigoac I/AppCompatViewInflater? app:theme is now deprecated. Please move to using android:theme instead.
11-12 05:10:01.419 7414-7414/com.example.juanisaac.unidascontigoac I/dalvikvm? Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
11-12 05:10:01.423 7414-7414/com.example.juanisaac.unidascontigoac W/dalvikvm? VFY: unable to resolve virtual method 426: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-12 05:10:01.423 7414-7414/com.example.juanisaac.unidascontigoac D/dalvikvm? VFY: replacing opcode 0x6e at 0x0002
11-12 05:10:01.431 7414-7414/com.example.juanisaac.unidascontigoac I/dalvikvm? Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
11-12 05:10:01.439 7414-7414/com.example.juanisaac.unidascontigoac W/dalvikvm? VFY: unable to resolve virtual method 448: Landroid/content/res/TypedArray;.getType (I)I
11-12 05:10:01.439 7414-7414/com.example.juanisaac.unidascontigoac D/dalvikvm? VFY: replacing opcode 0x6e at 0x0002
11-12 05:10:01.479 7414-7414/com.example.juanisaac.unidascontigoac I/AppCompatViewInflater? app:theme is now deprecated. Please move to using android:theme instead.
11-12 05:10:01.547 7414-7416/com.example.juanisaac.unidascontigoac D/dalvikvm? GC_CONCURRENT freed 192K, 3% free 8841K/9068K, paused 13ms+0ms, total 26ms
11-12 05:10:01.631 7414-7414/com.example.juanisaac.unidascontigoac D/dalvikvm? GC_FOR_ALLOC freed 5K, 3% free 8858K/9068K, paused 13ms, total 14ms
11-12 05:10:01.707 7414-7414/com.example.juanisaac.unidascontigoac I/dalvikvm-heap? Grow heap (frag case) to 10.120MB for 1517220-byte allocation
11-12 05:10:01.719 7414-7424/com.example.juanisaac.unidascontigoac D/dalvikvm? GC_FOR_ALLOC freed <1K, 3% free 10340K/10552K, paused 14ms, total 14ms
11-12 05:10:01.739 7414-7416/com.example.juanisaac.unidascontigoac D/dalvikvm? GC_CONCURRENT freed 0K, 3% free 10340K/10552K, paused 8ms+0ms, total 11ms
11-12 05:10:01.759 7414-7414/com.example.juanisaac.unidascontigoac D/dalvikvm? GC_FOR_ALLOC freed <1K, 3% free 10339K/10552K, paused 6ms, total 6ms
11-12 05:10:01.875 7414-7414/com.example.juanisaac.unidascontigoac I/dalvikvm-heap? Grow heap (frag case) to 15.908MB for 6068844-byte allocation
11-12 05:10:01.887 7414-7424/com.example.juanisaac.unidascontigoac D/dalvikvm? GC_FOR_ALLOC freed <1K, 2% free 16266K/16480K, paused 12ms, total 12ms
11-12 05:10:01.919 7414-7416/com.example.juanisaac.unidascontigoac D/dalvikvm? GC_CONCURRENT freed <1K, 2% free 16266K/16480K, paused 7ms+0ms, total 20ms
11-12 05:10:02.051 7414-7414/com.example.juanisaac.unidascontigoac D/libEGL? loaded /system/lib/egl/libEGL_genymotion.so
11-12 05:10:02.067 7414-7414/com.example.juanisaac.unidascontigoac D/? HostConnection::get() New Host Connection established 0xb92c56a8, tid 7414
11-12 05:10:02.087 7414-7414/com.example.juanisaac.unidascontigoac D/libEGL? loaded /system/lib/egl/libGLESv1_CM_genymotion.so
11-12 05:10:02.087 7414-7414/com.example.juanisaac.unidascontigoac D/libEGL? loaded /system/lib/egl/libGLESv2_genymotion.so
11-12 05:10:02.147 7414-7414/com.example.juanisaac.unidascontigoac W/EGL_genymotion? eglSurfaceAttrib not implemented
11-12 05:10:02.171 7414-7414/com.example.juanisaac.unidascontigoac D/OpenGLRenderer? Enabling debug mode 0
11-12 05:10:04.511 7414-7414/com.example.juanisaac.unidascontigoac I/Choreographer? Skipped 46 frames! The application may be doing too much work on its main thread.
11-12 05:10:05.599 7414-7414/com.example.juanisaac.unidascontigoac W/dalvikvm? Exception Landroid/os/NetworkOnMainThreadException; thrown while initializing Lcom/example/juanisaac/unidascontigoac/Elementos_Eventos;
11-12 05:10:05.599 7414-7414/com.example.juanisaac.unidascontigoac D/AndroidRuntime? Shutting down VM
11-12 05:10:05.603 7414-7414/com.example.juanisaac.unidascontigoac W/dalvikvm? threadid=1: thread exiting with uncaught exception (group=0xa620e908)
11-12 05:10:05.615 7414-7414/com.example.juanisaac.unidascontigoac E/AndroidRuntime? FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at com.example.juanisaac.unidascontigoac.Adaptador_Eventos.getCount(Adaptador_Eventos.java:57)
at android.widget.ListView.setAdapter(ListView.java:462)
at com.example.juanisaac.unidascontigoac.Fragments.EducacionFragment.onCreateView(EducacionFragment.java:40)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.example.juanisaac.unidascontigoac.JSONParser.getJSONFromUrl(JSONParser.java:37)
at com.example.juanisaac.unidascontigoac.Elementos_Eventos.elementos(Elementos_Eventos.java:55)
at com.example.juanisaac.unidascontigoac.Elementos_Eventos.<clinit>(Elementos_Eventos.java:39)
at com.example.juanisaac.unidascontigoac.Adaptador_Eventos.getCount(Adaptador_Eventos.java:57)
at android.widget.ListView.setAdapter(ListView.java:462)
at com.example.juanisaac.unidascontigoac.Fragments.EducacionFragment.onCreateView(EducacionFragment.java:40)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
11-12 05:10:08.059 7414-7414/com.example.juanisaac.unidascontigoac I/Process? Sending signal. PID: 7414 SIG: 9