SQL中的Nullpointer异常,Listivew

时间:2015-06-05 04:24:28

标签: java android sqlite listview

由于我的高通而引发了Nullpointer异常。

非常抱歉,非常感谢你指出并容忍它。

再次感谢。

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);
    }
}

1 个答案:

答案 0 :(得分:1)

来自班级model的班级成员InputTab从未初始化。因此,您在第model.requery();行获得了一个NPE。这就是您保存数据的原因,但在此之后,您获得了NPE。您不需要在此课程中使用任何Cursor,当然,您不需要requery()任何内容。