2017-04-04 41 views
0

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

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" /> 

UPDATE: 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) 
+0

你能告诉我们堆栈跟踪吗? –

+0

任何崩溃日志?你安装了Crashlytics吗? –

+0

请发布stacktrace和gradle buildtypes配置 –

回答

1

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

检查这条线在build.gradle文件,并确保它被设置为false

minifyEnabled false 

Proguard的在编的Android APK的一个混淆工具。但它需要一个规则文件。如果您需要详细解释,请让我知道!

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"); 
     } 
    }); 

} 
} 

只作了被删除功能的更改添加和复位和使它们形成onClickListener从代码中。

相关问题