触摸空白区域时活动崩溃

时间:2014-04-02 04:51:09

标签: android android-activity

我有一个TextView和一个随机范围的按钮。当我按下按钮时,一切都很酷,但如果我触摸屏幕的任何其他部分,应用程序崩溃并重新启动。

这是我的java

public class Jugar extends Activity {

TextView t;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.jugar);}

    t = (TextView) findViewById(R.id.textView1);
    Typeface myCustomFont = Typeface.createFromAsset(getAssets(),
            "fonts/soopafre.ttf");
    t.setTypeface(myCustomFont);

    final TextView textOne = (TextView) findViewById(R.id.textView1);
    Button drunken = (Button) findViewById(R.id.button1);

    final String[] misPrendas = {

    };

    drunken.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            int rando = (int) (Math.random() * 64);// poner numero exacto de
                                                    // consignas
            textOne.setText(misPrendas[rando]);
        }
    });
}
}

这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="lanzarJugar"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/fondogota" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Arranken!"
    android:textSize="25sp" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="58dp"
    android:background="@drawable/botondrunken"
    android:onClick="drunken" />

</RelativeLayout>  

我的logcat:

04-07 15:11:45.311: E/AndroidRuntime(1602): FATAL EXCEPTION: main
04-07 15:11:45.311: E/AndroidRuntime(1602): Process: org.drunken, PID: 1602
04-07 15:11:45.311: E/AndroidRuntime(1602): java.lang.IllegalStateException: Could not find a method lanzarJugar(View) in the activity class org.drunken.Jugar for onClick handler on view class android.widget.RelativeLayout
04-07 15:11:45.311: E/AndroidRuntime(1602): at android.view.View$1.onClick(View.java:3810)
04-07 15:11:45.311: E/AndroidRuntime(1602): at android.view.View.performClick(View.java:4438)
04-07 15:11:45.311: E/AndroidRuntime(1602): at android.view.View$PerformClick.run(View.java:18422)
04-07 15:11:45.311: E/AndroidRuntime(1602): at android.os.Handler.handleCallback(Handler.java:733)
04-07 15:11:45.311: E/AndroidRuntime(1602): at android.os.Handler.dispatchMessage(Handler.java:95)
04-07 15:11:45.311: E/AndroidRuntime(1602): at android.os.Looper.loop(Looper.java:136)
04-07 15:11:45.311: E/AndroidRuntime(1602): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 15:11:45.311: E/AndroidRuntime(1602): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 15:11:45.311: E/AndroidRuntime(1602): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 15:11:45.311: E/AndroidRuntime(1602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 15:11:45.311: E/AndroidRuntime(1602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 15:11:45.311: E/AndroidRuntime(1602): at dalvik.system.NativeStart.main(Native Method)
04-07 15:11:45.311: E/AndroidRuntime(1602): Caused by: java.lang.NoSuchMethodException: lanzarJugar [class android.view.View]
04-07 15:11:45.311: E/AndroidRuntime(1602): at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-07 15:11:45.311: E/AndroidRuntime(1602): at java.lang.Class.getMethod(Class.java:857)
04-07 15:11:45.311: E/AndroidRuntime(1602): at android.view.View$1.onClick(View.java:3803)
04-07 15:11:45.311: E/AndroidRuntime(1602): ... 11 more

1 个答案:

答案 0 :(得分:2)

更改此

 setContentView(R.layout.jugar);}

setContentView(R.layout.jugar);

}放错地方了。

您还要初始化TextView两次

t=(TextView) findViewById(R.id.textView1);
final TextView textOne = (TextView) findViewById(R.id.textView1); // no need for this

只需使用

t.setText(misPrendas[rando]);

你的字符串数组是空的

final String[] misPrendas = {

};