首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >java.lang.RuntimeException:无法启动活动ComponentInfo: java.lang.ClassCastException:无法转换为android.widget.Button

java.lang.RuntimeException:无法启动活动ComponentInfo: java.lang.ClassCastException:无法转换为android.widget.Button
EN

Stack Overflow用户
提问于 2017-02-12 00:02:25
回答 2查看 8.8K关注 0票数 2

我试着使用android让这段代码正常工作,但是每次我运行它时,模拟器中的应用程序都会崩溃,并显示一个错误,即"App已停止“。我看了一下逻辑猫,得到了这个错误。

逻辑猫:

02-11 12:39:49.826 4852-4852/co5025PID: 4852 java.lang.RuntimeException:无法启动活动java.lang.ClassCastException: android.support.v7.widget.AppCompatEditText不能转换为android.widget.Button在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)在android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:( android.app.ActivityThread.main(ActivityThread.java:6077) ) android.os.Looper.loop(Looper.java:154)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)的java.lang.reflect.Method.invoke(原生方法)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)中,由: java.lang.ClassCastException: android.support.v7.widget.AppCompatEditText引起的不能转换为android.widget.Button在co5025.example.com.week10.MainActivity.onCreate(MainActivity.java:42) at android.app.Activity.performCreate(Activity.java:6664)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)在android.app.ActivityThread.-wrap12(ActivityThread.java)在android.app.ActivityThread$H.handleMessage(ActivityThread(android.os.Handler.dispatchMessage(Handler.java:102)).java:1460(在android.os.Looper.loop(Looper.java:154))在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(本地方法)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)(在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755))

MainActivity.java

代码语言:javascript
复制
package co5025.example.com.week10;

            import android.app.FragmentManager;
            import android.content.Intent;
            import android.os.Bundle;
            import android.support.design.widget.FloatingActionButton;
            import android.support.design.widget.Snackbar;
            import android.support.v7.app.AppCompatActivity;
            import android.support.v7.widget.Toolbar;
            import android.view.Menu;
            import android.view.MenuItem;
            import android.view.View;
            import android.widget.Button;
            import android.widget.EditText;
            import android.widget.TextView;
            import android.support.v4.app.FragmentManager.BackStackEntry;

            public class MainActivity extends AppCompatActivity implements View.OnClickListener {
                EditText editUsername;
                EditText editPassword;
                TextView errorMessage;
                Button butLogin;
                private FragmentManager.BackStackEntry v;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                    setSupportActionBar(toolbar);

                    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                    fab.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                                    .setAction("Action", null).show();
                        }
                    });
                    editPassword    =   (EditText)  findViewById(R.id.edit_password);
                    editUsername    =   (EditText)  findViewById(R.id.edit_username);
                    butLogin    =   (Button) findViewById(R.id.edit_username);
                    butLogin.setOnClickListener(this);

                    if (getIntent().getBooleanExtra("EXIT", false)) finish();
                }

                @Override
                public void onClick(View view) {
                    if  (checkPassword())   {
                        Intent  i;
                        switch  (v.getId()) {
                            case    R.id.but_login:
                                i   =   new Intent(this,    GameActivity.class);
                                startActivity(i);
                                break;
                        }
                    }else{
                        errorMessage    =   (TextView)  findViewById(R.id.error_message);
                        errorMessage.setText("Error: Incorrect Password/Username. Try again!");
                    }
                }

                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                    // Inflate the menu; this adds items to the action bar if it is present.
                    getMenuInflater().inflate(R.menu.menu_main, menu);
                    return true;
                }

                @Override
                public boolean onOptionsItemSelected(MenuItem item) {
                    // Handle action bar item clicks here. The action bar will
                    // automatically handle clicks on the Home/Up button, so long
                    // as you specify a parent activity in AndroidManifest.xml.
                    int id = item.getItemId();

                    //noinspection SimplifiableIfStatement
                    if (id == R.id.action_settings) {
                        return true;
                    }

                    return super.onOptionsItemSelected(item);
                }

                public  boolean checkPassword(){
                    if  (editUsername.getText().toString().equals("test")   &&
                            (editPassword.getText().toString().equals("1234")))
                        return  true;
                    else
                        return  false;
                }
            }
EN

回答 2

Stack Overflow用户

发布于 2017-02-12 00:11:25

Stacktrace告诉您出了什么问题:AppCompatEditText cannot be cast to android.widget.Button --用于按钮的findByViewId用于EditText

这些都是相同的:

editUsername =(EditText) findViewById(R.id.edit_username);

butLogin = (Button) findViewById(R.id.edit_username);

  • 从xml布局中将Button更改/指向正确的id
票数 2
EN

Stack Overflow用户

发布于 2017-06-08 12:58:26

根据这一点: java.lang.ClassCastException: android.support.v7.widget.AppCompatEditText不能在android.app上转换为android.widget.Button

您正在将Button butLogin转换为EditText

editUsername = (EditText) findViewById(R.id.edit_username);butLogin = (Button) findViewById(R.id.edit_username);

在这里,您要将相同的Id转换为editUsername和butLogin

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42182724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档