NullPointerException using append method on a view in android?
I am trying to populate data from database into a TextView using this code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
se开发者_C百科tContentView(R.layout.main);
TextView view = (TextView) findViewById(R.id.textView);
db = (new DatabaseHelper(this)).getWritableDatabase();
cursor = getCursor();
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
String text = cursor.getString(2);
view.append(text);
cursor.moveToNext();
}
However I am getting a NullPointerException on this line:
view.append(text);
and I am not sure about the reason as the view exists.
However I am getting a NullPointerException on this line:
view.append(text); and I am not sure about the reason as the view exists.
If you are sure that view
exists and is not null, then text
has to be null. You could for instance rewrite it as view.append(text != null ? text : "(null)");
You have to give the directions of the database how my example look.
I do the same example, but with this difference:
**db = SQLiteDatabase.openDatabase("/data/data/cl.kl.muestrausuario/DataBases/BDUsuarios",null,SQLiteDatabase.OPEN_READONLY);**
Cursor c = db.query("usuario", new String[]{"regID", "nombre", "apellido"}, null, null, null, null, null);
c.moveToFirst();
while (c.isAfterLast() == false)
{
view.append("\n" + c.getString(1));
view.append("\r" + c.getString(2));
c.moveToNext();
精彩评论