陷入空指针异常
我被困住了。我试图用 SQLite 表的字段填充列表视图。我对 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;
}
}
感谢您的建议。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在调用
findViewById
但您从未设置内容视图。调用super
后,您必须将setContentView(R.layout.yourlayout)
放入 onCreate 方法中。这就是为什么你的主页按钮为空。You are calling
findViewById
but 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.根据您的日志,您应该查看文件的第 72 行,位于 onCreate() 中的某个位置——我无法从您提供的代码中分辨出行号。我建议使用调试器单步执行 onCreate,这样应该很快就能发现问题。我的猜测是您的 getItems() 返回 null。
According to your log, you should take a look at line 72 of your file, somewhere in your onCreate() -- I can't tell the line numbers from the code you put up. I would suggest single stepping through your onCreate using the debugger, that should reveal the problem pretty quickly. My guess is that your getItems() is returning null.