简体   繁体   English

java.lang.IllegalStateException:在活动类com.test.apps.test.MainActivity的onClick中找不到方法ExitApp(View)

[英]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. 因此,我是第一次使用Android Studio。 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: 但是,当我处于登录活动中时,我使用按钮OnClick调用该活动中的方法,由于某种原因,java不在该活动中查找其在Main中的查找,并抛出以下错误:

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? 我确定我只是缺少一些设置,我的理解是java首先搜索当前类而不是其父类, 为什么我的Second Activity中的一个按钮甚至在Main Activity中都查找?

AndroidManifest.xml 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 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 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 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? 您实际上是在LoginActivity的onCreate方法中设置内容视图吗? 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. 我假设您只是为了简洁起见,但是发布完整的LoginActivity和MainActivity代码可能有助于深入了解它。 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. 否则,在Activity中使用findViewById()和addOnClickListener()而不是在布局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: 您的Activity MainActivity中不存在ExitApp()方法:

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 如果要执行ExitApp()方法, LoginActivity加载正确的布局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: 因此,解决方案是在.MainActivity中使用以下代码来正确地“启动” Login Activity,并告诉Java查找该“ active”活动:

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

Question was edited to reflect the correct working code 已对问题进行编辑以反映正确的工作代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:找不到方法onClick(View) - java.lang.IllegalStateException: Could not find a method onClick(View) java.lang.IllegalStateException:在视图类上定义的 android:onClick 属性的父级或祖先上下文中找不到方法 pay(View) - java.lang.IllegalStateException: Could not find method pay(View) in a parent or ancestor Context for android:onClick attribute defined on view class java.lang.IllegalStateException: Could not find method count(View) in a parent or ancestor Context for android:onClick attribute defined on view class - java.lang.IllegalStateException: Could not find method count(View) in a parent or ancestor Context for android:onClick attribute defined on view class java.lang.IllegalStateException:在 android:onClick 属性定义的父或祖先上下文中找不到方法 play(View) - java.lang.IllegalStateException: Could not find method play(View) in a parent or ancestor Context for android:onClick attribute defined java.lang.IllegalStateException:无法在父级或祖先语句中找到方法btnSubmit(View)for android:onClick - java.lang.IllegalStateException: Could not find method btnSubmit(View) in a parent or ancestor Context for android:onClick java.lang.IllegalStateException:找不到方法saveData(view) - java.lang.IllegalStateException: Could not find a method saveData(view) java.lang.IllegalStateException:找不到方法 - java.lang.IllegalStateException: Could not find method java.lang.IllegalStateException:找不到方法? - java.lang.IllegalStateException: Could not find a method? 错误:java.lang.IllegalStateException:无法执行android:onClick的方法 - Error: java.lang.IllegalStateException: Could not execute method for android:onClick java.lang.IllegalStateException:无法执行android:onClick的方法 - java.lang.IllegalStateException: Could not execute method for android:onClick in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM