java Android应用程序可以在调试中工作,但不能发布
我正在将我的第一个程序移植到安卓上,这个应用程序是一个简单的按钮点击器,它在前面的数字上加1,以计算你点击的次数。当我试图运行代码的发布版本时,当我试图按下按钮时,它会崩溃。我对调试版本没有任何问题
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView text;
Button adder;
Button reset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
adder = (Button) findViewById(R.id.button01);
reset = (Button) findViewById(R.id.button02);
}
protected void add(View view)
{
int count;
count = Integer.parseInt((String) text.getText()) + 1;
text.setText("" + count);
}
protected void reset(View v)
{
text.setText("0");
}
}
这是XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/activity_main"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="person.clicker.MainActivity">
<Button 安卓:id="@+id/button01"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Click"
安卓:onClick="add"
安卓:layout_above="@+id/button02"
安卓:layout_centerHorizontal="true"
安卓:layout_marginBottom="59dp" />
<Button 安卓:id="@+id/button02"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="reset"
安卓:onClick="reset"
安卓:layout_marginBottom="136dp"
安卓:layout_alignParentBottom="true"
安卓:layout_alignLeft="@+id/button01"
安卓:layout_alignStart="@+id/button01" />
<TextView 安卓:id="@+id/text"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="0"
安卓:layout_alignParentTop="true"
安卓:layout_centerHorizontal="true"
安卓:layout_marginTop="49dp" />
更新: 格拉德尔。建造
apply plugin: 'com.安卓.application'
安卓 {
signingConfigs {
config {
keyAlias 'austin'
keyPassword 'Austin42'
storeFile file('D:/key.jks')
storePassword 'Austin42'
}
}
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "austinhenley.clicker"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
multiDexEnabled true
versionName "1.0"
signingConfig signingConfigs.config
}
buildTypes {
release {
minifyEnabled false
}
}
productFlavors {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
安卓TestCompile('com.安卓.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.安卓.support', module: 'support-annotations'
})
wearApp project(':wear')
compile 'com.安卓.support:appcompat-v7:25.3.1'
compile 'com.安卓.support:multidex:1.0.1'
testCompile 'junit:junit:4.12'
}
我已经能够在发布版本上运行调试器,这就是按下按钮时发生的情况
E/AndroidRuntime: FATAL EXCEPTION: main
Process: austinhenley.clicker, PID: 31983
java.lang.IllegalStateException: Could not find method add(View) in a parent or ancestor Context for 安卓:onClick attribute defined on view class 安卓.support.v7.widget.AppCompatButton with id 'button01'
at 安卓.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at 安卓.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at 安卓.view.View.performClick(View.java:5637)
at 安卓.view.View$PerformClick.run(View.java:22433)
at 安卓.os.Handler.handleCallback(Handler.java:751)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:154)
at 安卓.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:776)
# 1 楼答案
我已经找到了解决方案,我遵循了android开发者的参考资料,并尝试在xml中使用
android.onClick
函数,使代码更加整洁。然而,在发布代码中,这不起作用。我将其更改为:所做的唯一更改是删除add和reset函数,并将它们从代码中放入onClickListener
# 2 楼答案
如果它在调试中工作,而不是在发布中,可能是proguard的问题
检查
build.gradle
文件中的这一行,并确保它设置为false
Proguard是用于编译的android APK的模糊工具。但它需要一个规则文件。如果你需要详细的解释,一定要让我知道