我使用单选按钮On和Off创建了一个活动。当用户单击“开”时,图像出现,并且“关闭”图像消失。一切正常,但当用户不小心点击图像时,应用程序将停止工作。图像没有点击功能。请参阅下面的代码。 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" 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="com.example.android.Recipe3"
android:onClick="Home"
android:clickable="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Beth's Spicy Oatmeal"
android:id="@+id/textView6"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="1/2 cup butter, softened"
android:id="@+id/textView22"
android:layout_below="@+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="1 cup packed light brown sugar"
android:id="@+id/textView23"
android:layout_below="@+id/textView22"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="1 1/2 cups all-purpose flour"
android:id="@+id/textView24"
android:layout_below="@+id/textView23"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="3 cups rolled oats"
android:id="@+id/textView25"
android:layout_below="@+id/textView24"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="1 teaspoon ground cinnamon"
android:id="@+id/textView26"
android:layout_below="@+id/textView25"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:id="@+id/button9"
android:layout_below="@+id/textView26"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="true"
android:enabled="true"
android:onClick="HomePage" />
<RadioGroup
android:layout_width="120dp"
android:layout_height="49dp"
android:orientation="horizontal"
android:id="@+id/radioGroup2"
android:layout_alignTop="@+id/button9"
android:layout_toRightOf="@+id/textView23"
android:layout_toEndOf="@+id/textView23">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On"
android:id="@+id/radioButton8"
android:checked="true"
android:button="@android:drawable/btn_radio"
android:enabled="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Off"
android:id="@+id/radioButton9"
android:layout_gravity="center_horizontal"
android:checked="false"
android:button="@android:drawable/btn_radio"
android:enabled="true"
android:clickable="true" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Image"
android:id="@+id/textView27"
android:layout_alignBaseline="@+id/button9"
android:layout_alignBottom="@+id/button9"
android:layout_toLeftOf="@+id/radioGroup2"
android:layout_toStartOf="@+id/radioGroup2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView3"
android:src="@drawable/beth"
android:layout_alignParentRight="false"
android:layout_alignParentEnd="false"
android:layout_below="@+id/radioGroup2"
android:layout_marginBottom="-30dp" />
</RelativeLayout>
Java代码:
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
public class Recipe3 extends ActionBarActivity {
ImageView imageView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe3);
imageView3 = (ImageView) findViewById(R.id.imageView3);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup2);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radioButton8) {
imageView3.setVisibility(View.VISIBLE);
} else if (checkedId == R.id.radioButton9) {
imageView3.setVisibility(View.GONE);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_recipe3, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void HomePage(View v){
Intent intent=new Intent(v.getContext(),Login.class);
startActivity(intent);
}}
答案 0 :(得分:0)
错误是Android无法找到Home(View view)
方法实现。
当您单击ImageView时,没有任何onClick for ImageView,因此它将其click事件传递给其父视图,意味着RelativeLayout
并且为相对布局编写了onClick="Home"
但未找到实现在你的活动中。
因此,android无法找到public void Home(View view) method
。
从android:onClick="Home"
移除RelativeLaout
这一行,或在您的活动中实施public void Home(View view)
方法。