0

我试图让一个以不同的布局ImageButton创建另一个。我的 logcat 将我带到这一行的错误,但没有提供进一步的反馈:ImageButtonOnClick

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)`
4

2 回答 2

0

您应该在代码中创建一个新ImageButton的,而不是尝试添加相同的ImageButton引用 ( R.id.abbutton)。您不能添加相同的视图(您使用 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

于 2013-10-26T05:18:53.123 回答
0
back = (ImageButton) findViewById(R.id.back);
addmed = (ImageButton) findViewById(R.id.addmed);

你正在使用这个 id 来点击并得到错误是

  adab = (ImageButton)findViewById(R.id.abbutton);

检查 id 和布局是否正确,因为您的布局中没有 abbutton 的 id

于 2013-10-26T05:29:04.127 回答