我有这组用于将数据插入我的sqlite数据库的代码。但是当我尝试插入时,为什么我的应用会停止?
package edu.nyp.project;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
public class AddData extends Activity {
Button insertButton = null;
EditText shopText= null;
EditText dealText= null;
EditText locationText= null;
EditText websiteText= null;
EditText categoryText= null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adddata);
insertButton = (Button) findViewById(R.id.button1);
insertButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
DBAdapter dbAdapter = new
DBAdapter(getApplicationContext());
try{
dbAdapter.open();
String shop = shopText.getText().toString();
String deal = dealText.getText().toString();
String location = locationText.getText().toString();
String website = websiteText.getText().toString();
String category = categoryText.getText().toString();
}
catch(Exception e){
Log.d("Add Data ", e.getMessage());
}
finally{
if (dbAdapter != null)
dbAdapter.close();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.adddata, menu);
return true;
}
}
这是DBAdapter文件的代码供参考。
package edu.nyp.project;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_SHOP = "shop";
public static final String KEY_DEAL = "deal";
public static final String KEY_LOCATION = "location";
public static final String KEY_WEBSITE = "website";
public static final String KEY_CATEGORY = "category";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "Project";
private static final String DATABASE_TABLE = "Deals";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table Deals (_id integer primary key autoincrement, " +
"shop varchar not null, deal varchar not null, " +
"location varchar not null, website varchar not null, category text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter (Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate (SQLiteDatabase db)
{
try{
db.execSQL(DATABASE_CREATE);
}
catch(SQLException e){
e.printStackTrace();
}
}
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion+ "to "+ newVersion
+ "which will destroy all data");
db.execSQL("DROP TABLE IF EXISTS Deals");
onCreate(db);
}
}
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
public void close()
{
DBHelper.close();
}
public long insertDeal(String shop, String deal, String location, String website, String category)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_SHOP, shop);
initialValues.put(KEY_DEAL, deal);
initialValues.put(KEY_LOCATION, location);
initialValues.put(KEY_WEBSITE, website);
initialValues.put(KEY_CATEGORY, category);
return db.insert(DATABASE_TABLE, null, initialValues);
}
}
和xml文件在下面,也可供参考。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/shopText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Shop name" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/dealText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Deal" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/locationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Location" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/websiteText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Website" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/categoryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Category" >
</EditText>
</TableRow>
</TableLayout>
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="60px"
android:text="Insert" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="60px"
android:text="Delete" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="60px"
android:text="Search" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
因为我认为你不要用findViewById绑定那些组件:
shopText.getText().toString();
String deal = dealText.getText().toString();
String location = locationText.getText().toString();
String website = websiteText.getText().toString();
String category = categoryText.getText().toString();
结果应该是NullPointerException。 告诉我们你是LogCat ......
否则,请在创建时添加此内容并再次尝试:
shopText= (EditText)findViewById(R.id.THE_ID);
dealText= (EditText)findViewById(R.id.THE_ID);;
locationText= (EditText)findViewById(R.id.THE_ID);;
websiteText= (EditText)findViewById(R.id.THE_ID);;
categoryText= (EditText)findViewById(R.id.THE_ID);;
答案 1 :(得分:0)
First of all get all EditText By id.
try{
shopText=(EditText)findViewById(R.id.shopText)
//get All EditText by id like above
dbAdapter.open();
String shop = shopText.getText().toString();
String deal = dealText.getText().toString();
String location = locationText.getText().toString();
String website = websiteText.getText().toString();
String category = categoryText.getText().toString();
//secondly
dbAdapter.insertDeal(shop,deal,location,website,category);
}