简体   繁体   中英

java.lang.IllegalStateException: Could not find a method ExitApp(View) in the activity class com.test.apps.test.MainActivity for onClick

So I'm using Android Studio for the first time. I'm got two Activities, when the Main Activity Loads it checks for a login flag set, if its not then it loads a Login Activity.

However, when I'm in the Login activity and I use a button OnClick to call a method which is in that Activity and for some reason, java is not looking in that Activity its looking in Main and throwing the following error:

java.lang.IllegalStateException: Could not find a method ExitApp(View) in the activity class com.test.apps.test.MainActivity for onClick

I'm sure I'm just missing some setting I am missing, my understanding is that java searches the current class first than its parent class, why is a button in my Second Activity even looking in my Main Activity?

AndroidManifest.xml

<activity
android:name=".MainActivity"
android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".LoginActivity"
        android:label="@string/title_activity_login"
        android:windowSoftInputMode="adjustResize|stateVisible" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
    </activity>

MainActivity.Java

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String LoggedIn = preferences.getString("Logged_In", "");
    if (LoggedIn.equals("")) {
       Intent intent = new Intent(this, LoginActivity.class);
       startActivity(intent);
    }
    else {
        setContentView(R.layout.activity_main);
    }
}

LoginActivity.java

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.app.Activity;

public class LoginActivity extends ActionBarActivity {
private EditText username;
private EditText password;

public void ExitApp(View view){
    finish();
}

activity_login.xml

<Button
android:layout_width="250dp"
android:layout_height="50dp"
android:text="Login"
android:id="@+id/btnLogin"
android:layout_below="@+id/txtPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:nestedScrollingEnabled="false"
android:onClick="ExitApp"
android:clickable="true"/>

Any help would be great...

Try to stop your app with an Intent

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Are you actually setting the content view in your LoginActivity's onCreate method? I assume you just cut it out for brevity on here, but maybe posting the full LoginActivity and MainActivity code would help get to the bottom of it. Unless I'm missing some detail, everything you've posted looks like it should work well enough.

Otherwise, there's really no disadvantage to using findViewById() and addOnClickListener() in your Activity instead of doing it in the layout xml.

    View loginButton = findViewById(R.id.btnLogin);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ExitApp();
        }
    });

Your method ExitApp() doesn´t exist in your Activity MainActivity:

Could not find a method ExitApp(View) in the activity class com.test.apps.test.MainActivity for onClick  

If you want execute the method ExitApp() from LoginActivity load the correct layout activity_login.xml

public class LoginActivity extends ActionBarActivity {

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

So the solution is to use the following code in the .MainActivity to "start" the Login Activity properly and tell java to look in that "active" activity:

Intent intent = new Intent(this, LoginActivity.class); 
startActivity(intent); 

Question was edited to reflect the correct working code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM