不能从一个活动跳到下一个活动

时间:2011-02-22 12:57:11

标签: android sqlite

嘿大家, 我不能从我的发射活动跳到我的第二个活动,我不知道为什么。这是我得到的错误:

02-22 11:43:04.858: ERROR/AndroidRuntime(18335): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.Database/android.Database.projects}: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT  _id, Name, Comment, projects

这是我的发布活动:

package android.Database;


import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class projectsDatabase extends Activity {
    /** Called when the activity is first created. */

    SQLiteDatabase myDB = null;
    final static String MY_DB_NAME ="projectsDatabase";
    final static String MY_DB_TABLE = "projects";

    static final int MENU_PROJECTS = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onCreateDBAndDBTabled();
        setContentView(R.layout.main);
    }

    private void onCreateDBAndDBTabled(){

        myDB = this.openOrCreateDatabase(MY_DB_NAME, MODE_PRIVATE, null);
        myDB.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DB_TABLE
                + " ( _id integer primary key autoincrement,"+ 
                "Name varchar(100),"+ 
                "Comment varchar(128),"+ 
                "BookingDetails varchar(255),"+ 
                "ProjectKind integer(3))"
                +";");
         }

    public boolean onCreateOptionsMenu(Menu menu) {
          super.onCreateOptionsMenu(menu);

            menu.add(0, MENU_PROJECTS, 0, R.string.menuProjects)
            .setShortcut('1', 'f')
            .setIcon(R.drawable.icon);
             return true;

          }

    public boolean onOptionsItemSelected(MenuItem item){
          switch (item.getItemId()) {
          case MENU_PROJECTS:
            Intent iProjects= new Intent(this, projects.class);
            startActivity(iProjects);
            return true;
          }
          return false;
        }



}

这是我想在点击菜单按钮时启动的活动:

package android.Database;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.TextView;

public class projects extends ListActivity {

        SQLiteDatabase myDB = null;

        static final int MENU_NEW_PROJECT = 0;

        public void onCreate(Bundle icicle){
            super.onCreate(icicle);

            myDB = this.openOrCreateDatabase(projectsDatabase.MY_DB_NAME, MODE_PRIVATE, null);

            Cursor c = myDB.rawQuery("SELECT  _id, ColumnName, ColumnComment, " + projectsDatabase.MY_DB_TABLE + ";", null);
            startManagingCursor(c);

                SimpleCursorAdapter adapter = new SimpleCursorAdapter (this,
                        android.R.layout.simple_list_item_1,
                        c,
                        new String[] {"_id"},
                        new int []{android.R.id.text1});

                adapter.setViewBinder (new ViewBinder (){

                    @Override 
                    public boolean setViewValue(View view, Cursor theCursor, int column){

                        String ColumnName = theCursor.getString(1);//Name
                        String ColumnComment = theCursor.getString(2);//Comment

                        ((TextView)view).setText(ColumnName + "," + ColumnComment);

                        return true;
                    }

                });
                this.setListAdapter(adapter);

        }

        @Override
                protected void onListItemClick(ListView l, View v, int position, long _id){
                            super.onListItemClick(l, v, position, _id);

                            Intent i = new Intent(this, projects_New.class);
                            i.putExtra("_id", _id);
                this.startActivity(i);
            }

        public boolean onCreateOptionsMenu(Menu menu){
            super.onCreateOptionsMenu(menu);
                    menu.add(0, MENU_NEW_PROJECT, 0, R.string.menuNewProject)
                .setShortcut('1', 'n')
                .setIcon(android.R.drawable.ic_menu_add);
            return true;
            }

        public boolean onOptionsItemSelected(MenuItem item){
            switch (item.getItemId()){
            case MENU_NEW_PROJECT:
                Intent iProjects_New = new Intent (this, projects_New.class);
                    startActivity (iProjects_New);
                    return true;
            }
            return false;


        }

}

我真的希望你能再次帮助我;)

-------------------------------------------- Edit-- ----------------------------------------- 现在我可以保存数据,但无法从我的listview中选择任何内容...这里是我的projects_new.class的代码:

package android.Database;


import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class projects_New extends Activity{
    SQLiteDatabase myDB = null;
    static final int MENU_INSERT_PROJECT = 0;
    static final int MENU_UPDATE_PROJECT = 0;

    @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.projects_new);

            Spinner s1 = (Spinner) findViewById(R.id.cb_ProjectKind);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( 
            this,
            R.array.ProjectKind,
            android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s1.setAdapter(adapter);

        if(getIntent().hasExtra("_id") == true){
                long l = getIntent().getExtras().getLong("_id");
                myDB = this.openOrCreateDatabase(projectsDatabase.MY_DB_NAME, MODE_PRIVATE, null);

                Cursor myCursor = myDB.rawQuery("SELECT _id, ColumnName, ColumnComment, ColumnBookingDetails, ColumnProjectKind FROM" 
                                                + projectsDatabase.MY_DB_TABLE + "WHERE _id = " +l+ "", null);
                startManagingCursor(myCursor);

                int ColumnName = myCursor.getColumnIndex("ColumnName");
                int ColumnComment = myCursor.getColumnIndex("ColumnComment");
                int ColumnBookingDetails = myCursor.getColumnIndex("ColumnBookingDetails");
                int ColumnProjectKind = myCursor.getColumnIndex("ColumnProjectKind");

                myCursor.moveToFirst();

                if (myCursor != null) {
                    if(myCursor.isFirst()){
                        EditText eName = (EditText)findViewById(R.id.ed_Name);
                        eName.setText(myCursor.getString(ColumnName));

                        EditText eComment = (EditText)findViewById(R.id.ed_Comment);
                        eComment.setText(myCursor.getString(ColumnComment));

                        EditText eBookingDetails = (EditText)findViewById(R.id.ed_BookingDetails);
                        eBookingDetails.setText (myCursor.getString(ColumnBookingDetails));

                        Spinner sProjectKind = (Spinner)findViewById(R.id.cb_ProjectKind);
                        sProjectKind.setSelection(myCursor.getInt(ColumnProjectKind), true);
                    }
                }
            }
        }

    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);

        if(getIntent().hasExtra("_id")== true)
        {
            menu.add(0,MENU_UPDATE_PROJECT, 0, R.string.menuUpdate)
            .setShortcut('1', 's')
            .setIcon(android.R.drawable.ic_menu_save);
        }
        else
        {
            menu.add(0, MENU_INSERT_PROJECT, 0, R.string.menuInsert)
            .setShortcut('1', 's')
            .setIcon(android.R.drawable.ic_menu_save);
        }
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
        case MENU_INSERT_PROJECT:
            EditText Name = (EditText)findViewById(R.id.ed_Name);
            EditText Comment = (EditText)findViewById(R.id.ed_Comment);
            EditText BookingDetails = (EditText)findViewById(R.id.ed_BookingDetails);
            Spinner ProjectKind = (Spinner)findViewById(R.id.cb_ProjectKind);

            int i = ProjectKind.getSelectedItemPosition();

            if(Name.length()!= 0)
            {
                myDB = this.openOrCreateDatabase(projectsDatabase.MY_DB_NAME, MODE_PRIVATE, null);


                        if(getIntent().hasExtra("id")== true)//update
                        {
                            long l = getIntent().getExtras().getLong("_id");
                            myDB.execSQL("UPDATE "+ projectsDatabase.MY_DB_TABLE+" SET "+
                                    "name = '"+Name.getText().toString()+"', "+
                                    "Comment = '"+Comment.getText().toString()+"', "+
                                    "BookingDetails ='"+BookingDetails.getText().toString()+"', "+
                                    "ProjectKind ='"+i+"', "+
                                    "WHERE _id = "+l+";"); 

                        }
                        else //insert
                        {
                            myDB.execSQL("INSERT INTO "+ projectsDatabase.MY_DB_TABLE +"(Name, Comment, BookingDetails, ProjectKind)"
                                                                                        +"VALUES ('"+Name.getText().toString()+"', "+
                                                                                        "'"+Comment.getText().toString()+"', "+
                                                                                        "'"+BookingDetails.getText().toString()+"', "+
                                                                                        "'"+i+"')");
                        }
                        finish();
                        return true;
            }
            else
            {
                Toast toast = Toast.makeText(this, "Please enter a name for this Project!" , Toast.LENGTH_SHORT);
                toast.show();
            }
        }

        return false;
    }

}

非常感谢您的所有努力和帮助:)

1 个答案:

答案 0 :(得分:1)

在您的Projects类中,您定义了以下行

    Cursor c = myDB.rawQuery("SELECT  _id, ColumnName, 
ColumnComment, " + projectsDatabase.MY_DB_TABLE + ";", null);

因为它说,你的表中没有colunm _id ,所以请检查_id是否是表中的一列。 请确保clolun _id存在与否