我是Android应用程序的新手,正在尝试本教程中给出的一个示例应用程序。 http://www.jameselsey.co.uk/blogs/techblog/android-and-sqlite-a-really-easy-tutorial-that-anyone-can-do/ 我做了完全相同的..并在IDE中创建了一个项目,并制作了与教程中相同的包。 这是我的主类代码:
package com.jameselsey;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import com.demo.sql.R;
public class Main extends ListActivity
{
private static String SAMPLE_TABLE_NAME = "PERSONS_TABLE";
private SQLiteDatabase sampleDB = null;
private List<String> results = new ArrayList<String>();
private Cursor cursor = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
sampleDB = openOrCreateDatabase("NAME", MODE_PRIVATE, null);
createTable();
insertData();
lookupData();
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
}
catch (SQLiteException se)
{
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
finally
{
if (sampleDB != null)
sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
sampleDB.close();
}
}
/**
* Create a table if it doesn't already exist
*/
private void createTable()
{
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
SAMPLE_TABLE_NAME +
" (PERSON_NAME VARCHAR, " +
" COUNTRY VARCHAR, " +
" AGE INT(3));");
}
/**
* Insert some test data, modify as you see fit
*/
private void insertData()
{
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('James','ENGLAND',25);");
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('Dave','USA',18);");
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('Jean-Paul','FRANCE',33);");
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('Sergio','SPAIN',42);");
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('Hitori','JAPAN',73);");
}
/**
* Run a query to get some data, then add it to a List and format as you require
*/
private void lookupData()
{
cursor = sampleDB.rawQuery("SELECT PERSON_NAME, COUNTRY, AGE FROM " +
SAMPLE_TABLE_NAME +
" where AGE > 10 ", null);
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
String personName = cursor.getString(cursor.getColumnIndex("PERSON_NAME"));
String country = cursor.getString(cursor.getColumnIndex("COUNTRY"));
int age = cursor.getInt(cursor.getColumnIndex("AGE"));
results.add("" + personName + ", " + country + ", " + age);
} while (cursor.moveToNext());
}
cursor.close();
}
}
}
以及我的main.xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" ></ListView>
</LinearLayout>
当我在模拟器中单击应用程序时出错。错误是
抱歉,应用程序demosql (进程com.demo.sql)已停止 不料。请再试一次。
com.demo.sql是R.java文件的位置。
任何想法,为什么我会收到此错误?!
感谢
编辑:1 哦,这是我从DDMS控制台获得的:
[2011-04-08 17:30:44 - demosql] ------------------------------
[2011-04-08 17:30:44 - demosql] Android Launch!
[2011-04-08 17:30:44 - demosql] adb is running normally.
[2011-04-08 17:30:44 - demosql] Performing com.demo.sql.sqldemoo activity launch
[2011-04-08 17:30:44 - demosql] Automatic Target Mode: launching new emulator with compatible AVD 'my_avd'
[2011-04-08 17:30:44 - demosql] Launching a new emulator with Virtual Device 'my_avd'
[2011-04-08 17:30:46 - demosql] New emulator found: emulator-5554
[2011-04-08 17:30:46 - demosql] Waiting for HOME ('android.process.acore') to be launched...
[2011-04-08 17:31:32 - demosql] WARNING: Application does not specify an API level requirement!
[2011-04-08 17:31:32 - demosql] Device API version is 8 (Android 2.2)
[2011-04-08 17:31:32 - demosql] HOME is up on device 'emulator-5554'
[2011-04-08 17:31:32 - demosql] Uploading demosql.apk onto device 'emulator-5554'
[2011-04-08 17:31:32 - demosql] Installing demosql.apk...
[2011-04-08 17:32:22 - demosql] Success!
[2011-04-08 17:32:22 - demosql] Starting activity com.demo.sql.sqldemoo on device emulator-5554
[2011-04-08 17:32:26 - demosql] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.demo.sql/.sqldemoo }
[2011-04-08 17:59:47 - demosql] ------------------------------
[2011-04-08 17:59:47 - demosql] Android Launch!
[2011-04-08 17:59:47 - demosql] adb is running normally.
[2011-04-08 17:59:47 - demosql] Performing com.demo.sql.sqldemoo activity launch
[2011-04-08 17:59:47 - demosql] Automatic Target Mode: launching new emulator with compatible AVD 'my_avd'
[2011-04-08 17:59:47 - demosql] Launching a new emulator with Virtual Device 'my_avd'
[2011-04-08 17:59:47 - demosql] New emulator found: emulator-5554
[2011-04-08 17:59:47 - demosql] Waiting for HOME ('android.process.acore') to be launched...
[2011-04-08 18:00:22 - demosql] WARNING: Application does not specify an API level requirement!
[2011-04-08 18:00:22 - demosql] WARNING: Unknown device API version!
[2011-04-08 18:00:22 - demosql] HOME is up on device 'emulator-5554'
[2011-04-08 18:00:22 - demosql] Uploading demosql.apk onto device 'emulator-5554'
[2011-04-08 18:00:22 - demosql] Installing demosql.apk...
[2011-04-08 18:01:09 - demosql] Success!
[2011-04-08 18:01:09 - demosql] Starting activity com.demo.sql.sqldemoo on device emulator-5554
[2011-04-08 18:01:18 - demosql] Attempting to connect debugger to 'com.demo.sql' on port 8645
[2011-04-08 18:02:18 - demosql] ------------------------------
[2011-04-08 18:02:18 - demosql] Android Launch!
[2011-04-08 18:02:18 - demosql] adb is running normally.
[2011-04-08 18:02:18 - demosql] Performing com.demo.sql.sqldemoo activity launch
[2011-04-08 18:02:18 - demosql] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2011-04-08 18:02:24 - demosql] WARNING: Application does not specify an API level requirement!
[2011-04-08 18:02:24 - demosql] WARNING: Unknown device API version!
[2011-04-08 18:02:27 - demosql] Application already deployed. No need to reinstall.
[2011-04-08 18:02:27 - demosql] Starting activity com.demo.sql.sqldemoo on device emulator-5554
[2011-04-08 18:02:33 - demosql] ------------------------------
[2011-04-08 18:02:33 - demosql] Android Launch!
[2011-04-08 18:02:33 - demosql] adb is running normally.
[2011-04-08 18:02:33 - demosql] Performing com.demo.sql.sqldemoo activity launch
[2011-04-08 18:02:33 - demosql] Automatic Target Mode: launching new emulator with compatible AVD 'my_avd'
[2011-04-08 18:02:33 - demosql] Launching a new emulator with Virtual Device 'my_avd'
[2011-04-08 18:02:34 - demosql] New emulator found: emulator-5554
[2011-04-08 18:02:34 - demosql] Waiting for HOME ('android.process.acore') to be launched...
编辑 - 2 我在eclipse中创建了一个新的android项目,这解决了这个问题。我认为在我以前的项目中,我没有编辑应用名称。但在新项目中,一切都很完美。所以,问题解决了。无论如何,我在这里发布了logcat消息:
04-08 17:07:15.240: ERROR/Zygote(32): setreuid() failed. errno: 2
04-08 17:07:28.931: ERROR/Zygote(32): setreuid() failed. errno: 17
04-08 17:07:31.020: ERROR/BatteryService(67): usbOnlinePath not found
04-08 17:07:31.020: ERROR/BatteryService(67): batteryVoltagePath not found
04-08 17:07:31.020: ERROR/BatteryService(67): batteryTemperaturePath not found
04-08 17:07:31.060: ERROR/SurfaceFlinger(67): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
04-08 17:07:39.851: ERROR/EventHub(67): could not get driver version for /dev/input/mouse0, Not a typewriter
04-08 17:07:39.851: ERROR/EventHub(67): could not get driver version for /dev/input/mice, Not a typewriter
04-08 17:07:40.071: ERROR/System(67): Failure starting core service
04-08 17:07:40.071: ERROR/System(67): java.lang.SecurityException
04-08 17:07:40.071: ERROR/System(67): at android.os.BinderProxy.transact(Native Method)
04-08 17:07:40.071: ERROR/System(67): at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
04-08 17:07:40.071: ERROR/System(67): at android.os.ServiceManager.addService(ServiceManager.java:72)
04-08 17:07:40.071: ERROR/System(67): at com.android.server.ServerThread.run(SystemServer.java:184)
04-08 17:07:41.941: ERROR/SoundPool(67): error loading /system/media/audio/ui/Effect_Tick.ogg
04-08 17:07:41.941: ERROR/SoundPool(67): error loading /system/media/audio/ui/KeypressStandard.ogg
04-08 17:07:41.951: ERROR/SoundPool(67): error loading /system/media/audio/ui/KeypressSpacebar.ogg
04-08 17:07:41.951: ERROR/SoundPool(67): error loading /system/media/audio/ui/KeypressDelete.ogg
04-08 17:07:41.961: ERROR/SoundPool(67): error loading /system/media/audio/ui/KeypressReturn.ogg
04-08 17:07:46.070: ERROR/ThrottleService(67): Could not open GPS configuration file /etc/gps.conf
04-08 17:07:47.550: ERROR/logwrapper(143): executing /system/bin/tc failed: No such file or directory
04-08 17:07:47.620: ERROR/logwrapper(147): executing /system/bin/tc failed: No such file or directory
04-08 17:07:47.720: ERROR/logwrapper(149): executing /system/bin/tc failed: No such file or directory
04-08 17:08:08.903: ERROR/HierarchicalStateMachine(67): TetherMaster - unhandledMessage: msg.what=3
非常感谢你们。
答案 0 :(得分:0)
只需删除setContentView(R.layout.main)即可。 您的程序现在将立即运行。
数据库的数据通过setListAdapter显示(new ArrayAdapter(this,android.R.layout.simple_list_item_1,results))。
希望您现在明白为什么setContentView(R.layout.main)需要删除。 享受:)