我正在尝试让ImageButton
以与ImageButton
不同的布局创建另一个OnClick
。我的logcat让我在这一行上出错,但没有提供进一步的反馈:
ImageButton adab = (ImageButton)findViewById(R.id.abbutton);
我的代码:
package org.iimed.www;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class Penicillins extends Activity implements OnClickListener{
ImageButton back,addmed;
Context abbuttonplain;
public void onCreate(Bundle SavedInstanceState){
super.onCreate(SavedInstanceState);
setContentView(R.layout.penicillin);
back = (ImageButton) findViewById(R.id.back);
addmed = (ImageButton) findViewById(R.id.addmed);
back.setOnClickListener(this);
addmed.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.back:
startActivity(new Intent(
Penicillins.this, ImageTextListViewActivity.class));
break;
case R.id.addmed:
RelativeLayout ll=(RelativeLayout)findViewById(R.layout.sundayopen);
LayoutParams param = new
RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageButton adab = (ImageButton)findViewById(R.id.abbutton);
ll.addView(adab,param);
}
}
}
感谢您的耐心等待。
编辑:按钮将在
上创建.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:background="@drawable/miimedback" >
<ImageView
android:id="@+id/pillboxm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="63dp"
android:contentDescription="@string/pillbox"
android:src="@drawable/pillbox" />
<ImageButton
android:id="@+id/satlid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pillboxm"
android:layout_alignRight="@+id/pillboxm"
android:background="@android:color/transparent"
android:contentDescription="@string/satlid"
android:src="@drawable/slid" />
<ImageButton
android:id="@+id/frilid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thulid"
android:layout_toLeftOf="@+id/satlid"
android:background="@android:color/transparent"
android:contentDescription="@string/frilid"
android:src="@drawable/flid" />
<ImageButton
android:id="@+id/thulid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pillboxm"
android:layout_toLeftOf="@+id/frilid"
android:background="@android:color/transparent"
android:contentDescription="@string/thulid"
android:src="@drawable/tlid" />
<ImageButton
android:id="@+id/homebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="11dp"
android:background="@android:color/transparent"
android:contentDescription="@string/homebutton"
android:src="@drawable/homebut1" />
<ImageButton
android:id="@+id/wedlid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thulid"
android:layout_toLeftOf="@+id/thulid"
android:background="@android:color/transparent"
android:contentDescription="@string/wedlid"
android:src="@drawable/wlid" />
<ImageButton
android:id="@+id/tuelid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pillboxm"
android:layout_toLeftOf="@+id/wedlid"
android:background="@android:color/transparent"
android:contentDescription="@string/tuelid"
android:src="@drawable/tlid" />
<ImageButton
android:id="@+id/monlid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pillboxm"
android:layout_toLeftOf="@+id/tuelid"
android:background="@android:color/transparent"
android:contentDescription="@string/monlid"
android:src="@drawable/mlid" />
<ImageButton
android:id="@+id/adbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/abbutton"
android:background="@android:color/transparent"
android:contentDescription="@string/adbutton"
android:src="@drawable/adbutton" />
<ImageButton
android:id="@+id/abbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/cobutton"
android:layout_alignParentRight="true"
android:background="@android:color/transparent"
android:contentDescription="@string/abbutton"
android:src="@drawable/antibiotic_buton" />
<ImageButton
android:id="@+id/bppressure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/monlid"
android:layout_alignTop="@+id/adbutton"
android:background="@android:color/transparent"
android:contentDescription="@string/bpbutton"
android:src="@drawable/bpbutton" />
<ImageButton
android:id="@+id/satlidopen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/monlid"
android:layout_toLeftOf="@+id/monlid"
android:background="@android:color/transparent"
android:contentDescription="@string/satlidopen"
android:src="@drawable/satlidopen" />
<ImageButton
android:id="@+id/cobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/satlidopen"
android:layout_marginBottom="133dp"
android:layout_toRightOf="@+id/wedlid"
android:background="@android:color/transparent"
android:contentDescription="@string/cobutton"
android:src="@drawable/cobutton" />
</RelativeLayout>
logcat:
01-06 23:35:23.725: E/AndroidRuntime(29654): at `org.iimed.www.Penicillins.onClick(Penicillins.java:50)`
答案 0 :(得分:0)
您应该在代码中创建新的ImageButton
,而不是尝试添加相同的ImageButton
引用(R.id.abbutton
)。您无法添加相同的View(使用findViewbyId可以获得对相同膨胀对象的引用)。尝试类似:
RelativeLayout ll=(RelativeLayout)findViewById(R.layout.sundayopen);
LayoutParams param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageButton adab = new ImageButton(getApplicationContext());
// Initialise ImageButton properties like the Image here...
ll.addView(adab,param);
此外,作为一种良好做法,您应该在成员变量中保留对RelativeLayout ll
的引用,在findViewbyId
中分配一次(使用onCreate
查找)并且只是在任何需要的地方使用它,这样每次单击R.id.addmed
ImageButton时都会保存不必要的查找。
答案 1 :(得分:0)
back = (ImageButton) findViewById(R.id.back);
addmed = (ImageButton) findViewById(R.id.addmed);
您正在使用此ID来点击并获取此错误
adab = (ImageButton)findViewById(R.id.abbutton);
检查ID和布局是否正确,因为您的布局中没有abbut都是id