0

java.lang.IllegalStateException:无法执行活动的方法。

我是安卓新手。想要显示数据库中存储的数据。
这是我的 Compview 类,它出现错误。

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


        DB = (new DBHelper(this)).getWritableDatabase();
        // searchText = (EditText) findViewById (R.id.searchText);

         eun = (TextView)findViewById(R.id.textV);
   Bundle bundle = getIntent().getExtras();

   String UName = bundle.getString("UserName");

   eun.setText(UName);



     }

  public void search(View view)
  {DB.isReadOnly();
   cursor = DB.rawQuery("SELECT _id, Cname, Cpost, Ccriteria, Cemail,Ccontact,Ccutoff FROM Comp WHERE Cusername LIKE ?", 
     new String[] {"%" + eun.getText().toString() + "%"});
   adapter = new SimpleCursorAdapter(
     this,
     R.layout.cmpinfo,
     cursor,
     new String[] {" Cname", "Cpost", "Ccriteria", "Cemail","Ccontact","Ccutoff"},
     new int[] {R.id.cname, R.id.cpost, R.id.ccriteria , R.id.cemail, R.id.ccontact, R.id.ccutoff});


   setListAdapter(adapter);
  }

  @Override
  public void onBackPressed() 
  {
   super.onBackPressed();
   Intent i = new Intent(CompView.this, LoginActivity.class);
   startActivity(i);
  }
  }

LOGCAT

04-09 01:32:54.322: W/dalvikvm(4302): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-09 01:32:54.342: E/AndroidRuntime(4302): FATAL EXCEPTION: main
04-09 01:32:54.342: E/AndroidRuntime(4302): java.lang.IllegalStateException: Could not execute method of the activity
04-09 01:32:54.342: E/AndroidRuntime(4302):     at android.view.View$1.onClick(View.java:2072)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at android.view.View.performClick(View.java:2408)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at android.view.View$PerformClick.run(View.java:8816)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at android.os.Handler.handleCallback(Handler.java:587)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at android.os.Looper.loop(Looper.java:123)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at java.lang.reflect.Method.invokeNative(Native Method)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at java.lang.reflect.Method.invoke(Method.java:521)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at dalvik.system.NativeStart.main(Native Method)
04-09 01:32:54.342: E/AndroidRuntime(4302): Caused by: java.lang.reflect.InvocationTargetException
04-09 01:32:54.342: E/AndroidRuntime(4302):     at com.google.data.CompView.search(CompView.java:68)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at java.lang.reflect.Method.invokeNative(Native Method)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at java.lang.reflect.Method.invoke(Method.java:521)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at android.view.View$1.onClick(View.java:2067)
04-09 01:32:54.342: E/AndroidRuntime(4302):     ... 11 more
04-09 01:32:54.342: E/AndroidRuntime(4302): Caused by: java.lang.IllegalArgumentException: column ' Cname' does not exist
04-09 01:32:54.342: E/AndroidRuntime(4302):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:312)
04-09 01:32:54.342: E/AndroidRuntime(4302):     at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:87)
04-09 01:32:54.342: E/AndroidRuntime(4302):     ... 15 more

DBHelper 类

  public class DBHelper extends SQLiteOpenHelper 


{
 private SQLiteDatabase db;
 public static final String KEY_ROWID = "_id";
 public static final String KEY_ROWID1 = "_id";
    public static final String KEY_FNAME = "firstname";
    public static final String KEY_LNAME = "lastname";
    public static final String KEY_GENDER = "gender";
    public static final String KEY_USER = "username";
    public static final String KEY_EMAIL = "email";
    public static final String KEY_CNAME="Cname"; 
    public static final String KEY_CPOST="Cpost"; 
    public static final String KEY_CCRITERIA="Ccriteria"; 
    public static final String KEY_CUSER="Cusername"; 
    public static final String KEY_CEMAIL="Cemail"; 
    public static final String KEY_CCONTACT="Ccontact"; 
    public static final String KEY_CCUTOFF="Ccutoff"; 


 DBHelper DB = null;
 private static final String DATABASE_NAME = "Sam1.db";
 private static final int DATABASE_VERSION = 2;
    public static final String DATABASE_TABLE_NAME = "JobSeeker";
    public static final String Company_Table = "Comp";
    private static final String DATABASE_TABLE_CREATE =
            "CREATE TABLE " + DATABASE_TABLE_NAME + "(" +
            "_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
            "firstname TEXT NOT NULL, lastname TEXT NOT NULL, gender TEXT NOT NULL, username TEXT NOT NULL, password TEXT NOT NULL, email TEXT NOT NULL);";
private static final String DATABASE_TABLE_CREATE2=
" create table " + Company_Table +
" (_id integer primary key autoincrement," +
" Cname TEXT NOT NULL, Cpost TEXT NOT NULL,Ccriteria TEXT NOT NULL,Cusername TEXT NOT NULL, Cpassword TEXT NOT NULL, Cemail TEXT NOT NULL,Ccontact TEXT NOT NULL,Ccutoff TEXT NOT NULL);" ;


    public DBHelper(Context context) {

  super(context, DATABASE_NAME, null, DATABASE_VERSION);
  System.out.println("In constructor");
}




@Override
public void onCreate(SQLiteDatabase db) {

 try{

  db.execSQL(DATABASE_TABLE_CREATE);
  db.execSQL(DATABASE_TABLE_CREATE2);



 }catch(Exception e){
  e.printStackTrace();
 }
}






@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 // TODO Auto-generated method stub

}




public Cursor rawQuery(String string, String[] strings) {
 // TODO Auto-generated method stub
 return null;
}




public void open() {

 getWritableDatabase(); 
}


public Cursor getDetails(String text) throws SQLException 
{

    Cursor mCursor =
            db.query(true, DATABASE_TABLE_NAME, 
              new String[]{KEY_ROWID, KEY_FNAME, KEY_LNAME, KEY_GENDER, KEY_USER, KEY_EMAIL}, 
              KEY_USER + "=" + text, 
              null, null, null, null, null);
    if (mCursor != null) 
    {
        mCursor.moveToFirst();
    }
    return mCursor;


}

public Cursor getCompDetails(String text) throws SQLException 
{

    Cursor mCursor =
            db.query(true, Company_Table, 
              new String[]{KEY_ROWID1, KEY_CNAME, KEY_CPOST, KEY_CCRITERIA, KEY_CUSER, KEY_EMAIL,KEY_CCONTACT,KEY_CCUTOFF}, 
              KEY_CUSER + "=" + text, 
              null, null, null, null, null);
    if (mCursor != null) 
    {
        mCursor.moveToFirst();
    }
    return mCursor;


}


}

这是我的welcome1 类,它工作正常并且能够从数据库中检索所有数据,但是当我对我的compview 类使用相同的方法来访问数据时它不起作用。!!

public class Welcome1 extends ListActivity
{


 protected TextView eun;
 protected SQLiteDatabase DB;
 protected Cursor cursor;
 protected ListAdapter adapter;
 protected TextView mUname;
 protected TextView mFname;
 protected TextView mLname;
 protected TextView mEmail;


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


        DB = (new DBHelper(this)).getWritableDatabase();
        // searchText = (EditText) findViewById (R.id.searchText);

         eun = (TextView)findViewById(R.id.textV);
   Bundle bundle = getIntent().getExtras();

   String UName = bundle.getString("UserName");

   eun.setText(UName);



     }

  public void search(View view)
  {
   cursor = DB.rawQuery("SELECT _id, firstname, lastname, gender, username, email FROM JobSeeker WHERE username LIKE ?", 
     new String[] {"%" + eun.getText().toString() + "%"});
   adapter = new SimpleCursorAdapter(
     this,
     R.layout.dtl,
     cursor,
     new String[] {"firstname", "lastname", "gender","username", "email"},
     new int[] {R.id.sfname, R.id.slname, R.id.sgender , R.id.suname, R.id.semail});


   setListAdapter(adapter);
  }

  @Override
  public void onBackPressed() 
  {
   super.onBackPressed();
   Intent i = new Intent(Welcome1.this, LoginActivity.class);
   startActivity(i);
  }
  }
4

1 回答 1

1

问题在于from创建时的参数adapter

adapter = new SimpleCursorAdapter(
    this,
    R.layout.cmpinfo,
    cursor,
    new String[] {" Cname", "Cpost", "Ccriteria", "Cemail","Ccontact","Ccutoff"},
    new int[] {R.id.cname, R.id.cpost, R.id.ccriteria , R.id.cemail, R.id.ccontact, R.id.ccutoff});

看看你在哪里创建String[]数组......

new String[] {" Cname", "Cpost", "Ccriteria", "Cemail","Ccontact","Ccutoff"}

...你有一个spacebefore Cname

logact 输出显示了这个...

column ' Cname' does not exist
于 2012-04-08T21:23:32.167 回答