2015-06-01 7 views
0

Я делаю приложение для Android, которое позволяет пользователям нажимать кнопку «плюс» или «минус», чтобы изменить номер, отображаемый в текстовом режиме.Android: настройка ID для массива кнопок изображения

Есть 18 текстовых просмотров и кнопка плюс и минус изображения для каждого текстового вида.

Активность падает, когда я добираюсь до этого действия, из-за этого раздела кода. Я не смог получить какую-либо полезную информацию при отладке кода, поэтому любая помощь будет оценена, спасибо!

В верхней части моей деятельности:

int score = 0; 
ImageButton addButtons[] = {(ImageButton)findViewById(R.id.hole1Up), (ImageButton)findViewById(R.id.hole2Up), (ImageButton)findViewById(R.id.hole3Up), (ImageButton)findViewById(R.id.hole4Up), (ImageButton)findViewById(R.id.hole5Up), (ImageButton)findViewById(R.id.hole6Up), (ImageButton)findViewById(R.id.hole7Up), (ImageButton)findViewById(R.id.hole8Up), (ImageButton)findViewById(R.id.hole9Up), (ImageButton)findViewById(R.id.hole10Up), (ImageButton)findViewById(R.id.hole11Up), (ImageButton)findViewById(R.id.hole12Up), (ImageButton)findViewById(R.id.hole13Up), (ImageButton)findViewById(R.id.hole14Up), (ImageButton)findViewById(R.id.hole15Up), (ImageButton)findViewById(R.id.hole16Up), (ImageButton)findViewById(R.id.hole17Up), (ImageButton)findViewById(R.id.hole18Up),}; 
ImageButton minusButtons[] = {(ImageButton)findViewById(R.id.hole1Down), (ImageButton)findViewById(R.id.hole2Down), (ImageButton)findViewById(R.id.hole3Down), (ImageButton)findViewById(R.id.hole4Down), (ImageButton)findViewById(R.id.hole5Down), (ImageButton)findViewById(R.id.hole6Down), (ImageButton)findViewById(R.id.hole7Down), (ImageButton)findViewById(R.id.hole8Down), (ImageButton)findViewById(R.id.hole9Down), (ImageButton)findViewById(R.id.hole10Down), (ImageButton)findViewById(R.id.hole11Down), (ImageButton)findViewById(R.id.hole12Down), (ImageButton)findViewById(R.id.hole13Down), (ImageButton)findViewById(R.id.hole14Down), (ImageButton)findViewById(R.id.hole15Down), (ImageButton)findViewById(R.id.hole16Down), (ImageButton)findViewById(R.id.hole17Down), (ImageButton)findViewById(R.id.hole18Down),}; 
int[] scoreIDs = new int[] {R.id.score1, R.id.score2, R.id.score3, R.id.score4, R.id.score5, R.id.score6, R.id.score7, R.id.score8, R.id.score9, R.id.score10, R.id.score11, R.id.score12, R.id.score13, R.id.score14, R.id.score15, R.id.score16, R.id.score17, R.id.score18,}; 

public void setPlusButtons() 
{ 
    for(int i = 0; i < addButtons.length; i++) 
    { 
     final int j = i; 
     ImageButton button = addButtons[i]; 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       TextView tv5 = (TextView) findViewById(scoreIDs[j]); 
       tv5.setText(score++); 
      } 
     }); 
    } 
} 

public void setMinusButtons() 
{ 
    for(int i = 0; i < minusButtons.length; i++) 
    { 
     final int j = i; 
     ImageButton button = minusButtons[i]; 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       TextView tv5 = (TextView) findViewById(scoreIDs[j]); 
       tv5.setText(score--); 
      } 
     }); 
    } 
} 

Мой OnCreate:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_course); 

    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     chosenCourseValue = extras.getString("passedChosenCourse"); 
    } 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

      AsyncAPI APIThread = new AsyncAPI(); 
      APIThread.execute(); 
    setPlusButtons(); 
    setMinusButtons(); 
} 

раздел моей деятельности XML

 <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Holes" 
      android:id="@+id/hole1" 
      android:layout_marginLeft="32dp" 
      android:textColor="#fff" 
      android:gravity="center" 
      android:background="@drawable/circle" 
      android:height="45dp" 
      android:width="45dp"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Pars" 
      android:id="@+id/hole1Par" 
      android:paddingLeft="22dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Score" 
      android:id="@+id/score1" 
      android:paddingLeft="42dp" /> 

     <ImageButton 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:scaleType="fitXY" 
      android:adjustViewBounds="true" 
      android:cropToPadding="false" 
      android:id="@+id/hole1Up" 
      android:gravity="center" 
      android:layout_marginLeft="32dp" 
      android:layout_marginTop="6dp" 
      android:src="@drawable/plus" 
      android:background="@null" /> 

     <ImageButton 
      android:layout_width="32dp" 
      android:layout_height="32dp" 
      android:scaleType="fitXY" 
      android:adjustViewBounds="true" 
      android:cropToPadding="false" 
      android:id="@+id/hole1Down" 
      android:gravity="center" 
      android:layout_marginLeft="26dp" 
      android:layout_marginTop="6dp" 
      android:src="@drawable/minus" 
      android:background="@null" /> 
    </LinearLayout> 
+0

Мне не удалось получить сообщение об ошибке путем отладки, так что надеялся, что кто-то сможет указать, что я делаю неправильно с моим кодом, см. Пункт 3. – OJJ

+1

Эй, OJJ, я думаю, вместо этого вы можете использовать «ListView или RecylerView» с компоновкой вырезов и адаптером. – theapache64

+0

Можете ли вы опубликовать файл макета 'activity_course' xml? У меня есть основная идея сократить код и сложность кода. – user370305

ответ

0

ОК .. Хорошо. Теперь просто пошаговый шаг за шагом.

Шаг 1: определяют взгляды как, мы включаем и android:tag свойства в представлениях.

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Score" 
     android:id="@+id/score1" 
     android:paddingLeft="42dp" /> 

    <ImageButton 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:scaleType="fitXY" 
     android:adjustViewBounds="true" 
     android:cropToPadding="false" 
     android:id="@+id/hole1Up" 
     android:gravity="center" 
     android:layout_marginLeft="32dp" 
     android:layout_marginTop="6dp" 
     android:src="@drawable/plus" 
     android:background="@null" 
     android:onClick="upButtonOnClick" // We defines up button's onclick in xml 
     android:tag="score1"/> // associated TextView id is in Tag of image button 

    <ImageButton 
     android:layout_width="32dp" 
     android:layout_height="32dp" 
     android:scaleType="fitXY" 
     android:adjustViewBounds="true" 
     android:cropToPadding="false" 
     android:id="@+id/hole1Down" 
     android:gravity="center" 
     android:layout_marginLeft="26dp" 
     android:layout_marginTop="6dp" 
     android:src="@drawable/minus" 
     android:background="@null" 
     android:onClick="downButtonOnClick" // We defines down button's onclick in xml 
     android:tag="score1"/> // associated TextView id is in Tag of image button 

Таким образом, все вверх кнопки изображения должны быть объявлены android:onClick="upButtonOnClick" и все кнопки изображения вниз должны быть объявлены android:onClick="downButtonOnClick" с ассоциированным TextView ID, как android:tag собственности.

Шаг 2:

Вместо setPlusButtons() и setMinusButtons() определяют upButtonOnClick() и downButtonOnClick() как,

public void upButtonOnClick(View imageButton) 
{ 
      int textViewId = getResources().getIdentifier(imageButton.getTag(), "id", getPackageName()); 
      TextView score = (TextView) findViewById(textViewId); 
      score.setText(score++); 
} 

И

public void downButtonOnClick(View imageButton) 
{ 
      int textViewId = getResources().getIdentifier(imageButton.getTag(), "id", getPackageName()); 
      TextView score = (TextView) findViewById(textViewId); 
      score.setText(score--); 
} 

Вот так. вам не нужно объявлять массив ImageButtons, Id TextView и снова не нужно устанавливать на клик-прослушиватель на все кнопки изображения.

Примечание: убедитесь, что тег ImageButton должен быть таким же, как соответствующий идентификатор TextView.

+1

Спасибо @ user370305, ваш ответ решил мою проблему – OJJ

0

findViewById() вернет представление только после того, как был установлен макет. Теперь ваш код присваивает значения null вашим ImageButton массивам, поэтому, когда вы их просматриваете и пытаетесь установить прослушиватели кликов, вы получаете крах NPE.

Это означает, что вам нужно будет установить для своего ImageButton[] объявление, а затем инициализировать его значения в onCreate() после того, как был вызван setContentView().

ImageButton addButtons[]; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_course); 

    addButtons = new ImageButton[]{(ImageButton)findViewById(R.id.hole1Up), (ImageButton)findViewById(R.id.hole2Up)}; 
    setPlusButtons(); 
} 
0

это всего лишь пример того, как можно установить идентификаторы группы кнопок:

Button [] buttens=new Button[5]; 
final int ID= R.id.bChoice1; 
for (int i = 0; i < 5; i++) { 
     button[i]=(Button)findViewById(ID+i); 
    } 

, но будьте осторожны, он работает до тех пор, как вы установили кнопку идентификаторов последовательно!

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