我是 android 开发新手,在 Hang-Man 游戏中遇到了一些问题。
这就是我希望它工作的方式: 1. 当应用程序启动时,打开数据库 (db.open()) 并从数据库 (db.random()) 中获取一个随机单词并放入生成的单词字符串。然后将 generatedWord 制作成 Spannablestring 文本 (SpannableString text = new SpannableString(generatedWord);) 并将整个文本制作成白色/不可见的颜色。
当用户在 textview userInput 中输入一个字符时,我想通过使用 onKeyListener 将它保存到 lastInput 变量:lastInput = (char)event.getUnicodeChar();
然后当按下 Enter 按钮时,应该调用 guess() 来发送 lastInput 的参数。在那里它检查 lastInput 是否在 indexOf generatedWord 中,如果是,它将使字符的文本再次变黑并再次可见,如果不是,它应该调用 showImages() 从而使图像可见。
事情是这样的!数据库正在工作,我刚刚让应用程序与 gui 和所有东西一起运行,然后我做了一些小改动,现在我无法再次启动应用程序,只会出现致命错误。我做错了什么,我能想到什么?
非常感谢
// 主要活动
package com.emanuelolsson.simplehangman;
import database.DBAdapter;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class HangMan extends Activity {
// DECLARE NEEDED VARIABLES
private String generatedWord = "";
private char lastInput = '\0';
private Button newGameButton, enterLetterButton;
private EditText wordHolder, userInput;
private ImageView imageOne, imageTwo, imageThree, imageFour, imageFive, imageSix, imageSeven, imageEight, winner, hanged;
private SpannableString text = new SpannableString(generatedWord);
private int count = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hang_man);
DBAdapter db = new DBAdapter(this);
// ASSIGN OBJECTS
newGameButton = (Button) findViewById(R.id.newGame);
enterLetterButton = (Button) findViewById(R.id.enter);
wordHolder = (EditText) findViewById(R.id.wordHolder);
userInput = (EditText) findViewById(R.id.userInput);
imageOne = (ImageView) findViewById(R.id.imageView1);
imageTwo = (ImageView) findViewById(R.id.imageView2);
imageThree = (ImageView) findViewById(R.id.imageView3);
imageFour = (ImageView) findViewById(R.id.imageView4);
imageFive = (ImageView) findViewById(R.id.imageView5);
imageSix = (ImageView) findViewById(R.id.imageView6);
imageSeven = (ImageView) findViewById(R.id.imageView7);
imageEight = (ImageView) findViewById(R.id.imageView8);
winner = (ImageView) findViewById(R.id.winner);
hanged = (ImageView) findViewById(R.id.hanged);
imageOne.setVisibility(View.INVISIBLE);
imageTwo.setVisibility(View.INVISIBLE);
imageThree.setVisibility(View.INVISIBLE);
imageFour.setVisibility(View.INVISIBLE);
imageFive.setVisibility(View.INVISIBLE);
imageSix.setVisibility(View.INVISIBLE);
imageSeven.setVisibility(View.INVISIBLE);
imageEight.setVisibility(View.INVISIBLE);
winner.setVisibility(View.INVISIBLE);
hanged.setVisibility(View.INVISIBLE);
// ADD LISTENERS
newGameButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
}
});
enterLetterButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
guess(lastInput);
}
});
userInput.setOnKeyListener(new View.OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
lastInput = (char)event.getUnicodeChar();
return false;
}
});
db.open();
db.randomize(generatedWord);
SpannableString text = new SpannableString(generatedWord);
text.setSpan(new ForegroundColorSpan(Color.WHITE), 0, text.length(), 0);
wordHolder.setText(text);
db.close();
}
// What happens when user clicks Enter
// If the guess (char) is in the index of generated word and not -1 it will span back hopefully to all char
public void guess(char guess) {
guess = Character.toUpperCase(guess);
if (generatedWord.indexOf(lastInput) != -1) {
text.setSpan(new ForegroundColorSpan(Color.BLACK), text.charAt(lastInput), text.charAt(lastInput), 0);
//guessedChars.add(guess);
} else {
showImages();
}
}
private void showImages() {
count ++;
if (count == 1)
{
imageOne.setVisibility(View.INVISIBLE);
}
else if (count == 2)
{
imageTwo.setVisibility(View.INVISIBLE);
}
else if (count == 3)
{
imageThree.setVisibility(View.INVISIBLE);
}
else if (count == 4)
{
imageFour.setVisibility(View.INVISIBLE);
}
else if (count == 5)
{
imageFive.setVisibility(View.INVISIBLE);
}
else if (count == 6)
{
imageSix.setVisibility(View.INVISIBLE);
}
else if (count == 7)
{
imageSeven.setVisibility(View.INVISIBLE);
}
else if (count == 8)
{
gameEnd();
}
}
private void gameEnd() {
if (count == 8)
{
hanged.setVisibility(View.VISIBLE);
}
else
{
winner.setVisibility(View.VISIBLE);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_hang_man, menu);
return true;
}
}
// 数据库适配器
package database;
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;
public class DBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_WORDS = "words";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "dbnames";
private static final String DATABASE_TABLE = "tblnames";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table tblnames (_id integer primary key autoincrement, "
+ "words 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);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS tblnames");
onCreate(db);
}
}
// Open DB
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
// Close DB
public void close()
{
DBHelper.close();
}
// INsert name
public long insertWord(String word)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_WORDS, word);
return db.insert(DATABASE_TABLE, null, initialValues);
}
// Delete a name
public boolean deleteWord(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}
// Get all names
public Cursor getAllWords()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_WORDS},
null,
null,
null,
null,
null);
}
// Get a certain name
public Cursor getWord(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_WORDS},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
// Update name
public boolean updateWord(long rowId, String word)
{
ContentValues args = new ContentValues();
args.put(KEY_WORDS, word);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
// Clears DB
public void clear() {
db.delete(DATABASE_TABLE, "1", null);
}
public void randomize(String word) {
@SuppressWarnings("unused")
Cursor cursor;
cursor = this.db.query("tblnames ORDER BY RANDOM() LIMIT 1", new String[] { "*" }, null, null, null, null, null);
}
}
// 布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:src="@drawable/one"
android:contentDescription="@string/picOne" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:src="@drawable/two"
android:contentDescription="@string/picTwo" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picThree"
android:src="@drawable/three" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:src="@drawable/four"
android:contentDescription="@string/picFour" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:src="@drawable/five"
android:contentDescription="@string/picFive" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:src="@drawable/six"
android:contentDescription="@string/picSix" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:src="@drawable/seven"
android:contentDescription="@string/picSeven" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:src="@drawable/eight"
android:contentDescription="@string/picEight" />
<EditText
android:id="@+id/wordHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_below="@+id/wordHolder"
android:layout_marginTop="28dp"
android:text="@string/enter" />
<EditText
android:id="@+id/userInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/enter"
android:layout_alignLeft="@+id/wordHolder"
android:layout_alignTop="@+id/enter"
android:layout_toLeftOf="@+id/enter"
android:ems="10"
android:inputType="text" />
<ImageView
android:id="@+id/winner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_centerHorizontal="true"
android:src="@drawable/winner"
android:contentDescription="@string/winner" />
<ImageView
android:id="@+id/hanged"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_centerHorizontal="true"
android:src="@drawable/hanged"
android:contentDescription="@string/hanged" />
<Button
android:id="@+id/newGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/enter"
android:layout_alignBottom="@+id/enter"
android:layout_toRightOf="@+id/enter"
android:text="@string/newGame" />
</RelativeLayout>