gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:03:47 25 4
gpt4 key购买 nike

我试图使用android studio来使此代码正常工作,但是每次运行模拟器时,模拟器中的应用程序都会崩溃,并显示错误“应用程序已停止”。我查看了logcat并收到此错误。

Logcat:

02-11 12:39:49.826 4852-4852 / co5025.example.com.week10 E / AndroidRuntime:FATAL EXCEPTION:main
流程:co5025.example.com.week10,PID:4852
java.lang.RuntimeException:无法启动 Activity ComponentInfo {co5025.example.com.week10 / co5025.example.com.week10.MainActivity}:java.lang.ClassCastException:android.support.v7.widget.AppCompatEditText无法转换为android.widget.Button
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
在android.app.ActivityThread.-wrap12(ActivityThread.java)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:154)
在android.app.ActivityThread.main(ActivityThread.java:6077)
在java.lang.reflect.Method.invoke(本机方法)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)
在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)
在android.app.Activity.performCreate(Activity.java:6664)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
在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.java:1460)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:154)
在android.app.ActivityThread.main(ActivityThread.java:6077)
在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

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;
}
}

最佳答案

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
  • 关于java - java.lang.RuntimeException:无法启动 Activity ComponentInfo:java.lang.ClassCastException:无法转换为android.widget.Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42182724/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com