I’m stuck. I’m tring to fill a listview with fields of a SQLite table. I’m quite new to programming Android. Any advice on why I am getting this error:
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dvd.clase/com.dvd.clase.Lista_Classes}: java.lang.NullPointerException
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at android.app.ActivityThread.access$2200(ActivityThread.java:126)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at android.os.Looper.loop(Looper.java:123)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at android.app.ActivityThread.main(ActivityThread.java:4595)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at java.lang.reflect.Method.invoke(Method.java:521)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at dalvik.system.NativeStart.main(Native Method)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): Caused by: java.lang.NullPointerException
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at com.dvd.clase.Lista_Classes.onCreate(Lista_Classes.java:72)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
10-24 10:24:31.154: ERROR/AndroidRuntime(1298): ... 11 more
with this code (I think the problem is here and not in the SQL query, but I might be wrong):
public class Lista_Classes extends ListActivity{
BaseDatosHelper miBBDDHelper;
private class ClasseAdapter extends ArrayAdapter<Classe> {
ArrayList<Classe> items;
public ClasseAdapter(Context context, int textViewResourceId, ArrayList<Classe> items)
{
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.lista_item, null);
}
Classe classe = items.get(position);
//if (classe != null) {
TextView tnClasse = (TextView) v.findViewById(R.id.numClasse);
TextView tColor = (TextView) v.findViewById(R.id.Color);
if (tnClasse != null) {
tnClasse.setText(classe.GetNumClase());
}
if (tColor != null) {
tColor.setText(classe.GetColor());
}
//}
return v;
}
}
public void crearBBDD() {
miBBDDHelper = new BaseDatosHelper(this);
try {
miBBDDHelper.crearDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
crearBBDD();
ArrayList<Classe> classes = getItems();
setListAdapter(new ClasseAdapter(this, R.layout.lista_item, classes));
Button home= (Button) this.findViewById(R.id.boton_home);
home.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
Intent myIntent = new Intent(view.getContext(), main.class);
startActivityForResult(myIntent, 0);
}
});
}
public ArrayList<Classe> getItems() {
miBBDDHelper.abrirBaseDatos();
ArrayList<Classe> listaClasses = miBBDDHelper.GetLlistaClasses();
miBBDDHelper.close();
return listaClasses;
}
}
Thanks for your advice.
You are calling
findViewByIdbut you never set the content view. You have to putsetContentView(R.layout.yourlayout)in your onCreate method after you callsuper. This is why your home button is null.