我正在使用 android phone 和 API 15 作为测试设备。我尝试 运行 这个应用程序,但它在加载后 运行 在我的设备上停止运行
I am using my android phone with API 15 as a testing device. I try to run this application and it'stops running on my device just after it's loaded
这很简单。我正在尝试学习如何为 android 应用程序建立 sqlite 连接。
我正在使用 Android Studio 编写代码。当我尝试在我的测试设备上加载应用程序时 - Android Phone of API 15 - 白屏闪烁并弹出一个警告框说 "LearnDB has stopped working".
Please tell me where I am going wrong.
IMPORTANT NOTE- Please try to explain how a sqlite connection should be exactly established and what rules should I definitely keep in mind while doing the same.
Feel free to tell me any good programming practices while working with sqlite for android.
这是我的 MainActivity.java 文件。
package com.example.summer.learndb;
import android.database.sqlite.SQLiteDatabase;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private SQLiteDatabase db ;
public static final String DB_NAME = "scoreDb.db";
public static final int DB_VERSION = 1;
public static final String CREATE_DB_TABLE = "CREATE TABLE IF NOT EXISTS scoreTable (score1 INTEGER, score2 INTEGER);";
public static final String DB_TABLE = "scoreTable";
Button b1= (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = openOrCreateDatabase(DB_NAME, SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.close();
try{
db.execSQL(DB_TABLE);
db.close();
Toast.makeText(getBaseContext(),"Table created!!!",Toast.LENGTH_LONG).show();
}
catch(Exception e){
Toast.makeText(getBaseContext(),"ERROR!!!!", Toast.LENGTH_LONG).show();
}
b1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
}
});
b2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
}
});
}
}
这是我的 activity_main.xml 文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="add"
android:id="@+id/button1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show"
android:id="@+id/button2" />
</LinearLayout>
获取一个上下文并编写以下代码。
public Context context;
db = context.openOrCreateDatabase("scoreDb.db", Context.MODE_PRIVATE, null);
您需要删除恰好在 openorCreateDatabase() 之后编写的以下行。
db.close();
写这个
db.execSQL(CREATE_DB_TABLE);
而不是
db.execSQL(DB_TABLE);
Ram Mansawala 绝对是正确的,但我认为你还有另一个问题。您的 db.execSQL 在 try catch 中,因此它不会是崩溃的原因。您所做的是,在 setContentView 之前初始化按钮。这是个问题,因为系统找不到它们。这样做:
public class MainActivity extends Activity {
private SQLiteDatabase db ;
public static final String DB_NAME = "scoreDb.db";
public static final int DB_VERSION = 1;
public static final String CREATE_DB_TABLE = "CREATE TABLE IF NOT EXISTS scoreTable (score1 INTEGER, score2 INTEGER);";
public static final String DB_TABLE = "scoreTable";
Button b1;
Button b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
db = openOrCreateDatabase(DB_NAME, SQLiteDatabase.CREATE_IF_NECESSARY,null);
.
.
.
etc
在 setContentView()
之后初始化您的按钮
编辑
解释 sqlite 应该如何实现超出了这里的范围,但是这里有很好的教程:
http://www.vogella.com/tutorials/AndroidSQLite/article.html
这很简单。我正在尝试学习如何为 android 应用程序建立 sqlite 连接。 我正在使用 Android Studio 编写代码。当我尝试在我的测试设备上加载应用程序时 - Android Phone of API 15 - 白屏闪烁并弹出一个警告框说 "LearnDB has stopped working".
Please tell me where I am going wrong.
IMPORTANT NOTE- Please try to explain how a sqlite connection should be exactly established and what rules should I definitely keep in mind while doing the same. Feel free to tell me any good programming practices while working with sqlite for android.
这是我的 MainActivity.java 文件。
package com.example.summer.learndb;
import android.database.sqlite.SQLiteDatabase;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private SQLiteDatabase db ;
public static final String DB_NAME = "scoreDb.db";
public static final int DB_VERSION = 1;
public static final String CREATE_DB_TABLE = "CREATE TABLE IF NOT EXISTS scoreTable (score1 INTEGER, score2 INTEGER);";
public static final String DB_TABLE = "scoreTable";
Button b1= (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = openOrCreateDatabase(DB_NAME, SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.close();
try{
db.execSQL(DB_TABLE);
db.close();
Toast.makeText(getBaseContext(),"Table created!!!",Toast.LENGTH_LONG).show();
}
catch(Exception e){
Toast.makeText(getBaseContext(),"ERROR!!!!", Toast.LENGTH_LONG).show();
}
b1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
}
});
b2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
}
});
}
}
这是我的 activity_main.xml 文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="add"
android:id="@+id/button1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show"
android:id="@+id/button2" />
</LinearLayout>
获取一个上下文并编写以下代码。
public Context context; db = context.openOrCreateDatabase("scoreDb.db", Context.MODE_PRIVATE, null);
您需要删除恰好在 openorCreateDatabase() 之后编写的以下行。
db.close();
写这个
db.execSQL(CREATE_DB_TABLE);
而不是
db.execSQL(DB_TABLE);
Ram Mansawala 绝对是正确的,但我认为你还有另一个问题。您的 db.execSQL 在 try catch 中,因此它不会是崩溃的原因。您所做的是,在 setContentView 之前初始化按钮。这是个问题,因为系统找不到它们。这样做:
public class MainActivity extends Activity {
private SQLiteDatabase db ;
public static final String DB_NAME = "scoreDb.db";
public static final int DB_VERSION = 1;
public static final String CREATE_DB_TABLE = "CREATE TABLE IF NOT EXISTS scoreTable (score1 INTEGER, score2 INTEGER);";
public static final String DB_TABLE = "scoreTable";
Button b1;
Button b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
db = openOrCreateDatabase(DB_NAME, SQLiteDatabase.CREATE_IF_NECESSARY,null);
.
.
.
etc
在 setContentView()
之后初始化您的按钮编辑
解释 sqlite 应该如何实现超出了这里的范围,但是这里有很好的教程:
http://www.vogella.com/tutorials/AndroidSQLite/article.html