Hi i have a problem and i dont know what wrong whit the code
I'm trying to get the String in the MainActivity2 to create a "login" page and the MyApplication should be the password so i can change it with a set in another Activity
Error :
Process: com.example.test1, PID: 23014
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test1/com.example.test1.MainActivity2}: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.test1.MyApplication
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.test1.MyApplication
at com.example.test1.MainActivity2.onCreate(MainActivity2.java:28)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
MyApplication Activity:
package com.example.test1;
import android.app.Application;
public class MyApplication extends Application {
private String vpassword = "1234";
public String getSomeVariable() {
return vpassword;
}
public void setSomeVariable(String vpassword) {
this.vpassword = vpassword;
}
}
MainActivity2
package com.example.test1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.button.MaterialButton;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
EditText username=(EditText) findViewById(R.id.et_email1);
EditText password=(EditText) findViewById(R.id.et_password1);
Button button2 = (Button) findViewById(R.id.button2);
TextView tv_cl2 = (TextView) findViewById(R.id.tv_cl2);
String s = ((MyApplication) this.getApplication()).getSomeVariable();
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(username.getText().toString().equals("user") && password.getText().toString().equals(s)){
//correct
Intent intent = new Intent(MainActivity2.this, MainActivity4.class);
startActivity(intent);
Toast.makeText(MainActivity2.this, "Login SUCCESSFUL", Toast.LENGTH_SHORT).show();
}else{
//incorrect
Toast.makeText(MainActivity2.this, "Login FAILED!!", Toast.LENGTH_SHORT).show();
}
}
});
tv_cl2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity2.this, MainActivity3.class);
startActivity(intent);
}
});
}
}
CodePudding user response:
Add android:name="com.example.test1.MyApplication"
to your <application>
element in your manifest. Without that, Android will not know to use MyApplication
.
For more, see https://developer.android.com/guide/topics/manifest/application-element#nm and https://developer.android.com/reference/android/app/Application.