我正在研究一个Android应用程序,在FTP服务器上传照片。应用程序运行正常,我尝试修改代码并使用 Log.v 在日志文件中获取用户数据。
现在我的问题开始了,每次我尝试运行我的应用程序时都会崩溃。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase db = openOrCreateDatabase("userData",MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS UserInfo(firstname VARCHAR,lastname VARCHAR);");
db.execSQL("INSERT INTO UserInfo VALUES('pulkit','gupta');");
Cursor c = db.rawQuery("SELECT * FROM UserInfo",null);
//Log.d("result",c.getString(c.getColumnIndex("firstname")));
db.close();
}
附加我的日志图片..
12-09 07:23:19.825: E/Trace(1942): error opening trace file: No such file or directory (2)
12-09 07:23:21.165: D/AndroidRuntime(1942): Shutting down VM
12-09 07:23:21.165: W/dalvikvm(1942): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
12-09 07:23:21.196: E/AndroidRuntime(1942): FATAL EXCEPTION: main
12-09 07:23:21.196: E/AndroidRuntime(1942): java.lang.RuntimeException: Unable to start activity ComponentInfo{abc.photoapp/abc.photoapp.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 12
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.os.Looper.loop(Looper.java:137)
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-09 07:23:21.196: E/AndroidRuntime(1942): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 07:23:21.196: E/AndroidRuntime(1942): at java.lang.reflect.Method.invoke(Method.java:511)
12-09 07:23:21.196: E/AndroidRuntime(1942): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-09 07:23:21.196: E/AndroidRuntime(1942): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-09 07:23:21.196: E/AndroidRuntime(1942): at dalvik.system.NativeStart.main(Native Method)
12-09 07:23:21.196: E/AndroidRuntime(1942): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 12
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
12-09 07:23:21.196: E/AndroidRuntime(1942): at abc.photoapp.MainActivity.onCreate(MainActivity.java:29)
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.app.Activity.performCreate(Activity.java:5104)
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-09 07:23:21.196: E/AndroidRuntime(1942): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-09 07:23:21.196: E/AndroidRuntime(1942): ... 11 more
我的AVD配置
设备:4.0“WVGA(480x800 hdpi) 目标:Android 4.2.2 - API级别17 CPU / ABI:ARM(armabi-v7a) RAM 512
任何人都可以帮我解决问题
答案 0 :(得分:0)
我想你忘了cursor.moveToFirst()试试吧