我试图让一个以不同的布局ImageButton
创建另一个。我的 logcat 将我带到这一行的错误,但没有提供进一步的反馈:ImageButton
OnClick
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>
日志猫:
01-06 23:35:23.725: E/AndroidRuntime(29654): at `org.iimed.www.Penicillins.onClick(Penicillins.java:50)`