【问题标题】:the generated APK is not working properly生成的 APK 无法正常工作
【发布时间】:2017-08-25 12:36:30
【问题描述】:

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

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

我尝试使用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'

方法的头是

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

<?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>

我该如何解决这个问题?

【问题讨论】:

  • 发布一些代码,例如xml 代码包含 login_button 和 Activity 的 login() 方法以及其他相关代码。
  • 你的 login() 是在哪里声明的?你的xml在哪里,login()在哪里用作onClick方法?
  • 我刚刚用 xml 和 java 代码更新了问题
  • 我的 login() 在 Login 类中声明,对应于上述 XML。请注意,当我在 android studio 中运行该应用程序时,它运行良好。

标签: java android apk signed-apk


【解决方案1】:

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

请按照以下步骤操作:

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

希望这可能有效。

【讨论】:

    【解决方案2】:

    如果它在 Android Studio 中运行良好,请不要仅以这种方式进行测试。 如果您执行“运行”或“调试”,Android Studio 并不总是执行应用程序的完整重新安装。相反,它在运行时修补应用程序,应用程序甚至可能在修补时运行(也称为“即时运行”)。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      • 2020-10-10
      • 2019-03-14
      • 2015-12-02
      • 2020-01-15
      • 2016-05-29
      相关资源
      最近更新 更多