2013-07-31 43 views
0

我試圖編寫簡單的隨機數發生器應用程序。我有隨機數發生器按鈕工作,但後來我改變了一些代碼(我認爲這與隨機數發生器按鈕無關),並且它開始崩潰並且出現「IllegalStateException:無法執行活動的方法」錯誤。從我所知道的情況來看,這個錯誤對於代碼是非常具體的,因爲我找不到適合我的代碼的任何答案。Android Java IllegalStateException:無法執行活動的方法

package com.example.randomgamechooser; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.TextView; 

public class MainScreen extends Activity { 

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


} 

public void chooseGame (View view) { 
    GameList dbUtil = new GameList(this); 
    dbUtil.open(); 
    String string = dbUtil.getRandomEntry(); 
    //TextView textView = new TextView(this); 
    TextView textView = (TextView) findViewById(R.id.chosenbox); 
    textView.setTextSize(40); 
    textView.setText(string); 
    //setContentView (textView); 
    dbUtil.close(); 
} 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main_screen, menu); 
    return true; 
} 
//starts the Game Selection activity 
public void openGames (View view) { 
    Intent intent = new Intent(this, GameSelction.class); 
    startActivity(intent); 
} 

} 

而且這是引用的遊戲列表類

package com.example.randomgamechooser; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

import java.util.Random; 

public class GameList { 

private static final String TAG = "GameList"; 

//database name 
private static final String DATABASE_NAME = "game_list"; 

//database version 
private static final int DATABASE_VERSION = 1; 

//table name 
private static final String DATABASE_TABLE = "game_list"; 

//table columns 
public static final String KEY_NAME = "name"; 
public static final String KEY_GENRE = "genre"; 
public static final String KEY_ROWID = "_id"; 

//database creation sql statement 
private static final String CREATE_GAME_TABLE = 
    "create table " + DATABASE_TABLE + " (" + KEY_ROWID + " integer primary key autoincrement, " 
    + KEY_NAME +" text not null, " + KEY_GENRE + " text not null);"; 

//Context 
private final Context mCtx; 
private DatabaseHelper mDbHelper; 
private static SQLiteDatabase mDb; 


//Inner private class. Database Helper class for creating and updating database. 

private static class DatabaseHelper extends SQLiteOpenHelper { 
    DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    // onCreate method is called for the 1st time when database doesn't exists. 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     Log.i(TAG, "Creating DataBase: " + CREATE_GAME_TABLE); 
     db.execSQL(CREATE_GAME_TABLE); 
    } 

    //onUpgrade method is called when database version changes. 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
       + newVersion); 
    } 
} 

//Constructor - takes the context to allow the database to be opened/created 
//@param ctx the Context within which to work 
public GameList(Context ctx) { 
    this.mCtx = ctx; 
} 

//This method is used for creating/opening connection 
//@return instance of GameList 
//@throws SQLException 
public GameList open() throws SQLException { 
    mDbHelper = new DatabaseHelper(mCtx); 
    mDb = mDbHelper.getWritableDatabase(); 
    return this; 
} 

//This method is used for closing the connection. 
public void close() { 
    mDbHelper.close(); 
} 


//This method is used to create/insert new game. 
//@param name 
// @param genre 
// @return long 
public long createGame(String name, String genre) { 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_NAME, name); 
    initialValues.put(KEY_GENRE, genre); 
    return mDb.insert(DATABASE_TABLE, null, initialValues); 
} 

// This method will delete game. 
// @param rowId 
// @return boolean 
public static boolean deleteGame(long rowId) { 
    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
} 


// This method will return Cursor holding all the games. 
// @return Cursor 
public Cursor fetchAllGames() { 
    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME, 
      KEY_GENRE}, null, null, null, null, null); 

} 


// This method will return Cursor holding the specific game. 
// @param id 
// @return Cursor 
// @throws SQLException 
public Cursor fetchGame(long id) throws SQLException { 
    Cursor mCursor = 
     mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, 
       KEY_NAME, KEY_GENRE}, KEY_ROWID + "=" + id, null, 
       null, null, null, null); 
    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 

    return mCursor; 
} 

public int getAllEntries() 
{ 
    Cursor cursor = mDb.rawQuery(
       "SELECT COUNT(name) FROM game_list", null); 
      if(cursor.moveToFirst()) { 
       return cursor.getInt(0); 
      } 
      return cursor.getInt(0); 

} 

public String getRandomEntry() 
{ 

    //id = getAllEntries(); 
    Random random = new Random(); 
    int rand = random.nextInt(getAllEntries()); 
    if(rand == 0) 
     ++rand; 
    Cursor cursor = mDb.rawQuery(
       "SELECT name FROM game_list WHERE _id = " + rand, null); 
      if(cursor.moveToFirst()) { 
       return cursor.getString(0); 
      } 
      return cursor.getString(0); 

} 


// This method will update game. 
// @param id 
// @param name 
// @param standard 
// @return boolean 
public boolean updateGame(int id, String name, String standard) { 
    ContentValues args = new ContentValues(); 
    args.put(KEY_NAME, name); 
    args.put(KEY_GENRE, standard); 
    return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + id, null) > 0; 
} 
} 

,這裏是錯誤日誌

07-31 14:50:45.215: D/AndroidRuntime(280): Shutting down VM 
07-31 14:50:45.215: W/dalvikvm(280): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
07-31 14:50:45.236: E/AndroidRuntime(280): FATAL EXCEPTION: main 
07-31 14:50:45.236: E/AndroidRuntime(280): java.lang.IllegalStateException: Could not execute method of the activity 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.view.View$1.onClick(View.java:2072) 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.view.View.performClick(View.java:2408) 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.view.View$PerformClick.run(View.java:8816) 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.os.Handler.handleCallback(Handler.java:587) 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.os.Handler.dispatchMessage(Handler.java:92) 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.os.Looper.loop(Looper.java:123) 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.app.ActivityThread.main(ActivityThread.java:4627) 
07-31 14:50:45.236: E/AndroidRuntime(280): at java.lang.reflect.Method.invokeNative(Native Method) 
07-31 14:50:45.236: E/AndroidRuntime(280): at java.lang.reflect.Method.invoke(Method.java:521) 
07-31 14:50:45.236: E/AndroidRuntime(280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
07-31 14:50:45.236: E/AndroidRuntime(280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
07-31 14:50:45.236: E/AndroidRuntime(280): at dalvik.system.NativeStart.main(Native Method) 
07-31 14:50:45.236: E/AndroidRuntime(280): Caused by: java.lang.reflect.InvocationTargetException 
07-31 14:50:45.236: E/AndroidRuntime(280): at com.example.randomgamechooser.MainScreen.chooseGame(MainScreen.java:23) 
07-31 14:50:45.236: E/AndroidRuntime(280): at java.lang.reflect.Method.invokeNative(Native Method) 
07-31 14:50:45.236: E/AndroidRuntime(280): at java.lang.reflect.Method.invoke(Method.java:521) 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.view.View$1.onClick(View.java:2067) 
07-31 14:50:45.236: E/AndroidRuntime(280): ... 11 more 
07-31 14:50:45.236: E/AndroidRuntime(280): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 
07-31 14:50:45.236: E/AndroidRuntime(280): at com.example.randomgamechooser.GameList.getRandomEntry(GameList.java:153) 
07-31 14:50:45.236: E/AndroidRuntime(280): ... 15 more 

回答

2

閱讀堆棧跟蹤carefuly。答案出現在最後一個「由...引起」異常處;在方法getRandomEntry

07-31 14:50:45.236: E/AndroidRuntime(280): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 
07-31 14:50:45.236: E/AndroidRuntime(280): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 
07-31 14:50:45.236: E/AndroidRuntime(280): at com.example.randomgamechooser.GameList.getRandomEntry(GameList.java:153) 
07-31 14:50:45.236: E/AndroidRuntime(280): ... 15 more 

查詢()返回空結果,而從第一個位置讀出。

+0

好吧真棒。我該如何解決? (我很抱歉,我是超級騙子沒有經驗) – Matt

+0

Yuo期待在'rand'記錄ID之一。我不確定,但_id不一定是 –

+0

您期望在'rand'記錄ID之一。我不確定,但'_id'不一定是連續的系列,從0開始(比如你的'rand'隨機數)。嘗試加載數組中的所有記錄並從中隨機獲取。像'gameNameArray [rand]'... –

相關問題