我以前只使用两个参数“体重”和腰围,然后我继续向数据库添加额外的列,导致当我尝试查看数据时应用程序强制关闭。
我想这可能是因为它们已经是数据库中的数据,所以我完全重新安装了应用程序(我正在我自己的设备上测试它)但它仍然无法正常工作。
下面是处理所有 SQL 的类:
public class Stats {
public static final String KEY_ROWID = "_id";
public static final String KEY_WEIGHT = "weight";
public static final String KEY_WAIST = "waist";
public static final String KEY_CHEST = "chest";
public static final String KEY_LEGS = "legs";
public static final String KEY_ARMS = "arms";
private static final String DATABASE_NAME = "statsDB";
private static final String DATABASE_TABLE = "personalStats";
private static final int DATABASE_VERSION = 1;
private DbHelper ffHelper;
private final Context ffContext;
private SQLiteDatabase ffDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_WEIGHT
+ " TEXT NOT NULL," + KEY_WAIST + " TEXT NOT NULL,"
+ KEY_CHEST + " TEXT NOT NULL," + KEY_LEGS
+ " TEXT NOT NULL," + KEY_ARMS + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
onCreate(db);
}
}
public Stats(Context c) {
ffContext = c;
}
public Stats open() throws SQLException {
ffHelper = new DbHelper(ffContext);
ffDatabase = ffHelper.getWritableDatabase();
return this;
}
public void close() {
ffHelper.close();
}
public long createEntry(String weight, String waist, String chest, String legs, String arms) {
ContentValues cv = new ContentValues();
cv.put(KEY_WEIGHT, weight);
cv.put(KEY_WAIST, waist);
cv.put(KEY_CHEST, chest);
cv.put(KEY_LEGS, legs);
cv.put(KEY_ARMS, arms);
return ffDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
String[] columns = new String[] { KEY_ROWID, KEY_WEIGHT, KEY_WAIST, KEY_CHEST, KEY_LEGS, KEY_ARMS };
Cursor c = ffDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iWeight = c.getColumnIndex(KEY_WEIGHT);
int iWaist = c.getColumnIndex(KEY_WAIST);
int iChest = c.getColumnIndex(KEY_CHEST);
int iLegs = c.getColumnIndex(KEY_LEGS);
int iArms = c.getColumnIndex(KEY_ARMS);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iRow) + " " + c.getString(iWeight)
+ " " + c.getString(iWaist)
+ " " + c.getString(iChest)
+ " " + c.getString(iLegs)
+ " " + c.getString(iArms) + "\n";
}
return result;
}
}
下面是我的日志:
I/ActivityManager( 1294): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.uhi.fatfighter/.Splash u=0} from pid 1567
D/PermissionCache( 630): checking android.permission.READ_FRAME_BUFFER for uid=1000 => granted (1098 us)
D/dalvikvm( 2733): Late-enabling CheckJNI
I/ActivityManager( 1294): Start proc com.uhi.fatfighter for activity com.uhi.fatfighter/.Splash: pid=2733 uid=10072 gids={3003, 1015, 1028}
D/dalvikvm( 1294): GC_FOR_ALLOC freed 764K, 26% free 7870K/10567K, paused 78ms, total 83ms
I/dalvikvm-heap( 1294): Grow heap (frag case) to 17.014MB for 1048592-byte allocation
E/Trace ( 2733): error opening trace file: No such file or directory (2)
D/libEGL ( 2733): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
D/libEGL ( 2733): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
D/libEGL ( 2733): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
D/OpenGLRenderer( 2733): Enabling debug mode 0
I/ActivityManager( 1294): Displayed com.uhi.fatfighter/.Splash: +980ms
D/dalvikvm( 1845): GC_CONCURRENT freed 1484K, 26% free 7796K/10439K, paused 19ms+6ms, total 95ms
D/dalvikvm( 1845): WAIT_FOR_CONCURRENT_GC blocked 76ms
D/AudioHardware( 633): AudioHardware pcm playback is going to standby.
V/AudioHardware( 633): closeMixer_l() mMixerOpenCnt: 1
D/AudioHardware( 633): closePcmOut_l() mPcmOpenCnt: 1
D/dalvikvm( 1457): GC_CONCURRENT freed 1820K, 71% free 2483K/8451K, paused 23ms+6ms, total 63ms
I/ActivityManager( 1294): START {act=com.uhi.fatfighter.MainActivity cmp=com.uhi.fatfighter/.MainActivity u=0} from pid 2733
I/ActivityManager( 1294): Displayed com.uhi.fatfighter/.MainActivity: +625ms
D/AudioHardware( 633): AudioHardware pcm playback is exiting standby.
V/AudioHardware( 633): open pcm_out driver
D/AudioHardware( 633): openPcmOut_l() mPcmOpenCnt: 0
V/AudioHardware( 633): openMixer_l() mMixerOpenCnt: 0
V/AudioHardware( 633): open playback normal
V/AudioHardware( 633): write() wakeup setting route SPK
I/ActivityManager( 1294): START {act=com.uhi.fatfighter.DBView cmp=com.uhi.fatfighter/.DBView u=0} from pid 2733
D/AndroidRuntime( 2733): Shutting down VM
W/dalvikvm( 2733): threadid=1: thread exiting with uncaught exception (group=0x41196300)
D/dalvikvm( 1294): GC_CONCURRENT freed 849K, 28% free 8860K/12167K, paused 19ms+12ms, total 184ms
E/AndroidRuntime( 2733): FATAL EXCEPTION: main
E/AndroidRuntime( 2733): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uhi.fatfighter/com.uhi.fatfighter.DBView}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup
E/AndroidRuntime( 2733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
E/AndroidRuntime( 2733): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
E/AndroidRuntime( 2733): at android.app.ActivityThread.access$600(ActivityThread.java:142)
E/AndroidRuntime( 2733): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
E/AndroidRuntime( 2733): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2733): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 2733): at android.app.ActivityThread.main(ActivityThread.java:4928)
E/AndroidRuntime( 2733): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2733): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 2733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 2733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
E/AndroidRuntime( 2733): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2733): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.view.ViewGroup
E/AndroidRuntime( 2733): at android.view.LayoutInflater.rInflate(LayoutInflater.java:747)
E/AndroidRuntime( 2733): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
E/AndroidRuntime( 2733): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
E/AndroidRuntime( 2733): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
E/AndroidRuntime( 2733): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime( 2733): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime( 2733): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime( 2733): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
E/AndroidRuntime( 2733): at android.app.Activity.setContentView(Activity.java:1867)
E/AndroidRuntime( 2733): at com.uhi.fatfighter.DBView.onCreate(DBView.java:15)
E/AndroidRuntime( 2733): at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime( 2733): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime( 2733): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
E/AndroidRuntime( 2733): ... 11 more
W/ActivityManager( 1294): Force finishing activity com.uhi.fatfighter/.DBView
W/ActivityManager( 1294): Force finishing activity com.uhi.fatfighter/.MainActivity
D/dalvikvm( 1294): GC_FOR_ALLOC freed 1194K, 37% free 7765K/12167K, paused 68ms, total 68ms
W/ActivityManager( 1294): Activity pause timeout for ActivityRecord{417c3b50 com.uhi.fatfighter/.DBView}
I/Process ( 2733): Sending signal. PID: 2733 SIG: 9
I/WindowState( 1294): WIN DEATH: Window{415b12d0 com.uhi.fatfighter/com.uhi.fatfighter.MainActivity paused=true}
I/ActivityManager( 1294): Process com.uhi.fatfighter (pid 2733) has died.
W/InputDispatcher( 1294): channel '4158f330 com.uhi.fatfighter/com.uhi.fatfighter.Splash (server)' ~ Consumer closed input channel or an error occurred. events=0x9
E/InputDispatcher( 1294): channel '4158f330 com.uhi.fatfighter/com.uhi.fatfighter.Splash (server)' ~ Channel is unrecoverably broken and will be disposed!
W/InputDispatcher( 1294): Attempted to unregister already unregistered input channel '4158f330 com.uhi.fatfighter/com.uhi.fatfighter.Splash (server)'
I/WindowState( 1294): WIN DEATH: Window{4158f330 com.uhi.fatfighter/com.uhi.fatfighter.Splash paused=false}
W/ActivityManager( 1294): Force removing ActivityRecord{41631c20 com.uhi.fatfighter/.Splash}: app died, no saved state
I/WindowManager( 1294): WINDOW DIED Window{4158f330 com.uhi.fatfighter/com.uhi.fatfighter.Splash paused=false}
W/InputMethodManagerService( 1294): Got RemoteException sending setActive(false) notification to pid 2733 uid 10072
下面是 DBView.java 的代码:
public class DBView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view_stats);
TextView tv = (TextView) findViewById(R.id.tvDBInfo);
Stats dbInfo = new Stats(this);
dbInfo.open();
String data = dbInfo.getData();
dbInfo.close();
tv.setText(data);
}
}
以下是 view_stats.xml 的代码:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Weight" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Waist" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Chest" >
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Legs" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Arms" />
</TextView>
</TableRow>
<TextView
android:id="@+id/tvDBInfo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="info from db" />
</TableLayout>
</TableLayout>