title: Fetching data from sqllite dynamically in table layout
body: <p>I am trying to fetch data dynamically from sqllite using table layout. I have used <code>getLayoutInflater()</code> for inflating but is not working and unable to get data in table rows.</p>
  
  <p>Code:</p>
  
  <p>MainActivity.java</p>
  
  <pre><code> package com.example.datab;
  
  import android.app.Activity;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.TableLayout;
  import android.widget.TableRow;
  import android.widget.TableRow.LayoutParams;
  import android.widget.TextView;
  
  public class MainActivity extends Activity {
  TextView user_tv,pass_tv; View view;
  TableLayout tb;int counter=0;
  TableRow tr;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.layoutt);
           tb=(TableLayout)findViewById(R.id.tablelayout);
  
          //tr=(TableRow)findViewById(R.id.tableRow1);
  
          SQLiteDatabase md=openOrCreateDatabase("databa",MODE_PRIVATE,null);
          md.execSQL("CREATE TABLE IF NOT EXISTS data (Username varchar(20),Password varchar(20))");
          md.execSQL("INSERT INTO data VALUES('nobal','Sharma')");
  
      Cursor cr=md.rawQuery("select * from data", null);
          cr.moveToFirst();
  
          //String username=cr.getString(cr.getColumnIndex("Username"));
          if (cr.moveToFirst()){
             do{
  
  
                 String username = cr.getString(cr.getColumnIndex("Username"));
                String passwd=cr.getString(cr.getColumnIndex("Password"));
  
          TableRow   view=(TableRow)getLayoutInflater().inflate(R.layout.layoutt,tb, false);//here NPE
          user_tv=(TextView)view.findViewById(R.id.textView1);
          pass_tv=(TextView)view.findViewById(R.id.textView2);
  
          user_tv .setText(username );
          pass_tv.setText(passwd);
         tb.addView(view);//and NPE here
  
             }while(cr.moveToNext());
  
          }cr.close();
  
      }
  }
  </code></pre>
  
  <p>And below is layoutt.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/tablelayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;TableRow
          android:id="@+id/tableRow1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:paddingLeft="10dp" 
          android:paddingTop="10dp"
          android:layout_marginLeft="50dp"
          android:layout_marginTop="100dp"&gt;
  
          &lt;TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Medium Text"
              android:textSize="35dp"
              android:paddingLeft="20dp"
              android:textAppearance="?android:attr/textAppearanceMedium" 
              android:background="@drawable/cell_shape"/&gt;
  
          &lt;TextView
              android:id="@+id/textView2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@drawable/cell_shape"
              android:text="Medium Text"
              android:textSize="35dp"
              android:textAppearance="?android:attr/textAppearanceMedium" 
              android:paddingLeft="20dp"/&gt;
  
      &lt;/TableRow&gt;
  
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p>logcat is not showing up NOW,but i do tested ,the code gives null pointer exception on the commented lines in the code</p>
  
  <p>Here is Logcat:</p>
  
  <pre><code>07-30 01:47:35.219: D/AndroidRuntime(973): Shutting down VM
  07-30 01:47:35.219: W/dalvikvm(973): threadid=1: thread exiting with uncaught exception (group=0x41465700)
  07-30 01:47:35.238: E/AndroidRuntime(973): FATAL EXCEPTION: main
  07-30 01:47:35.238: E/AndroidRuntime(973): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datab/com.example.datab.MainActivity}: java.lang.ClassCastException: android.widget.TableLayout cannot be cast to android.widget.TableRow
  07-30 01:47:35.238: E/AndroidRuntime(973):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at android.os.Handler.dispatchMessage(Handler.java:99)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at android.os.Looper.loop(Looper.java:137)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at android.app.ActivityThread.main(ActivityThread.java:5103)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at java.lang.reflect.Method.invokeNative(Native Method)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at java.lang.reflect.Method.invoke(Method.java:525)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at dalvik.system.NativeStart.main(Native Method)
  07-30 01:47:35.238: E/AndroidRuntime(973): Caused by: java.lang.ClassCastException: android.widget.TableLayout cannot be cast to android.widget.TableRow
  07-30 01:47:35.238: E/AndroidRuntime(973):  at com.example.datab.MainActivity.onCreate(MainActivity.java:42)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at android.app.Activity.performCreate(Activity.java:5133)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  07-30 01:47:35.238: E/AndroidRuntime(973):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
  07-30 01:47:35.238: E/AndroidRuntime(973):  ... 11 more
  </code></pre>