Я получаю исключение, когда я запускаю свое приложение, хотя нет синтаксической ошибки, и имена идентификаторов и переменных совпадают. Я попытался восстановить файл R, очистить проект, перезапустить эмулятор, Eclipse, ничего не получилось. Существует только одна кнопка, и даже комментирование кода, который включает кнопку, не решает проблему. Я новичок, так что медведь со мной :)
Это моя основная деятельность:
package com.android.hangman;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.text.InputFilter.LengthFilter;
import android.view.View;
import android.widget.*;
public class Hangman extends Activity {
TextView word, tries, cuvant, incercari, letter;
EditText litera;
Button newG;
int valIncercari = 5;
ArrayList<String> cuvinte = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
word = (TextView)findViewById(R.id.word);
tries = (TextView)findViewById(R.id.tries);
cuvant = (TextView)findViewById(R.id.cuvant);
incercari = (TextView)findViewById(R.id.incercari);
letter = (TextView)findViewById(R.id.letter);
newG = (Button)findViewById(R.id.newG);
litera = (EditText)findViewById(R.id.newG);
AssetManager am = this.getAssets();
try {
InputStream is = am.open("cuvinte.txt");
InputStreamReader inputStreamReader = new InputStreamReader(is);
BufferedReader b = new BufferedReader(inputStreamReader);
String rand;
while((rand=b.readLine())!=null){
cuvinte.add(rand);
}
} catch (IOException e) {
Toast.makeText(this, "No words file", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
public void newGame(View view){
Random rand = new Random();
String stringCuvant = cuvinte.get(rand.nextInt(cuvinte.size()));
cuvant.setText("");
for(int i = 0; i< stringCuvant.length(); i++){
cuvant.append("_ ");
}
incercari.setText(valIncercari);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Word"/>
<TextView
android:id="@+id/tries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tries"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/cuvant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
<TextView
android:id="@+id/incercari"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/letter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Letter"/>
<EditText
android:id="@+id/litera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<Button
android:id="@+id/newG"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="NEW GAME"
android:onClick="newGame"
android:gravity="bottom"/>
</LinearLayout>
И исключение:
06-25 11:22:35.402: E/AndroidRuntime(337): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.hangman/com.android.hangman.Hangman}: java.lang.ClassCastException: android.widget.Button
Заранее благодарю за ваши ответы!
litera = (EditText)findViewById(R.id.newG);
вы нажимаете кнопку Edittext
litera = (EditText)findViewById(R.id.newG);
Эта строка называет EditText, в то время как идентификатор для кнопки, вы должны изменить его на:
litera = (EditText)findViewById(R.id.litera);
litera = (EditText)findViewById(R.id.newG);
Должно быть изменено на
litera = (EditText)findViewById(R.id.litera);
id newG is 'twice'
newG = (Button)findViewById(R.id.newG);
litera = (EditText)findViewById(R.id.newG);
должно быть
litera = (EditText)findViewById(R.id.litera);
Вы получаете исключение ClassCastException, поскольку пытаетесь наложить кнопку на EditText, что, очевидно, невозможно.
Итак, измените свой код на
litera = (EditText)findViewById(R.id.litera);