单击任何按钮时,应用程序崩溃

时间:2016-02-21 19:12:50

标签: android android-button

每当我点击任何按钮时,我的应用都会崩溃。什么想法可能会出错?

我得到的错误是

  

错误 - 无法在父级或中找到方法openMastermind(View)   android的ancestor Context:在视图类上定义的onClick属性   android.support.v7.widget.AppCompatButton,id为' button1'

public class MainActivity extends ListActivity {


ArrayList<Guess> guesses = new ArrayList<>();
ListView listview;
//ImageButton checkButton = (ImageButton) findViewById(R.id.checkButton);
private ImageView[] guessViews;
private Colour[] currentGuess = new Colour[4];
private int guessPosition = 0;
private Colour[] solution = new Colour[4];

public void openInstructions(View view) {
    Intent startInstructionsActivity = new Intent(this, Instructions.class);
    startActivity(startInstructionsActivity);
}

public void openMastermind(View view) {
    Intent startMastermindActivity = new Intent(this, mastermind1.class);
    startActivity(startMastermindActivity);
}

public void openHighscores(View view) {
    Intent startHighscoresActivity = new Intent(this, Highscores.class);
    startActivity(startHighscoresActivity);
}

public MainActivity() {

    Random r = new Random();
    for (int i = 0; i < solution.length; i++) {
        solution[i] = Colour.values()[r.nextInt(Colour.values().length)];
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mastermind1);


    final ListAdapter myAdapter = new CustomAdapter(this, guesses);
    listview = getListView();

    listview.setAdapter(myAdapter);

    guessViews = new ImageView[4];
    guessViews[0] = (ImageView) findViewById(R.id.guess1);
    guessViews[1] = (ImageView) findViewById(R.id.guess2);
    guessViews[2] = (ImageView) findViewById(R.id.guess3);
    guessViews[3] = (ImageView) findViewById(R.id.guess4);

这是包含按钮的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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#006699">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Mastermind"
    android:id="@+id/textView"
    android:textColor="#000000"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/play_button"
    android:id="@+id/button1"
    android:layout_marginTop="70dp"
    android:layout_below="@+id/textView"
    android:layout_alignLeft="@+id/textView"
    android:onClick="openMastermind"
    android:background="#ffffff"
    android:layout_alignRight="@+id/textView" />

<Button

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/instructions_button"
    android:id="@+id/button2"
    android:onClick="openInstructions"
    android:background="#ffffff"
    android:layout_centerVertical="true"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignRight="@+id/button1" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/highscores_button"
    android:id="@+id/button3"
    android:layout_marginTop="45dp"
    android:onClick="openHighscores"
    android:layout_below="@+id/button2"
    android:layout_alignLeft="@+id/button2"
    android:textColor="#000000"
    android:background="#ffffff"
    android:layout_alignRight="@+id/button1" />

</RelativeLayout>

这是崩溃的logcat

02-21 19:18:30.788 2506-2506/? E/AndroidRuntime: FATAL EXCEPTION: main
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime: Process: com.example.i7678939.mastermind4, PID: 2506
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime: java.lang.IllegalStateException: Could not find method openMastermind(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button1'
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime:     at android.view.View.performClick(View.java:5198)
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21147)
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
02-21 19:18:30.788 2506-2506/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-21 19:18:30.799 262-475/? W/ActivityManager:   Force finishing activity com.example.i7678939.mastermind4/.mastermind1
02-21 19:18:30.857 62-62/? E/EGL_emulation: tid 62: eglCreateSyncKHR(1243): error 0x3004 (EGL_BAD_ATTRIBUTE)
02-21 19:18:31.302 262-764/? I/OpenGLRenderer: Initialized EGL, version 1.4
02-21 19:18:31.390 262-764/? W/EGL_emulation: eglSurfaceAttrib not implemented
02-21 19:18:31.391 262-764/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa3bd6b20, error=EGL_SUCCESS
02-21 19:18:31.498 262-280/? W/ActivityManager: Activity pause timeout for ActivityRecord{eb0a95a u0 com.example.i7678939.mastermind4/.mastermind1 t22 f}
02-21 19:18:31.857 262-272/? I/art: Background partial concurrent mark sweep GC freed 18556(1531KB) AllocSpace objects, 11(220KB) LOS objects, 33% free, 7MB/10MB, paused 6.822ms total 943.879ms
02-21 19:18:31.987 262-715/? W/art: Long monitor contention event with owner method=void com.android.server.am.ActivityStack$ActivityStackHandler.handleMessage(android.os.Message) from ActivityStack.java:283 waiters=0 for 110ms
02-21 19:18:32.317 690-770/? W/EGL_emulation: eglSurfaceAttrib not implemented
02-21 19:18:32.317 690-770/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa3cddec0, error=EGL_SUCCESS
02-21 19:18:33.593 1002-1013/? I/art: Background partial concurrent mark sweep GC freed 28190(1126KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 4MB/8MB, paused 2.418ms total 389.758ms
02-21 19:18:42.119 262-280/? W/ActivityManager: Activity destroy timeout for ActivityRecord{eb0a95a u0 com.example.i7678939.mastermind4/.mastermind1 t22 f}

1 个答案:

答案 0 :(得分:-1)

尝试在Activity类代码中设置Button的onClickListener,而不是在XML布局中。这种做法使您的代码更好,更可靠。最好保持UI xml和应用程序逻辑的分离。

Button mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             openInstructions(v);
         }
     });