我被困住了。我想用一个SQLite表的字段填充listview。我是编程Android的新手。关于我为什么会收到此错误的任何建议:
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
使用此代码(我认为问题出在这里而不是在SQL查询中,但我可能错了):
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;
}
}
感谢您的建议。
答案 0 :(得分:4)
您正在呼叫findViewById
,但您从未设置内容视图。致电setContentView(R.layout.yourlayout)
后,您必须在onCreate方法中添加super
。这就是你的主页按钮为空的原因。
答案 1 :(得分:0)
根据您的日志,您应该查看文件的第72行,在onCreate()中的某个位置 - 我无法从您提供的代码中分辨出行号。我建议使用调试器单步执行onCreate,这应该可以很快地揭示问题。我的猜测是你的getItems()返回null。