jeudi 4 juin 2015

Tab cum ListView cum SQLite app NullPointerException

For pratice purposes,I am trying to make an app that has tab function and is able to key in data which would be saved into a sql-database and will be reflected in the ListView.

However i got an runtime error when i hit the save button after keying in the fields of the my input.tab. According to my Logcat, i got an error code, nullpointer exception. After resetting the app, the data is saved and found in my ListView.

enter image description here

enter image description here

InputTab.java

 public class InputTab extends Activity{

  Cursor model=null;
  EditText name=null;
  EditText food=null;
  EditText category=null;
  RadioGroup types=null;
 RestaurantHelper helper;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inputdata);

   helper = new RestaurantHelper(this);

    name=(EditText)findViewById(R.id.namex);
    food=(EditText)findViewById(R.id.foodx);
    category=(EditText)findViewById(R.id.catx);

    Button save=(Button)findViewById(R.id.save);

    save.setOnClickListener(onSave);
}

    View.OnClickListener onSave=new View.OnClickListener() {
        public void onClick(View v) {

          helper.insert(name.getText().toString(),
                        food.getText().toString(),
                        category.getText().toString());
          model.requery();
        }
      };

}

ResturantHelper.java

class RestaurantHelper extends SQLiteOpenHelper {
  private static final String DATABASE_NAME="lunchlist.db";
  private static final int SCHEMA_VERSION=1;

  public RestaurantHelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
      //sample spacing name away from id

      db.execSQL("CREATE TABLE gandalf " +
            "(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT," +
            " food TEXT, category TEXT);");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // no-op, since will not be called until 2nd schema
    // version exists
  }

  public Cursor getAll() {
    return(getReadableDatabase()
            .rawQuery("SELECT _id, name, food, category FROM gandalf ORDER BY name",
                      null));
  }

  public void insert(String name, String food,
                     String category) {
    ContentValues cv=new ContentValues();

    cv.put("name", name);
    cv.put("food", food);
    cv.put("category", category);

    getWritableDatabase().insert("gandalf", "name", cv);
  }

  public String getName(Cursor c) {
    return(c.getString(1));
  }

  public String getFood(Cursor c) {
    return(c.getString(2));
  }

  public String getCategory(Cursor c) {
    return(c.getString(3));
  }

}

ListViewTab.java

public class ListViewTab extends Activity{

      Cursor model=null;
      RestaurantAdapter adapter=null;
      private RestaurantHelper helper= new RestaurantHelper(this);

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listcontact);
        ListView list=(ListView)findViewById(R.id.restaurants);

        helper=new RestaurantHelper(this);

        model=helper.getAll();

        adapter=new RestaurantAdapter(model);

        list.setAdapter(adapter);
        startManagingCursor(model);
    }

        class RestaurantHolder {
            private TextView name=null;
            private TextView food=null;
            private TextView category=null;

            RestaurantHolder(View row) {
              name=(TextView)row.findViewById(R.id.namer);
              food=(TextView)row.findViewById(R.id.foodr);
              category=(TextView)row.findViewById(R.id.catr);
            }

            void populateFrom(Cursor c, RestaurantHelper helper) {
              name.setText(helper.getName(c));
              food.setText(helper.getFood(c));
              category.setText(helper.getCategory(c));

            }
          }

        class RestaurantAdapter extends CursorAdapter {
            RestaurantAdapter(Cursor c) {
              super(ListViewTab.this, c);
            }

            @Override
            public void bindView(View row, Context ctxt,
                                 Cursor c) {
              RestaurantHolder holder=(RestaurantHolder)row.getTag();

              holder.populateFrom(c, helper);
            }

            @Override
            public View newView(Context ctxt, Cursor c,
                                 ViewGroup parent) {
              LayoutInflater inflater=getLayoutInflater();
              View row=inflater.inflate(R.layout.row, parent, false);
              RestaurantHolder holder=new RestaurantHolder(row);

              row.setTag(holder);

              return(row);
            }
          }

    }

Main.java

public class Main extends TabActivity {


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

    TabSpec tab1 = tabHost.newTabSpec("Input");
    TabSpec tab2 = tabHost.newTabSpec("List of Food");  

    tab1.setIndicator("Food");
    tab1.setContent(new Intent(this,InputTab.class));

    tab2.setIndicator("View Order");
    tab2.setContent(new Intent(this,ListViewTab.class));

    tabHost.addTab(tab1);
    tabHost.addTab(tab2);
    }
}

Aucun commentaire:

Enregistrer un commentaire