我的android代码有什么问题? [英] What is wrong with my android code?

查看:92
本文介绍了我的android代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我遇到错误的班级:

Here is the class where i am getting the error:

public class NewGame extends Activity implements OnClickListener{
    EditText word;
    Button createGame;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newgame);

        word = (EditText) findViewById(R.id.word);
        createGame = (Button) findViewById(R.id.createGame);
        createGame.setOnClickListener(this);

    }

    public void onClick(View v){
        Intent i = new Intent(NewGame.this, Game.class);
        i.putExtra("word", word.getText().toString());
        startActivity(i);
    }
}



错误是ClassCastException:android.widget.Button

以防万一,这里是newgame.xml:



The error is ClassCastException: android.widget.Button

And just in case here is newgame.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:text="Create New Game"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:gravity="center"/>

    <EditText android:hint="Enter Word"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/word"
            android:password="true"/>

    <Button android:id="@+id/createGame"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Create Game"
            android:textSize="20sp"/>
</LinearLayout>

推荐答案

如果您确定该代码中包含异常,则在这里createGame = (Button) findViewById(R.id.createGame);.您假定类型为Button,但不是.

您应该指出代码行抛出异常.您可以在调试器或异常处理程序下查看异常堆栈,并在提出问题时正确报告它.

—SA
If you''re sure that the exception is in this code, it''s here" createGame = (Button) findViewById(R.id.createGame);. You assume that the type is Button but it is not.

You should indicate the line of code throwing exception. You can look at exception stack under debugger or exception handler and report it properly when asking a question.

—SA


这篇关于我的android代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆