启动一个新的Activity-Application停止工作

时间:2013-10-14 16:42:32

标签: android android-intent android-activity

我正在尝试为学校作业构建一个简单的应用程序。当用户点击图像按钮时,会打开一个新视图,其中包含图像的图片。但是,只要用户点击我的应用程序中的图像,它就会停止。 logcat错误说在android view.view.onclick上说,但我看不出错误在哪里。提前谢谢。

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>

1 个答案:

答案 0 :(得分:2)

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

public void apple(View buttona){}

public void banana(View buttonb){}

public void strawberry(View buttons){}

你完成了。