0

我的申请需要一些帮助。每次我尝试保存放入文本字​​段的值时,我的应用程序都会崩溃(按下按钮“保存”后)。我创建了以下类: 这是我的主要活动类(我有 TabHost 来切换 beetwing 活动 - 在这种情况下是一周中的几天)。

 package com.projekt;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class MainActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //--- TABHOST ---//
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, PoniedzialekActivity.class);
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("Pn").setIndicator("Pn",
                          res.getDrawable(R.drawable.ic_launcher))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, WtorekActivity.class);
        spec = tabHost.newTabSpec("Wt").setIndicator("Wt",
                          res.getDrawable(R.drawable.ic_launcher))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, SrodaActivity.class);
        spec = tabHost.newTabSpec("Sr").setIndicator("Sr",
                          res.getDrawable(R.drawable.ic_launcher))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, CzwartekActivity.class);
        spec = tabHost.newTabSpec("Cz").setIndicator("Cz",
                          res.getDrawable(R.drawable.ic_launcher))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, PiatekActivity.class);
        spec = tabHost.newTabSpec("Pt").setIndicator("Pt",
                          res.getDrawable(R.drawable.ic_launcher))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
        //--- END OF TABHOST ---//

    }
}

这是我的 DatabaseHelper 类:

    package com.projekt;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "database.db";
    private static final int DATABASE_VERSION = 1;
    public static final String TAG = "ProjectDatabase";

    final static String PN_TABLE = "Tablica Poniedzialek";
    static final String PN_KEY_ID = "_id";
    final static String PN_KEY_NAME ="name";                         
    final static String PN_KEY_TYPE_OF_SESSION ="type_of_session";
    final static String PN_KEY_START_TIME ="start_time";
    final static String PN_KEY_END_TIME ="end_time";
    final static String PN_KEY_ROOM ="room";

    static final String WT_TABLE = "Tablica Wtorek";
    static final String WT_KEY_ID = "_id";
    static final String WT_KEY_NAME ="name";
    static final String WT_KEY_TYPE_OF_SESSION ="type_of_session";
    static final String WT_KEY_START_TIME ="start_time";
    static final String WT_KEY_END_TIME ="end_time";
    static final String WT_KEY_ROOM ="room";

    static final String SR_TABLE = "Tablica Sroda";
    static final String SR_KEY_ID = "_id";
    static final String SR_KEY_NAME ="name";
    static final String SR_KEY_TYPE_OF_SESSION ="type_of_session";
    static final String SR_KEY_START_TIME ="start_time";
    static final String SR_KEY_END_TIME ="end_time";
    static final String SR_KEY_ROOM ="room";

    static final String CZ_TABLE = "Tablica Czwartek";
    static final String CZ_KEY_ID = "_id";
    static final String CZ_KEY_NAME ="name";
    static final String CZ_KEY_TYPE_OF_SESSION ="type_of_session";
    static final String CZ_KEY_START_TIME ="start_time";
    static final String CZ_KEY_END_TIME ="end_time";
    static final String CZ_KEY_ROOM ="room";

    static final String PT_TABLE = "Tablica Piatek";
    static final String PT_KEY_ID = "_id";
    static final String PT_KEY_NAME ="name";
    static final String PT_KEY_TYPE_OF_SESSION ="type_of_session";
    static final String PT_KEY_START_TIME ="start_time";
    static final String PT_KEY_END_TIME ="end_time";
    static final String PT_KEY_ROOM ="room";

    //--- KONSTRUKTOR ---//
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }           
    //--- POMOCNIK DO OTWIERANIA/AKTUALIZOWANIA BAZY DANYCH ---//
    private DatabaseHelper dbhelper; 
    //--- ZMIENNA DO PRZECHOWYWANIA INSTANCJI BAZY DANYCH ---//
    @SuppressWarnings("unused")
    private SQLiteDatabase db; 

    //--- TWORZENIE TABLIC ---//
    @Override
    public void onCreate(SQLiteDatabase db) {

        String pn_sql = "CREATE TABLE" + PN_TABLE +
                        "(PN_KEY_ID integer primary key autoincrement, " +
                        "PN_KEY_TYPE_OF_SESSION text, " +
                        "PN_KEY_NAME text, " +
                        "PN_KEY_START_TIME text, " +
                        "PN_KEY_END_TIME text, " +
                        "PN_KEY_ROOM text);";
        String wt_sql = "CREATE TABLE" + WT_TABLE +
                        "(WT_KEY_ID integer primary key autoincrement, " +
                        "WT_KEY_TYPE_OF_SESSION text, " +
                        "WT_KEY_NAME text, " +
                        "WT_KEY_START_TIME text, " +
                        "WT_KEY_END_TIME text, " +
                        "WT_KEY_ROOM text);";
        String sr_sql = "CREATE TABLE" + SR_TABLE +
                        "(SR_KEY_ID integer primary key autoincrement, " +
                        "SR_KEY_TYPE_OF_SESSION text, " +
                        "SR_KEY_NAME text, " +
                        "SR_KEY_START_TIME text, " +
                        "SR_KEY_END_TIME text, " +
                        "SR_KEY_ROOM text);";
        String cz_sql = "CREATE TABLE" + CZ_TABLE +
                        "(CZ_KEY_ID integer primary key autoincrement, " +
                        "CZ_KEY_TYPE_OF_SESSION text, " +
                        "CZ_KEY_NAME text, " +
                        "CZ_KEY_START_TIME text, " +
                        "CZ_KEY_END_TIME text, " +
                        "CZ_KEY_ROOM text);";
        String pt_sql = "CREATE TABLE" + PT_TABLE +
                        "(PT_KEY_ID integer primary key autoincrement, " +
                        "PT_KEY_TYPE_OF_SESSION text, " +
                        "PT_KEY_NAME text, " +
                        "PT_KEY_START_TIME text, " +
                        "PT_KEY_END_TIME text, " +
                        "PT_KEY_ROOM text);";

        db.execSQL(pn_sql);
        db.execSQL(wt_sql);
        db.execSQL(sr_sql);
        db.execSQL(cz_sql);
        db.execSQL(pt_sql);
    }
    @Override
    //--- ON UPGRADE ---//
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("Projekt", "aktualizacja z wersji " + oldVersion
              + " do wersji " + newVersion + " ( stare dane ulegna usunieciu ) ");
                 db.execSQL("DROP TABLE IF EXISTS" + PN_TABLE);
                 db.execSQL("DROP TABLE IF EXISTS" + WT_TABLE);
                 db.execSQL("DROP TABLE IF EXISTS" + SR_TABLE);
                 db.execSQL("DROP TABLE IF EXISTS" + CZ_TABLE);
                 db.execSQL("DROP TABLE IF EXISTS" + PT_TABLE);
        onCreate(db);
    }
    //--- OPEN DATABASE ---//
    public void open() throws SQLiteException 
    {
        try
        {
            db = dbhelper.getWritableDatabase();
        }
        catch(SQLiteException ex)
        {
            db = dbhelper.getReadableDatabase();
        }
    }
    //--- CLOSE DATABASE ---//
    public void close()
        {
            dbhelper.close();
        }

} //--- KONIEC KLASY DatabaseHelper ---//

这是我的 PoniedzialekActivity (我想在按钮上单击“添加”开始一个新的意图,我在其中向数据库添加值)

   package com.projekt;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class PoniedzialekActivity extends Activity implements OnClickListener {


    private Button butPnAdd;    
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_poniedzialek);
        butPnAdd = (Button) findViewById(R.id.butPnAdd);
        butPnAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.butPnAdd){
        Intent i = new Intent(PoniedzialekActivity.this,dodawaniePoniedzialek.class);
        startActivity(i);
        }
    }
}

这是我的 dodawanieActivity,我只是将值添加到数据库中

package com.projekt;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class dodawaniePoniedzialek extends Activity implements OnClickListener{
    private EditText TypeOfSessionEditText;
    private EditText NameEditText;
    private EditText StartTimeEditText;
    private EditText EndTimeEditText;
    private EditText RoomEditText;

    private Button cancelButton;
    private Button saveButton;

    private ArrayList<Poniedzialek> pn_list;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dodawanie);

        TypeOfSessionEditText = (EditText) findViewById(R.id.editText1);
        NameEditText = (EditText) findViewById(R.id.editText2);
        StartTimeEditText = (EditText) findViewById(R.id.editText3);
        EndTimeEditText = (EditText) findViewById(R.id.editText4);
        RoomEditText = (EditText) findViewById(R.id.editText5);

        cancelButton = (Button) findViewById(R.id.cancelButton);
        cancelButton.setOnClickListener(this);
        saveButton = (Button) findViewById(R.id.saveButton);
        saveButton.setOnClickListener(this);


        pn_list = new ArrayList<Poniedzialek>(); // dodano <Poniedzialek>

    }
    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.cancelButton){
        //  finish();
            Intent i = new Intent(dodawaniePoniedzialek.this,MainActivity.class);
            startActivity(i);
        }else if(v.getId() == R.id.saveButton){
            // Pobiera wartosci podane przez uzytkownika
            String providedTypeOfSession = TypeOfSessionEditText.getText().toString();
            String providedName = NameEditText.getText().toString();
            String provideStartTime = StartTimeEditText.getText().toString();
            String provideEndTime = EndTimeEditText.getText().toString();
            String provideRoom = RoomEditText.getText().toString();
            // Przekazuje powyzsze wartosci do metod set w klasie Poniedzialek
            Poniedzialek pn = new Poniedzialek();
            pn.setTypeOfSession(providedTypeOfSession);
            pn.setName(providedName);
            pn.setStartTime(provideStartTime);
            pn.setEndTime(provideEndTime);
            pn.setRoom(provideRoom);
            // Dodaj do listy
            pn_list.add(pn);
            // Dodaj do bazy
            addPnSession(pn);
            // Wyjdz z istniejacego UI i wroc do poprzedniego UI
        //  finish();
            Intent i = new Intent(dodawaniePoniedzialek.this,MainActivity.class);
            startActivity(i);
        } 
    }
    // metoda dodawania do bazy danych
    public void addPnSession(Poniedzialek pn){
        DatabaseHelper dbhelper = new DatabaseHelper(this);
        SQLiteDatabase db = dbhelper.getWritableDatabase();

        ContentValues pn_values = new ContentValues();
        pn_values.put(DatabaseHelper.PN_KEY_TYPE_OF_SESSION,pn.getTypeOfSession());
        pn_values.put(DatabaseHelper.PN_KEY_NAME, pn.getName());
        pn_values.put(DatabaseHelper.PN_KEY_START_TIME,pn.getStartTime());
        pn_values.put(DatabaseHelper.PN_KEY_END_TIME, pn.getEndTime());
        pn_values.put(DatabaseHelper.PN_KEY_ROOM,pn.getRoom());

        db.insert(DatabaseHelper.PN_TABLE, null, pn_values);
        db.close();
    }
}

这是我的 Poniedzialek 类(用于将值添加到数据库中)

package com.projekt;

public class Poniedzialek {

    //private variables
    int id;
    String name;
    String start_time;
    String end_time;
    String type_of_session;
    String room;


    // pusty constructor
    public Poniedzialek(){

    }

    // constructor
    public Poniedzialek(int id, String name,String start_time,String end_time,String type_of_session,String room){
        this.id = id;
        this.type_of_session = type_of_session;
        this.name = name;
        this.start_time = start_time;
        this.end_time = end_time;
        this.room = room;
    }

    // getting ID
    public int getID(){
        return this.id;
    }

    // setting id
    public void setID(int id){
        this.id = id;
    }

    // getting name
    public String getName(){
        return this.name;
    }

    // setting name
    public void setName(String name){
        this.name = name;
    }

    // getting start time
    public String getStartTime(){
        return this.start_time;
    }

    // setting start time
    public void setStartTime(String start_time){
        this.start_time = start_time;
    }

    // getting end time
    public String getEndTime(){
        return this.end_time;
    }

    // setting end time
    public void setEndTime(String end_time){
        this.end_time = end_time;
    }

    // getting type of session
    public String getTypeOfSession(){
        return this.type_of_session;
    }

    // setting type of session
    public void setTypeOfSession(String type_of_session){
        this.type_of_session = type_of_session;
    }

    // getting room
    public String getRoom(){
        return this.room;
    }

    // setting room
    public void setRoom(String room){
        this.room = room;
    }

}

顺便说一句,谁能告诉我如何在 PoniedzialekActivity 启动(或开关,不知道)上显示之前放入数据库的值 - 在列表中。

根据要求,这是 Log 所说的:

06-11 13:08:49.104: D/PhoneWindow(331): couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@405222a8 has no id.
06-11 13:08:50.784: I/Database(331): sqlite returned: error code = 1, msg = near "TABLETablica_Poniedzialek": syntax error
06-11 13:08:50.784: E/Database(331): Failure 1 (near "TABLETablica_Poniedzialek": syntax error) on 0x2da910 when preparing 'CREATE TABLETablica_Poniedzialek(PN_KEY_ID integer primary key autoincrement, PN_KEY_TYPE_OF_SESSION text, PN_KEY_NAME text, PN_KEY_START_TIME text, PN_KEY_END_TIME text, PN_KEY_ROOM text);'.
06-11 13:08:50.804: D/AndroidRuntime(331): Shutting down VM
06-11 13:08:50.804: W/dalvikvm(331): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-11 13:08:50.815: E/AndroidRuntime(331): FATAL EXCEPTION: main
06-11 13:08:50.815: E/AndroidRuntime(331): android.database.sqlite.SQLiteException: near "TABLETablica_Poniedzialek": syntax error: CREATE TABLETablica_Poniedzialek(PN_KEY_ID integer primary key autoincrement, PN_KEY_TYPE_OF_SESSION text, PN_KEY_NAME text, PN_KEY_START_TIME text, PN_KEY_END_TIME text, PN_KEY_ROOM text);
06-11 13:08:50.815: E/AndroidRuntime(331):  at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
06-11 13:08:50.815: E/AndroidRuntime(331):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
06-11 13:08:50.815: E/AndroidRuntime(331):  at com.projekt.DatabaseHelper.onCreate(DatabaseHelper.java:117)
06-11 13:08:50.815: E/AndroidRuntime(331):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
06-11 13:08:50.815: E/AndroidRuntime(331):  at com.projekt.dodawaniePoniedzialek.addPnSession(dodawaniePoniedzialek.java:79)
06-11 13:08:50.815: E/AndroidRuntime(331):  at com.projekt.dodawaniePoniedzialek.onClick(dodawaniePoniedzialek.java:69)
06-11 13:08:50.815: E/AndroidRuntime(331):  at android.view.View.performClick(View.java:2485)
06-11 13:08:50.815: E/AndroidRuntime(331):  at android.view.View$PerformClick.run(View.java:9080)
06-11 13:08:50.815: E/AndroidRuntime(331):  at android.os.Handler.handleCallback(Handler.java:587)
06-11 13:08:50.815: E/AndroidRuntime(331):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 13:08:50.815: E/AndroidRuntime(331):  at android.os.Looper.loop(Looper.java:123)
06-11 13:08:50.815: E/AndroidRuntime(331):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-11 13:08:50.815: E/AndroidRuntime(331):  at java.lang.reflect.Method.invokeNative(Native Method)
06-11 13:08:50.815: E/AndroidRuntime(331):  at java.lang.reflect.Method.invoke(Method.java:507)
06-11 13:08:50.815: E/AndroidRuntime(331):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-11 13:08:50.815: E/AndroidRuntime(331):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-11 13:08:50.815: E/AndroidRuntime(331):  at dalvik.system.NativeStart.main(Native Method)

最后一件事,我无法工作是在我的 PoniedzialekActivity 中显示放入数据库的值,你能告诉我我应该怎么做才能让它工作吗?我陷入了死胡同(这是我想要在 atm 工作的最后一件事)。我试着做这样的事情。请帮忙。

package com.projekt;


import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;


public class PoniedzialekActivity extends ListActivity implements OnClickListener { // now Extends ListActivity instead of Activity
    /* ADDED */
    private DatabaseHelper dbhelper;
    private SQLiteDatabase db;
    /* DEDDA */
    private Button butPnAdd;    
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_poniedzialek);
        butPnAdd = (Button) findViewById(R.id.butPnAdd);
        butPnAdd.setOnClickListener(this);

        List<Poniedzialek> values = showPn();

        // Use the SimpleCursorAdapter to show the
        // elements in a ListView
        ArrayAdapter<Poniedzialek> pn_adapter = new ArrayAdapter<Poniedzialek>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(pn_adapter);
    }
    /* ADDED */
    public List<Poniedzialek> showPn(){
        List<Poniedzialek> pn_list = new ArrayList<Poniedzialek>();

        Cursor cursor = db.query(DatabaseHelper.PN_TABLE, null, null, null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Poniedzialek pn = cursorToPoniedzialek(cursor);
            pn_list.add(pn);
            cursor.moveToNext();
        }
        // Make sure to close the cursor
        cursor.close();
        return pn_list;
    }
    private Poniedzialek cursorToPoniedzialek(Cursor cursor) {
        Poniedzialek pn = new Poniedzialek();
        pn.setID(cursor.getInt(0));
        pn.setStartTime(cursor.getString(1));
        pn.setEndTime(cursor.getString(2));
        pn.setTypeOfSession(cursor.getString(3));
        pn.setName(cursor.getString(4));
        pn.setRoom(cursor.getString(5));
        return pn;
    }
    /* DEDDA */
    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.butPnAdd){
        Intent i = new Intent(PoniedzialekActivity.this,dodawaniePoniedzialek.class);
        startActivity(i);
        }
    }
}
4

1 回答 1

1

您的表创建被搞砸了。您没有使用您设置的常量并且您缺少空格(最后一个是杀死您的程序的那个)。

这:

   String pn_sql = "CREATE TABLE" + PN_TABLE + 
                    "(PN_KEY_ID integer primary key autoincrement, " + 
                    "PN_KEY_TYPE_OF_SESSION text, " + 
                    "PN_KEY_NAME text, " + 
                    "PN_KEY_START_TIME text, " + 
                    "PN_KEY_END_TIME text, " + 
                    "PN_KEY_ROOM text);"; 

应该是这样的:

   String pn_sql = "CREATE TABLE " + PN_TABLE + 
                    "(" + PN_KEY_ID + " integer primary key autoincrement, " + 
                    PN_KEY_TYPE_OF_SESSION + " text, " + 
                    PN_KEY_NAME + " text, " + 
                    PN_KEY_START_TIME + " text, " + 
                    PN_KEY_END_TIME + " text, " + 
                    PN_KEY_ROOM + " text);"; 

对所有表创建语句重复。

于 2012-06-11T13:11:20.150 回答