Android应用程序在调试中工作,但不能发布

时间:2017-04-04 17:29:08

标签: java android

我将我的第一个程序移植到android,该应用程序是一个简单的按钮点击器,它将前一个数字加1,以计算您点击的次数。当我尝试运行代码的发布版本时,当我尝试按下按钮时它会崩溃。我对调试版本没有任何问题。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.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:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="person.clicker.MainActivity">

<Button android:id="@+id/button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click"
    android:onClick="add"
    android:layout_above="@+id/button02"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="59dp" />

<Button android:id="@+id/button02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="reset"
    android:onClick="reset"
    android:layout_marginBottom="136dp"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/button01"
    android:layout_alignStart="@+id/button01" />

<TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="49dp" />

更新: gradle.build

apply plugin: 'com.android.application'

android {
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')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
wearApp project(':wear')
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.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 android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button01'
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                  at android.view.View.performClick(View.java:5637)
                  at android.view.View$PerformClick.run(View.java:22433)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6126)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

2 个答案:

答案 0 :(得分:1)

如果它在调试中工作而不是在发布中,可能是一个proguard问题。

build.gradle文件中检查此行,并确保将其设置为false

minifyEnabled false

Proguard用于编译android apks的混淆工具。但它需要一个规则文件。如果你需要详细的解释,请告诉我!

答案 1 :(得分:0)

我找到了解决方案,我正在关注android开发人员参考,我尝试在xml中使用android.onClick函数使其在代码中更加整洁。但是在发布代码中,这不起作用。我更改了以下内容:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.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);
    adder.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            int count;
            count = Integer.parseInt((String) text.getText()) + 1;
            text.setText("" + count);
        }
    });
    reset = (Button) findViewById(R.id.button02);
    reset.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            text.setText("0");
        }
    });

}
}

所做的唯一更改是删除函数add and reset并将它们放入代码中的onClickListener。