-1

我有这组代码用于将数据插入到我的 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>
4

2 回答 2

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);;
于 2013-02-14T09:28:04.877 回答
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);

                }
于 2013-02-14T09:31:54.163 回答