0

I am trying to build a simple app for a school assignment. When the user clicks on the image button a new view opens up with a picture of the image.However whenever a user clicks on the image in my application the it stops. The logcat error says says at android view.view.onclick, but I can't see where the mistake is. Thanks in advance.

main_activity.java

    package com.example.kids;  

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageButton;

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void apple(ImageButton buttona){

    Intent intent = new Intent(this, Apple.class);
    startActivity(intent);

}

public void banana(ImageButton buttonb){

Intent intent = new Intent(this, Banana.class);
startActivity(intent);}

public void strawberry(ImageButton buttons){

    Intent intent = new Intent(this, Strawberry.class);
    startActivity(intent);

}}

Apple.java

package com.example.kids;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class Apple extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.apple, menu);
    return true;
}

}

Activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="100"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_weight="9.93"
    android:text="Pick a fruit"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="30"
    android:src="@drawable/applebudget"
    android:onClick="apple"
    />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="30"
    android:src="@drawable/bananabudget"
    android:onClick="banana"
     />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="30"
    android:src="@drawable/strawberrybudget"
    android:onClick="strawberry"
     />

</LinearLayout>
4

1 回答 1

2

更改 onClick() 方法的方法签名:

public void apple(View buttona){}

public void banana(View buttonb){}

public void strawberry(View buttons){}

你完成了。

于 2013-10-14T16:52:54.843 回答