2017-04-04 73 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從代碼中。

相關問題