Дебаггер тоже показывает, что ошибка где-то на стадии onCreate... правда пользуюсь им , так сказать - интуитивно.
Но ума не приложу, что у меня неправильно в Манифесте? может я еще в МаинАктивити напортачил? с 1го взгляда ничего не найду. Помогите разобраться. Работаю в Eclipse. Заранее благодарю.
MainActivity
Код: Выделить всё
package ru.redanger.sqltestphone;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends ListActivity implements LoaderCallbacks<Cursor>{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openDB();
String[] from = new String[] { DBAdapter.NAME_COLUMN + DBAdapter.PHONE_COLUMN };
int[] to = new int[] { R.id.myListView };
scAdapter = new SimpleCursorAdapter (this,
R.layout.activity_main, null, from, to, 0);
myDb = new DBAdapter(this);
// setListAdapter(scAdapter); // адаптер для листвью из другого туториала
Bundle bndl = new Bundle();
registerForContextMenu(lvData);
getLoaderManager().initLoader(LOADER_ID, bndl, (android.app.LoaderManager.LoaderCallbacks<Cursor>)this);
}
DBAdapter myDb;
EditText newText;
SimpleCursorAdapter scAdapter;
ListView lvData;
private static final int LOADER_ID = 1;
public static String name;
public void addName(View view) {
String name = newText.getText().toString().trim();
myDb.insertRow(name);
}
public String getName() {
return name;
}
@Override
protected void onDestroy() {
super.onDestroy();
myDb.close();
}
private void openDB() {
myDb = new DBAdapter(this);
myDb.open();
registerForContextMenu(lvData);
}
Button btnOk = (Button) findViewById(R.id.btnSave);
EditText enterText = (EditText) findViewById(R.id.edit_text);
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bndl) {
// TODO Auto-generated method stub
return new MyCursorLoader(this, myDb);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// TODO Auto-generated method stub
scAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// TODO Auto-generated method stub
}
static class MyCursorLoader extends CursorLoader {
DBAdapter db;
public MyCursorLoader(Context context, DBAdapter db) {
super(context);
this.db = db;
}
@Override
public Cursor loadInBackground() {
Cursor cursor= db.getAllRows();
return cursor; }
}
}
Код: Выделить всё
package ru.redanger.sqltestphone;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "PhoneBase.db";
private static final String DATABASE_VERSION = "table";
private static final int DB_VERSION =1;
//index field name
public static final String KEY_ID = "_id";
public static final int COL_KEY_ID = 0;
public static final String NAME_COLUMN = "name";
public static final String PHONE_COLUMN = "number";
public static final int COL_NAME = 1;
public static final int COL_PHONE = 2;
public static final String[] ALL_KEYS = new String[] {KEY_ID, NAME_COLUMN, PHONE_COLUMN};
private static final String DATABASE_CREATE = " create table "
+ DATABASE_VERSION + " (" + KEY_ID
+ " integer primary key autoincrement, "
+ NAME_COLUMN + " text not null "
+ PHONE_COLUMN + " text not null);";
private SQLiteDatabase db;
private final Context context;
public DBAdapter(Context ctx) {
this.context = ctx;
dbHelper = new myDbHelper(context);
}
/*public DBAdapter(Context context) {
this.context = context;
} */
private myDbHelper dbHelper;
public DBAdapter open() throws SQLException {
try {
db = dbHelper.getWritableDatabase();
}catch (SQLiteException e) {
db = dbHelper.getReadableDatabase();
}
return this;
}
public void close() {
db.close();
}
public long insertRow(String name) {
/*
* CHANGE 3:
*/
// TODO: Update data in the row with new fields.
// TODO: Also change the function's arguments to be what you need!
// Create row's data:
ContentValues initialValues = new ContentValues();
initialValues.put(NAME_COLUMN, MainActivity.name);
//initialValues.put(PHONE_COLUMN, phone);
// Insert it into the database.
return db.insert(DATABASE_VERSION, null, initialValues);
}
public boolean deleteRow(long rowId) {
String where = KEY_ID + "=" + rowId;
return db.delete(DATABASE_VERSION, where, null) != 0;
}
public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_VERSION, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public Cursor getRow(long rowId) {
String where = KEY_ID + "=" + rowId;
Cursor c = db.query(true, DATABASE_VERSION, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Change an existing row to be equal to new data.
public boolean updateRow(long rowId, String name, String number) {
String where = KEY_ID + "=" + rowId;
/*
* CHANGE 4:
*/
// TODO: Update data in the row with new fields.
// TODO: Also change the function's arguments to be what you need!
// Create row's data:
ContentValues newValues = new ContentValues();
newValues.put(NAME_COLUMN, name);
newValues.put(PHONE_COLUMN, number);
// Insert it into the database.
return db.update(DATABASE_VERSION, newValues, where, null) != 0;
}
private static class myDbHelper extends SQLiteOpenHelper
{
myDbHelper(Context context) {
super(context, DATABASE_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");
// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_VERSION);
// Recreate new database:
onCreate(_db);
}
}
}
Код: Выделить всё
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.redanger.sqltestphone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Код: Выделить всё
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="onOkBtnClick"
android:text="@android:string/ok" />
<EditText
android:id="@+id/edit_text"
android:layout_width="fill_parent"
android:layout_toLeftOf="@id/btnSave"
android:layout_height="wrap_content"
android:hint = "Новая запись" />
</RelativeLayout>
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>