Thursday, January 31, 2019

java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.ClassCastException: cannot be cast to android.widget.Button

I tried to get this code working using android studio but the app in the emulator crashes everytime I run it and displays an error which is "App has stopped". I looked at the logcat and got this error.



Logcat:



02-11 12:39:49.826 4852-4852/co5025.example.com.week10 E/AndroidRuntime: FATAL EXCEPTION: main
Process: co5025.example.com.week10, PID: 4852
java.lang.RuntimeException: Unable to start activity ComponentInfo{co5025.example.com.week10/co5025.example.com.week10.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatEditText cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatEditText cannot be cast to android.widget.Button
at co5025.example.com.week10.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at 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;
}
}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...