如何从数据库android显示数据

时间:2016-11-10 22:40:56

标签: android sqlite nullpointerexception android-sqlite

我正在尝试进行查询,但显示错误。 这是我的代码

public String c(){ 
    objBD = new Bd(nContext, "Laboratorios", null, 1);
    db = objBD.getReadableDatabase();
    Cursor c=db.rawQuery("SELECT * FROM Laboratorios WHERE ID = (SELECT MAX(ID) FROM Laboratorios);", null);
    if(c!=null) {
        while (c.moveToNext()) {
            String iLab = c.getString(c.getColumnIndex("Descripcion"));

            //.add(iLab);
            return iLab;
        }
    }
    return null;
}

这是我想要显示它的活动

protected void onCreate(Bundle savedInstanceState) {
    TextView Lab=(TextView)findViewById(R.id.lab_con);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_consulta);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ConexionBD O1=new ConexionBD(this);
    O1.c();


    Lab.setText(O1);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();

        }
    });




    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        /*String texto ="bien";
        Toast toast = Toast.makeText(this, texto, Toast.LENGTH_LONG);
        toast.show();
        Intent i =new Intent(this,Labs.class);
        startActivity(i);*/

}



public void salir (View view){
    String texto ="Se ha guardado su registro";
    Toast toast = Toast.makeText(this, texto, Toast.LENGTH_LONG);
    toast.show();
    Intent salida=new Intent( Intent.ACTION_MAIN); //Llamando a la activity principal
    finish();

}

这是错误

  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

即时尝试在空白活动中进行,但总是有错误,谢谢你的帮助。

这是错误

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.oscar.app/com.example.oscar.app.consulta}:java.lang.NullPointerException:尝试调用虚方法'void android.support android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)上android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)的空对象引用上的.design.widget.FloatingActionButton.setOnClickListener(android.view.View $ OnClickListener)' :2707)在android.app.ActivityThread.-wrap12(ActivityThread.java)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:154)位于com.android.internal的java.lang.reflect.Method.invoke(Native Method)的android.app.ActivityThread.main(ActivityThread.java:6077)。 os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)产生的原因:java.lang.NullPoi nterException:尝试调用虚拟方法“无效android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View $ OnClickListener)”上在com.example.oscar.app.consulta.onCreate(CONSULTA空对象引用。 java:31)在android.app.Anstrumentation.callActivityOnCreate(Instrumentation.java:1118)android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)的android.app.Activity.performCreate(Activity.java:6664) android.app.ActivityThread.handleLaunchActivity在android.os(ActivityThread.java:2707)在android.app.ActivityThread.-wrap12(ActivityThread.java)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)。 Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:154)在android.app.ActivityThread.main(ActivityThread.java:6077)在java.lang.reflect.Method.invoke (本地方法)在com.android.internal.os.Z的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) ygoteInit.main(ZygoteInit.java:755)

1 个答案:

答案 0 :(得分:0)

问题似乎如下

"Debug"

您似乎正在尝试查找尚未在屏幕上绘制的视图。尝试将行移动到活动布局之后,如下所示:

TextView Lab=(TextView)findViewById(R.id.lab_con);