2013-10-26 6 views
0

Я пытаюсь получить ImageButton, чтобы создать еще один ImageButton в другом макете от OnClick. Мой LogCat берет меня за ошибки на этой линии, но не дает никакой дополнительной обратной связи:OnCreate 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> 

в LogCat:

01-06 23:35:23.725: E/AndroidRuntime(29654): at `org.iimed.www.Penicillins.onClick(Penicillins.java:50)` 
+0

Вы можете добавить ошибку LogCat сообщение и этот конкретный формат XML вместе с вопросом. –

+0

R.id.abbutton находится в R.layout.penicillin справа? –

+0

@antimo, если это так, тогда 'RelativeLayout ll = (RelativeLayout) findViewById (R.layout.sundayopen);' должен был сбой первым. Пожалуйста, добавьте целую (относительную) stacktrace из logcat, а не только одну строку из нее. – linakis

ответ

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 и просто использовать его везде, где вы нужно так, чтобы вы сохраняли ненужные запросы каждый раз, когда вы нажимаете кнопку ImageButton R.id.addmed.

+0

Я вижу, но где он получает новое изображение для новой кнопки? – ToeKnee

+0

Я добавил этот контекст abbuttonplain; должен (getApplicationContext()); работать сейчас? – ToeKnee

+0

Попробуйте один из: 'adab.setImageBitmap (bitmap);' 'adab.setImageDrawable (drawable);' – linakis

0
back = (ImageButton) findViewById(R.id.back); 
addmed = (ImageButton) findViewById(R.id.addmed); 

вы используете этот идентификатор, чтобы нажать и получить сообщение об ошибке для этого

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

проверки идентификатора и расположение собственно, как у вас нет идентификатора abbuttonin макета

Смежные вопросы