生成的 APK 无法正常工作

the generated APK is not working properly

这是我在 PlayStore 上部署的第一个应用程序,我在生成的签名 APK 方面遇到了一些问题。

当我 运行 在 phone (API 21 Android 5.0.1 上使用 android studio 2.3.3(使用 ADB)时),该应用程序运行良好,没有任何问题。当我生成 apk(选中 V1 和 V2)并将其安装在同一个 phone 上时,它显示启动画面,然后当我按下按钮(登录)时,应用程序关闭。

我试过用Android监控看原因,监控说(Method login (onClick method for login button)) is not implemented,但是实现了。

 java.lang.IllegalStateException: Could not find method login(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'login_button'

方法的header是

protected void login(View v) throws IOException, InterruptedException {}

登录方式

// Login button onClick method
protected void login(View v) throws IOException, InterruptedException {
    if (checkBox.isChecked()){
        // save password
        SharedPreferences sharedPreferences = 
        Login.this.getSharedPreferences(
                "LOGIN_INFO",MODE_PRIVATE
        );
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("PASSWORD",event_key_edittext.getText().toString());
        editor.putBoolean("CHECKBOX_VALUE",checkBox.isChecked());
        editor.commit();
    }
    else{
        // clear sharedprefrences
        SharedPreferences sharedPreferences = 
        Login.this.getSharedPreferences(
                "LOGIN_INFO",MODE_PRIVATE
        );
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }
    // get the key
    event_key = event_key_edittext.getText().toString();
    // check internet before checking with the api
    if (internetConnectionAvailable(2000)) new VerifyLoginTask().execute();
    // if no internet, show msg
    else internet_msg.setVisibility(View.VISIBLE);
}

XML 登录 Activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context="agha.ticket_app.Login">

<ImageView
    android:id="@+id/login_circles"
    android:layout_width="match_parent"
    android:layout_height="105dp"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:src="@drawable/dectop" />

<ImageView
    android:id="@+id/login_icon"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_below="@id/login_circles"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:src="@drawable/app_icon" />

<TextView
    android:id="@+id/login_txt_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/login_icon"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="@string/login_main_txt"
    android:textColor="@color/colorGreen"
    android:textSize="20dp" />

<TextView
    android:id="@+id/login_txt_password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/login_txt_login"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="@string/login_sub_txt"
    android:textColor="@color/colorPink"
    android:textSize="10dp" />

<EditText
    android:id="@+id/login_edit_password"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_below="@id/login_txt_password"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:hint="@string/login_edittext_hint"
    android:inputType="text"
    android:maxLines="1"
    android:singleLine="true"
    android:textColor="@color/colorPink"
    android:textColorHint="@color/colorPink"
    android:textColorLink="@color/colorGreen"
    android:textSize="15dp"
    android:theme="@style/MyEditTextTheme" />

<TextView
    android:id="@+id/login_error_txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/login_edit_password"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:text="@string/login_incorrect_pass"
    android:textColor="#011627"
    android:textStyle="bold"
    android:visibility="invisible" />

<TextView
    android:id="@+id/login_internet_txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/login_edit_password"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:text="@string/connection_error"
    android:textColor="#011627"
    android:textStyle="bold"
    android:visibility="invisible" />

<CheckBox
    android:id="@+id/login_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/login_error_txt"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:buttonTint="@color/colorPink"
    android:text="@string/login_check_box"
    android:textColor="@color/colorGreen"
    android:textSize="10dp" />

<Button
    android:id="@+id/login_button"
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:layout_below="@id/login_checkbox"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="25dp"
    android:background="@drawable/login_button_bg"
    android:elevation="15dp"
    android:onClick="login"
    android:text="@string/login_button"
    android:textColor="#FFFFFF" />

    </RelativeLayout>

我该如何解决这个问题?

如果在 Android Studio 中 运行 没问题,请不要仅以这种方式进行测试。 Android 如果您执行 "Run" 或 "Debug",Studio 并不总是执行应用程序的完整重新安装。相反,它会在 运行 时修补应用程序,应用程序甚至可能 运行 正在修补(又名 "Instant Run")。

因此您应该从设备上卸载该应用程序,然后重新安装以进行测试。这应该与用户从 Google Play 加载的环境相似。

此外,您可以创建签名的 APK 并通过 adb 手动将其安装到设备上。同样不要忘记卸载其他版本的应用程序,以便您重新开始。否则,私有应用程序目录中的现有文件可能会更改您的应用程序的行为。

问题不在于 apk 的登录按钮。

按照以下步骤操作:

  1. 在您的 class
  2. 中实施 OnClickListener
  3. 在onCreate()中初始化按钮 Button button = (Button) findViewById(R.id.login_button);
  4. 设置按钮的setOnClickListener()方法 button.setOnClickListener()
  5. 然后在oncreate()之外调用方法 public void accountButton (View v){ //do some thing which you want }

希望这能奏效。