ciao a tutti..sto creando un adapter personalizzato che mi visualizzi in una listView un elenco di articoli che hanno un Titolo, una categoria e una data..Il problema è che l'applicazione mi si blocca all'inizio e non riesco a capire il perchè..cmq vi posto il codice e il LogCat:
Main.xml:
activity_lista_activity__row__article.xml :
ArticolesInfo.java:
ArticleAdapter.java:
MainActivity.java:
Ora il problema è che l'emulatore parte, poi quando clicco sulla App mi dice che "Sfortunatamente l'app è stata Stoppata"
Questo è il LogCat:
Grazie in anticipo a chi può aiutarmi
Main.xml:
Codice:
<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"
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.custom_adapter.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
activity_lista_activity__row__article.xml :
Codice:
<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="200dp"
tools:context="com.example.custom_adapter.ListaActivity_Row_Article"
android:padding="10dp"
android:descendantFocusability="blocksDescendants"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:orientation="vertical"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="@style/big_textstyle"
android:id="@+id/txt_Titolo"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="@style/small_textstyle"
android:id="@+id/txt_Categoria"/>
</LinearLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="@style/small_textstyle"
android:maxLength="6"
android:id="@+id/txt_Data"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
ArticolesInfo.java:
Codice:
package com.example.custom_adapter;
import java.util.Date;
public class ArticolesInfo {
private String titolo;
private String categoria;
private Date data;
public ArticolesInfo(String titolo, String categoria, Date data){
this.titolo=titolo;
this.categoria=categoria;
this.data=data;
}
public void setTitolo(String t){
this.titolo=t;
}
public void setCategoria(String c){
this.categoria=c;
}
public void setData(Date d){
this.data=d;
}
public String getTitolo(){
return titolo;
}
public String getCategoria(){
return categoria;
}
public Date getData(){
return data;
}
}
ArticleAdapter.java:
Codice:
package com.example.custom_adapter;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ArticleAdapter extends ArrayAdapter<ArticolesInfo> {
private List<ArticolesInfo> articles=null;
private Context context=null;
private TextView txt;
private SimpleDateFormat simple = new SimpleDateFormat("dd/MM",Locale.ITALIAN);
public ArticleAdapter (Context context, int textViewResourceId,List<ArticolesInfo> objects){
super(context, textViewResourceId, objects);
}
@Override
public int getCount(){
return articles.size();
}
public ArticolesInfo getItem(int position){
return articles.get(position);
}
public long getItemId(int position){
return getItem(position).hashCode();
}
public View getView(int position,View v, ViewGroup vg){
if (v==null){
v=LayoutInflater.from(context).inflate(R.layout.activity_lista_activity__row__article, null);
/* Qui mi da un warning di questo tipo:"Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)" */
}
ArticolesInfo ai= (ArticolesInfo) getItem(position);
txt=(TextView) v.findViewById(R.id.txt_Titolo);
txt.setText(ai.getTitolo());
txt=(TextView) v.findViewById(R.id.txt_Categoria);
txt.setText(ai.getCategoria());
txt=(TextView) v.findViewById(R.id.txt_Data);
txt.setText(simple.format(ai.getData()));
return v;
}
}
MainActivity.java:
Codice:
package com.example.custom_adapter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
private ArticleAdapter adapter = new ArticleAdapter(this, R.layout.activity_lista_activity__row__article, generateNews());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);
}
private List<ArticolesInfo> generateNews(){
ArrayList<ArticolesInfo> list= new ArrayList<ArticolesInfo>();
Calendar c=Calendar.getInstance();
//popolamento articoli Fittizi
c.set(2014,3,12);
ArticolesInfo tmp=new ArticolesInfo("Intent e Messaggi","Android Java",new Date(c.getTimeInMillis()));
list.add(tmp);
tmp.setTitolo("Ciclo di vita di una'Activity");
tmp.setCategoria("Android");
c.set(2014,1,12);
tmp.setData(new Date(c.getTimeInMillis()));
list.add(tmp);
tmp.setTitolo("Costruire un Array di Stringhe");
tmp.setCategoria("Java Android");
c.set(2014,7,10);
tmp.setData(new Date(c.getTimeInMillis()));
list.add(tmp);
return list;
}
}
Ora il problema è che l'emulatore parte, poi quando clicco sulla App mi dice che "Sfortunatamente l'app è stata Stoppata"
Questo è il LogCat:
Codice:
12-05 12:31:50.245: E/Trace(782): error opening trace file: No such file or directory (2)
12-05 12:31:50.835: D/AndroidRuntime(782): Shutting down VM
12-05 12:31:50.835: W/dalvikvm(782): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-05 12:31:50.855: E/AndroidRuntime(782): FATAL EXCEPTION: main
12-05 12:31:50.855: E/AndroidRuntime(782): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.custom_adapter/com.example.custom_adapter.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
12-05 12:31:50.855: E/AndroidRuntime(782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
12-05 12:31:50.855: E/AndroidRuntime(782): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-05 12:31:50.855: E/AndroidRuntime(782): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-05 12:31:50.855: E/AndroidRuntime(782): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-05 12:31:50.855: E/AndroidRuntime(782): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 12:31:50.855: E/AndroidRuntime(782): at android.os.Looper.loop(Looper.java:137)
12-05 12:31:50.855: E/AndroidRuntime(782): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-05 12:31:50.855: E/AndroidRuntime(782): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 12:31:50.855: E/AndroidRuntime(782): at java.lang.reflect.Method.invoke(Method.java:511)
12-05 12:31:50.855: E/AndroidRuntime(782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-05 12:31:50.855: E/AndroidRuntime(782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-05 12:31:50.855: E/AndroidRuntime(782): at dalvik.system.NativeStart.main(Native Method)
12-05 12:31:50.855: E/AndroidRuntime(782): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
12-05 12:31:50.855: E/AndroidRuntime(782): at android.app.Activity.getSystemService(Activity.java:4410)
12-05 12:31:50.855: E/AndroidRuntime(782): at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
12-05 12:31:50.855: E/AndroidRuntime(782): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
12-05 12:31:50.855: E/AndroidRuntime(782): at com.example.custom_adapter.ArticleAdapter.<init>(ArticleAdapter.java:22)
12-05 12:31:50.855: E/AndroidRuntime(782): at com.example.custom_adapter.MainActivity.<init>(MainActivity.java:14)
12-05 12:31:50.855: E/AndroidRuntime(782): at java.lang.Class.newInstanceImpl(Native Method)
12-05 12:31:50.855: E/AndroidRuntime(782): at java.lang.Class.newInstance(Class.java:1319)
12-05 12:31:50.855: E/AndroidRuntime(782): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
12-05 12:31:50.855: E/AndroidRuntime(782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
12-05 12:31:50.855: E/AndroidRuntime(782): ... 11 more
Grazie in anticipo a chi può aiutarmi