生成的APK无法正常工作
这是我第一次部署在PlayStore上的应用程序,而且我正在面对有关生成的已签名APK的一些问题。生成的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>
我怎样才能解决这个问题?
问题不在于它的apk登录按钮。
遵循以下步骤:
- 在你的类实现
OnClickListener
- 初始化的onCreate()
Button button = (Button) findViewById(R.id.login_button);
- 按钮设置
setOnClickListener()
方法按钮button.setOnClickListener()
- 话外调用方法的
oncreate()
public void accountButton (View v){ //do some thing which you want }
希望这可能工作。
这适用于我 –
如果它在Android Studio中运行良好,请不要仅以此方式对其进行测试。 如果您执行“运行”或“调试”,Android Studio并不总是执行完整的应用程序重新安装。相反,它在运行时为应用程序打补丁,应用程序甚至可以在修补时运行(即“即时运行”)。
因此,您应该从设备上卸载应用程序,然后再次安装它进行测试。这应该与用户从Google Play加载的环境类似。
此外,您可以创建签名的APK,并通过adb手动将其安装到设备上。再次不要忘记卸载其他版本的应用程序,以便您重新开始。否则私有应用程序目录中的现有文件可能会改变您的应用程序的行为。
发布一些代码例如包含'login_button'的xml代码和Activity的'login()'方法以及其他相关代码。 – Abbas
您的登录()声明在哪里? 你的xml在哪里,其中login()用作onClick方法? – Hetfieldan24
我刚刚用xml和java代码更新了这个问题 –