2016-11-10 57 views
-2

即時嘗試執行查詢,但顯示錯誤。 這是我的代碼如何顯示數據庫中的數據android

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 :嘗試在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)處的空對象引用上調用虛方法'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View $ OnClickListener)' android.app.ActivityThread.android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: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)at android.app.ActivityThread.main(ActivityThread.java:6077)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)導致:java.lang.NullPointerException:嘗試調用虛擬方法'void android.support。在android.app.Activity.performCreate的com.example.oscar.app.consulta.onCreate(consulta.java:31)處的空對象引用上設計「design.widget.FloatingActionButton.setOnClickListener(android.view.View $ OnClickListener) (android.app.ActivityThread.java:2599)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)上的android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) )at android.app.ActivityThread.-wrap12(ActivityThread.java)at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)at android.os .Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:154)at android.app.ActivityThread.main(ActivityThread.java:6077)at java.lang.reflect.Method。調用(本地方法)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

回答

0

這個問題似乎是以下

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); 
TextView Lab=(TextView)findViewById(R.id.lab_con); 
+0

我做到了,但不起作用 –

相關問題