- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 SQLiteHelper 源文件,我用它来创建数据库等等。我有另一个源文件需要实例化数据库、提取数据并显示到 UI。但是,当我在模拟器上启动应用程序时,应用程序崩溃了。我不确定出了什么问题。源文件中没有给出错误。
餐厅数据库源文件
package com.demostudio.restaurants;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class RestaurantDB extends SQLiteOpenHelper {
static final String dbName = "RestaurantDB";
static final Integer dbVersion = 1;
static final String restaurantListTable = "RestaurantList";
static final String colRestaurantID = "RestaurantID";
static final String colRestaurantName = "RestaurantName";
static final String colRestaurantStore = "StoreNo";
public RestaurantDB(Context context) {
super(context, dbName, null, dbVersion);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String newTableSQL =
"CREATE TABLE "+
restaurantListTable+
" ("+colRestaurantID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
colRestaurantName+" TEXT NOT NULL, "+
colRestaurantStore+" TEXT NOT NULL "+
");";
db.execSQL(newTableSQL);
ContentValues cv = new ContentValues();
cv.put(colRestaurantName, "Restaurant AAA");
cv.put(colRestaurantStore, "1");
db.insert(restaurantListTable, null, cv);
cv.put(colRestaurantName, "Restaurant BBB");
cv.put(colRestaurantStore, "2");
db.insert(restaurantListTable, null, cv);
db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
AllRestaurants源文件
package com.demostudio.restaurants;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
public class AllRestaurants extends ListActivity {
RestaurantDB db = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
db=new RestaurantDB(AllRestaurants.this);
onPostExecute( ) ;
}
private Cursor doQuery() {
return(db.getReadableDatabase().rawQuery("SELECT colRestaurantID AS _id, colRestaurantName, colRestaurantStore "
+ "FROM RestaurantListTable ORDER BY colRestaurantName", null));
}
public void onPostExecute( ) {
SimpleCursorAdapter adapter;
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
adapter=new SimpleCursorAdapter(this, R.layout.activity_all_restaurants,
doQuery(), new String[] {
RestaurantDB.colRestaurantName,
RestaurantDB.colRestaurantStore },
new int[] { R.id.title, R.id.value },
0);
}
else {
adapter=new SimpleCursorAdapter(this, R.layout.activity_all_restaurants,
doQuery(), new String[] {
RestaurantDB.colRestaurantName,
RestaurantDB.colRestaurantStore },
new int[] { R.id.title, R.id.value });
}
setListAdapter(adapter);
}
}
Activity ListView 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="wrap_content" >
<ListView
android:id="@android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
activity_all_restaurants XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="20sp"
android:textStyle="bold"/>
</RelativeLayout>
日志文件
12-13 14:08:52.805: W/Trace(3861): Unexpected value from nativeGetEnabledTags: 0
12-13 14:08:52.805: W/Trace(3861): Unexpected value from nativeGetEnabledTags: 0
12-13 14:08:54.795: D/dalvikvm(3861): GC_FOR_ALLOC freed 68K, 7% free 2670K/2860K, paused 139ms, total 143ms
12-13 14:08:54.826: I/dalvikvm-heap(3861): Grow heap (frag case) to 3.796MB for 1127536-byte allocation
12-13 14:08:54.946: D/dalvikvm(3861): GC_FOR_ALLOC freed 1K, 5% free 3770K/3964K, paused 112ms, total 112ms
12-13 14:08:55.145: D/dalvikvm(3861): GC_CONCURRENT freed <1K, 5% free 3785K/3964K, paused 12ms+27ms, total 203ms
12-13 14:08:55.618: D/AndroidRuntime(3861): Shutting down VM
12-13 14:08:55.618: W/dalvikvm(3861): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-13 14:08:55.715: E/AndroidRuntime(3861): FATAL EXCEPTION: main
12-13 14:08:55.715: E/AndroidRuntime(3861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demostudio.restaurants/com.demostudio.restaurants.AllRestaurants}: java.lang.IllegalArgumentException: column '_id' does not exist
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.os.Looper.loop(Looper.java:137)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-13 14:08:55.715: E/AndroidRuntime(3861): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 14:08:55.715: E/AndroidRuntime(3861): at java.lang.reflect.Method.invoke(Method.java:511)
12-13 14:08:55.715: E/AndroidRuntime(3861): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-13 14:08:55.715: E/AndroidRuntime(3861): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-13 14:08:55.715: E/AndroidRuntime(3861): at dalvik.system.NativeStart.main(Native Method)
12-13 14:08:55.715: E/AndroidRuntime(3861): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.support.v4.widget.CursorAdapter.(CursorAdapter.java:151)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.support.v4.widget.ResourceCursorAdapter.(ResourceCursorAdapter.java:93)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.support.v4.widget.SimpleCursorAdapter.(SimpleCursorAdapter.java:89)
12-13 14:08:55.715: E/AndroidRuntime(3861): at com.demostudio.restaurants.AllRestaurants.onPostExecute(AllRestaurants.java:27)
12-13 14:08:55.715: E/AndroidRuntime(3861): at com.demostudio.restaurants.AllRestaurants.onCreate(AllRestaurants.java:18)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.Activity.performCreate(Activity.java:5104)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-13 14:08:55.715: E/AndroidRuntime(3861): ... 11 more
我之前将 listview 和两个 textview 放在同一个 XML 文件中,但是没有用。所以现在我把它放在一个单独的 XML 文件中,但它仍然不起作用。
最佳答案
在你的助手的 onCreate()
中,你有:
String newTableSQL =
"CREATE TABLE "+
`+
" ("+colRestaurantID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
colRestaurantName+" TEXT NOT NULL, "+
colRestaurantStore+" TEXT NOT NULL "+
");";
注意您是如何连接字符串来创建完整语句的。生成的 SQL 将是:
CREATE TABLE RestaurantList (RestaurantID INTEGER PRIMARY KEY AUTOINCREMENT, RestaurantName TEXT NOT NULL, RestaurantStore TEXT NOT NULL );
基于您对 restaurantListTable
、colRestaurantID
、colRestaurantName
、colRestaurantStore
的值。
但是,在您的 rawQuery()
调用中,您的 SQL 是这样的:
SELECT colRestaurantID AS _id, colRestaurantName, colRestaurantStore FROM RestaurantListTable ORDER BY colRestaurantName
请注意,您在 rawQuery()
SQL 中的列名前面有 col
,而在您的 CREATE TABLE
。这行不通,因为您需要列名保持一致。
是更改CREATE TABLE
语句还是更改SELECT
语句由您决定。
关于android - 无法从 SQLite 数据库中提取数据并显示在 Activity 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13843126/
我通过 spring ioc 编写了一些 Rest 应用程序。但我无法解决这个问题。这是我的异常(exception): org.springframework.beans.factory.BeanC
我对 TestNG、Spring 框架等完全陌生,我正在尝试使用注释 @Value通过 @Configuration 访问配置文件注释。 我在这里想要实现的目标是让控制台从配置文件中写出“hi”,通过
为此工作了几个小时。我完全被难住了。 这是 CS113 的实验室。 如果用户在程序(二进制计算器)结束时选择继续,我们需要使用 goto 语句来到达程序的顶部。 但是,我们还需要释放所有分配的内存。
我正在尝试使用 ffmpeg 库构建一个小的 C 程序。但是我什至无法使用 avformat_open_input() 打开音频文件设置检查错误代码的函数后,我得到以下输出: Error code:
使用 Spring Initializer 创建一个简单的 Spring boot。我只在可用选项下选择 DevTools。 创建项目后,无需对其进行任何更改,即可正常运行程序。 现在,当我尝试在项目
所以我只是在 Mac OS X 中通过 brew 安装了 qt。但是它无法链接它。当我尝试运行 brew link qt 或 brew link --overwrite qt 我得到以下信息: ton
我在提交和 pull 时遇到了问题:在提交的 IDE 中,我看到: warning not all local changes may be shown due to an error: unable
我跑 man gcc | grep "-L" 我明白了 Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more inf
我有一段代码,旨在接收任何 URL 并将其从网络上撕下来。到目前为止,它运行良好,直到有人给了它这个 URL: http://www.aspensurgical.com/static/images/a
在过去的 5 个小时里,我一直在尝试在我的服务器上设置 WireGuard,但在完成所有设置后,我无法 ping IP 或解析域。 下面是服务器配置 [Interface] Address = 10.
我正在尝试在 GitLab 中 fork 我的一个私有(private)项目,但是当我按下 fork 按钮时,我会收到以下信息: No available namespaces to fork the
我这里遇到了一些问题。我是 node.js 和 Rest API 的新手,但我正在尝试自学。我制作了 REST API,使用 MongoDB 与我的数据库进行通信,我使用 Postman 来测试我的路
下面的代码在控制台中给出以下消息: Uncaught DOMException: Failed to execute 'appendChild' on 'Node': The new child el
我正在尝试调用一个新端点来显示数据,我意识到在上一组有效的数据中,它在数据周围用一对额外的“[]”括号进行控制台,我认为这就是问题是,而新端点不会以我使用数据的方式产生它! 这是 NgFor 失败的原
我正在尝试将我的 Symfony2 应用程序部署到我的 Azure Web 应用程序,但遇到了一些麻烦。 推送到远程时,我在终端中收到以下消息 remote: Updating branch 'mas
Minikube已启动并正在运行,没有任何错误,但是我无法 curl IP。我在这里遵循:https://docs.traefik.io/user-guide/kubernetes/,似乎没有提到关闭
每当我尝试docker组成任何项目时,都会出现以下错误。 我尝试过有和没有sudo 我在这台机器上只有这个问题。我可以在Mac和Amazon WorkSpace上运行相同的容器。 (myslabs)
我正在尝试 pip install stanza 并收到此消息: ERROR: No matching distribution found for torch>=1.3.0 (from stanza
DNS 解析看起来不错,但我无法 ping 我的服务。可能是什么原因? 来自集群中的另一个 Pod: $ ping backend PING backend.default.svc.cluster.l
我正在使用Hibernate 4 + Spring MVC 4当我开始 Apache Tomcat Server 8我收到此错误: Error creating bean with name 'wel
我是一名优秀的程序员,十分优秀!