陷入空指针异常

发布于 2024-12-11 20:57:06 字数 4619 浏览 0 评论 0原文

我被困住了。我试图用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百善笑为先 2024-12-18 20:57:06

您正在调用 findViewById 但您从未设置内容视图。调用 super 后,您必须将 setContentView(R.layout.yourlayout) 放入 onCreate 方法中。这就是为什么你的主页按钮为空。

You are calling findViewById but you never set the content view. You have to put setContentView(R.layout.yourlayout) in your onCreate method after you call super. This is why your home button is null.

蓝天 2024-12-18 20:57:06

根据您的日志,您应该查看文件的第 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文