2014-09-22 62 views
0

我正在构建一个Android应用程序,基本上我希望它可以在屏幕上随意点击(任何地方)。现在我试图将RelativeLayout设置为android:clickable="true"和我的TextView相同,但是当我尝试启动Activity时,应用程序崩溃。我希望它显示时间,让我们说30秒,然后在这30秒内计算点击次数。需要onclick帮助

XML

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.orion.peky.thetapgame.Game">  

     <TextView 
      android:text="Tap to start" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_centerHorizontal="true" 
      android:textSize="50dp" 
      android:textColor="#FFFFFFFF" 
      android:id="@+id/tekst" /> 

    </RelativeLayout> 

活动

MAIN package com.orion.peky.thetapgame; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.TextView; 



public class Game extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_game); 
    } 
    TextView tekst=(TextView)findViewById(R.id.tekst); 
    int brojac=0; 

    public void broji(View view){ 
     brojac=brojac+1; 
     tekst.setText("Tapped "+brojac+" times"); 
    } } 
+0

安置自己的错误日志 – 2014-09-22 19:44:10

+0

我不能得到它,我的笔记本电脑无法运行emulatio,我不能运行它在手机上同时连接到笔记本电脑... BTW我删除了android:clickable =“true”和android:onclick =“broji”来运行它没有它... – Peky5 2014-09-22 19:45:12

+0

移动'tekst =(TextView)findViewById R.id.tekst);'在'setContentView' – 2014-09-22 19:47:51

回答

0

你必须与onCreate()方法超出此行错误:

TextView tekst=(TextView)findViewById(R.id.tekst); 
int brojac=0; 

把内方法,声明TextView tekst作为一个类变量,然后你可以使用它你broji()方法太里面:

public class Game extends Activity { 

private TextView tekst; 
private int brojac=0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_game); 
     tekst=(TextView)findViewById(R.id.tekst); 
    } 

    public void broji(View view){ 
     brojac=brojac+1; 
     tekst.setText("Tapped "+brojac+" times"); 
    } 
} 
+1

之后的活动内部onCreate,并且在这个世界中,没有人能解释任何东西。事实上,你值得+1没有任何解释:) – 2014-09-22 19:49:09

+0

现在工作我知道我有一个错误,但没有注意到它是一个全局变量xd谢谢 – Peky5 2014-09-22 19:56:36