This is my main activity I can't understand what's the wrong going on.while i try to run this,DDMS show NullPointerException.Please help me for find out the error.
public class MainActivity extends ActionBarActivity implements OnClickListener {
private Button btnAdd, btnView;
private TextView txtView;
private EditText etName;
private DBHelper dbHelper;
// private osList oslist;
private ArrayList<osList> allList;
private osList olist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCustomActionBar();
btnAdd = (Button) findViewById(R.id.btnAdd);
btnView = (Button) findViewById(R.id.btnView);
txtView = (TextView) findViewById(R.id.txtView);
etName = (EditText) findViewById(R.id.etName);
btnAdd.setOnClickListener(this);
btnView.setOnClickListener(this);
}
private void setCustomActionBar() {
// TODO Auto-generated method stub
ActionBar actionBar = getSupportActionBar();
// actionBar.setBackgroundDrawable(new ColorDrawable(
// com.arifxdroid.listviewdemo.R.color.my_color));
// actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
// View view = getLayoutInflater()
// .inflate(R.layout.custom_actionbar, null);
// actionBar.setCustomView(view, new ActionBar.LayoutParams(
// ActionBar.LayoutParams.MATCH_PARENT,
// ActionBar.LayoutParams.MATCH_PARENT));
actionBar.setBackgroundDrawable(new ColorDrawable(R.color.my_color));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
} else if (id == R.id.action_add) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAdd:
String givenName = etName.getText().toString();
olist = new osList(givenName);
long inserted = dbHelper.insertOS(olist);
if (inserted == -1) {
txtView.setText("Data can't be inserted");
} else {
Toast.makeText(getApplicationContext(), "sucessfully added",
Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnView:
allList = dbHelper.getosList();
if (allList != null || allList.size() == 0) {
// txtView.setText("NO list found");
Toast.makeText(getApplicationContext(), "No List found",
Toast.LENGTH_SHORT).show();
} else {
for (osList l : allList) {
txtView.append("\n" + l.toString());
}
}
break;
default:
break;
}
}
}
This is SQLite helper class
public class DBHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "ListOfOS";
public static final int VERSION = 1;
public static final String TABLE_NAME = "allOS";
public static final String ID_FIELD = "_ID";
public static final String NAME_FIELD = "osName";
public static final String TABLE_SQL = "CREATE TABLE " + TABLE_NAME + " ("
+ ID_FIELD + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME_FIELD
+ " TEXT)";
public DBHelper(Context context) {
super(context, DB_NAME, null, VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public long insertOS(osList oslist) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues value = new ContentValues();
value.put(NAME_FIELD, oslist.getOsName());
long inserted = db.insert(TABLE_NAME, "", value);
db.close();
return inserted;
}
public ArrayList<osList> getosList() {
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<osList> all = new ArrayList<osList>();
Cursor cursor = db.query(TABLE_SQL, null, null, null, null, null, null);
if (cursor != null) {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
do {
String name = cursor.getString(cursor
.getColumnIndex(NAME_FIELD));
osList list = new osList(name);
all.add(list);
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return all;
}
}
Aucun commentaire:
Enregistrer un commentaire