Arya1209
Arya1209

Reputation: 63

Unable to change to another activity

I want to change from main activity to login activity through button log in. i followed all the steps but it still doesn't work when i test it on my device.

this is main.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    View v = findViewById(R.id.buttonlog);
    v.setOnClickListener((View.OnClickListener) this);

}

@Override
public void onClick(View v) {
    if(v.getId()==R.id.buttonlog)
    {
        Intent intent = new Intent(this,LoginActivity.class);
        this.startActivity(intent);
    }
}}

Login activity is:

 public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

  }
}

The Logcat:

12-06 19:57:03.960 6374-6374/com.example.ancaalexandra.proiectandroidd D/ViewRootImpl@c94e23f[MainActivity]: ViewPostImeInputStage processPointer 0
12-06 19:57:03.961 6374-6374/com.example.ancaalexandra.proiectandroidd E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
12-06 19:57:03.961 6374-6374/com.example.ancaalexandra.proiectandroidd V/BoostFramework: BoostFramework() : mPerf = null
12-06 19:57:04.046 6374-6374/com.example.ancaalexandra.proiectandroidd D/ViewRootImpl@c94e23f[MainActivity]: ViewPostImeInputStage processPointer 1
12-06 19:57:04.435 6374-6374/com.example.ancaalexandra.proiectandroidd D/ViewRootImpl@c94e23f[MainActivity]: ViewPostImeInputStage processPointer 0
12-06 19:57:04.522 6374-6374/com.example.ancaalexandra.proiectandroidd D/ViewRootImpl@c94e23f[MainActivity]: ViewPostImeInputStage processPointer 1
12-06 19:57:04.791 6374-6374/com.example.ancaalexandra.proiectandroidd D/ViewRootImpl@c94e23f[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0
12-06 19:57:04.837 6374-6374/com.example.ancaalexandra.proiectandroidd D/AndroidRuntime: Shutting down VM
12-06 19:57:04.838 6374-6374/com.example.ancaalexandra.proiectandroidd E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                         Process: com.example.ancaalexandra.proiectandroidd, PID: 6374
                                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ancaalexandra.proiectandroidd/com.example.ancaalexandra.proiectandroidd.LoginActivity}: java.lang.ClassCastException: com.example.ancaalexandra.proiectandroidd.LoginActivity cannot be cast to android.view.View$OnClickListener
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                                                                                             at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
                                                                                          Caused by: java.lang.ClassCastException: com.example.ancaalexandra.proiectandroidd.LoginActivity cannot be cast to android.view.View$OnClickListener
                                                                                             at com.example.ancaalexandra.proiectandroidd.LoginActivity.onCreate(LoginActivity.java:19)
                                                                                             at android.app.Activity.performCreate(Activity.java:6956)
                                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
                                                                                             at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                             at android.os.Looper.loop(Looper.java:154) 
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:6776) 
                                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518) 
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 

I declared both activities in the manifest file. What i did wrong? thank you!

Upvotes: 0

Views: 599

Answers (2)

Maciej Białorucki
Maciej Białorucki

Reputation: 561

Your way of handling findViewById() and setting onClick listener is quite unusual.

Change this code :

View v = findViewById(R.id.buttonlog);
v.setOnClickListener((View.OnClickListener) this);

to this code :

 Button loginButton = (Button)findViewById(R.id.buttonlog);
    loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
            }
        });

And it should work just fine.

Upvotes: 2

Delpes
Delpes

Reputation: 1050

Your error is on this line:

v.setOnClickListener((View.OnClickListener) this);

which is explained in your stacktrace:

LoginActivity cannot be cast to android.view.View$OnClickListener

You are trying to cast your Activity LoginActivityto an OnClickListener, which won't work.

If your Activity implements an OnClickListener you just need to pass this :

class LoginActivity extends Activity implements OnClickListener {

    protected void onCreate(Bundle savedValues) {
        ...
        v.setOnClickListener(this)
    }

}

Upvotes: 1

Related Questions