0

I'm trying to get an ImageButton to create another ImageButton in a different layout from an OnClick. My logcat takes me to an error on this line, but provides no further feedback:

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

My code:

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);
        }
    }
}

Thanks for you patience.

Edit: the .xml the button is to create on

<?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>

the logcat:

01-06 23:35:23.725: E/AndroidRuntime(29654):    at `org.iimed.www.Penicillins.onClick(Penicillins.java:50)`
ToeKnee
  • 47
  • 1
  • 10

2 Answers2

0

You should create a new ImageButton in code, and not try to add the same ImageButton reference (R.id.abbutton). You can't add the same View (you get a reference to same inflated object using findViewbyId). Try something like:

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);

Also, as a good practice you should keep the reference to your RelativeLayout ll in a member variable, assign in it once (look it up using findViewbyId) in your onCreate and just use it wherever you need it, that way you save the unnecessary lookups every time you click the R.id.addmed ImageButton.

linakis
  • 1,203
  • 10
  • 19
  • I see, but where does it get the new Image for the new button? – ToeKnee Oct 26 '13 at 05:24
  • I've added this Context abbuttonplain; should the (getApplicationContext()); work now? – ToeKnee Oct 26 '13 at 05:26
  • Try one of: `adab.setImageBitmap(bitmap);` `adab.setImageDrawable(drawable);` – linakis Oct 26 '13 at 05:27
  • What's the Exception? Be a little more descriptive. – linakis Oct 26 '13 at 05:34
  • ll.addView(adab,param); On this line 01-07 00:01:06.590: E/AndroidRuntime(1187): at org.iimed.www.Penicillins.onClick(Penicillins.java:52) – ToeKnee Oct 26 '13 at 05:41
  • Add the whole relative stack trace, the addView failed for a reason, that is mentioned somewhere in there. – linakis Oct 26 '13 at 05:43
  • 01-07 00:01:05.439: D/ActivityManager(778): Trying to launch org.iimed.www/.Penicillins 01-07 00:01:05.449: V/SmartFaceService - 3rd party pause(778): onReceive [android.intent.action.ACTIVITY_STATE/org.iimed.www/create] 01-07 00:01:06.590: E/AndroidRuntime(1187): at org.iimed.www.Penicillins.onClick(Penicillins.java:52) – ToeKnee Oct 26 '13 at 05:57
  • Is that relevant? that's all that is showing and only the one line is a red error. – ToeKnee Oct 26 '13 at 06:04
0
back = (ImageButton) findViewById(R.id.back);
addmed = (ImageButton) findViewById(R.id.addmed);

you are using this id's to click and getting error for this is

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

check id and layout proper as you have no id of abbuttonin your layout

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
Vivek Shah
  • 380
  • 2
  • 8