Programming an Android Button to update EditText views
Posted
by
bergler77
on Stack Overflow
See other posts from Stack Overflow
or by bergler77
Published on 2012-07-09T03:11:20Z
Indexed on
2012/07/09
3:15 UTC
Read the original article
Hit count: 577
Ok guys, I have a button in android that i'm trying to use to update 8 EditText Views with different random numbers. Everything works up until I click the button. It appears I am missing a resource according to the debugger, but I'm not sure what. I've tried several different ways of implementing the button. Here is what I have after looking at several posts.
import java.util.Random;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MyCharNewChar extends MyCharActivity {
private OnClickListener randomButtonListener = new OnClickListener(){
public void onClick(View v) {
//Button creates a set of random numbers and updates the values
//of the EditText views.
Random rand = new Random();
int STR = 1 + rand.nextInt(12);
int AGI = 1 + rand.nextInt(12);
int DEX = 1 + rand.nextInt(12);
int WIS = 1 + rand.nextInt(12);
int INT = 1 + rand.nextInt(12);
int CON = 1 + rand.nextInt(12);
int HP = 1 + rand.nextInt(20);
int AC = 1 + rand.nextInt(6);
EditText str = (EditText) findViewById(R.id.str);
str.setText(STR);
EditText agi = (EditText) findViewById(R.id.agi);
agi.setText(AGI);
EditText dex = (EditText) findViewById(R.id.dex);
dex.setText(DEX);
EditText wis = (EditText) findViewById(R.id.wis);
wis.setText(WIS);
EditText intel = (EditText) findViewById(R.id.intel);
intel.setText(INT);
EditText con = (EditText) findViewById(R.id.con);
con.setText(CON);
EditText hp = (EditText) findViewById(R.id.baseHP);
hp.setText(HP);
EditText ac = (EditText) findViewById(R.id.baseAC);
ac.setText(AC);
}
};
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.newchar);
Button randomButton = (Button) findViewById(R.id.randomButton);
randomButton.setOnClickListener(randomButtonListener);
}
}
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayoutNew1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background" >
<TextView
android:id="@+id/newCharLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/new_character_screen"
android:textSize="24dp"
android:textColor="@color/splash"
android:textStyle="bold"
android:gravity="center"/>
<TextView
android:id="@+id/nameLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/nameLabel"
android:textSize="18dp"
android:textColor="@color/splash"/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TableLayout
android:id="@+id/statsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TableRow
android:id="@+id/tableRow01"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/strLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/strLabel"
android:textSize="18dp"
android:textColor="@color/splash"/>
<EditText
android:id="@+id/str"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:inputType="number" />
<TextView
android:id="@+id/agiLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/agiLabel"
android:textSize="18dp"
android:textColor="@color/splash"/>
<EditText
android:id="@+id/agi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:inputType="number"/>
<TextView
android:id="@+id/dexLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dexLabel"
android:textSize="18dp"
android:textColor="@color/splash"/>
<EditText
android:id="@+id/dex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:inputType="number"/>
</TableRow>
<TableRow
android:id="@+id/tableRow02"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="@+id/intLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/intLabel"
android:textSize="18dp"
android:textColor="@color/splash"/>
<EditText
android:id="@+id/intel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:inputType="number"/>
<TextView
android:id="@+id/wisLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wisLabel"
android:textSize="18dp"
android:textColor="@color/splash"/>
<EditText
android:id="@+id/wis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:inputType="number"/>
<TextView
android:id="@+id/conLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/conLabel"
android:textSize="18dp"
android:textColor="@color/splash"/>
<EditText
android:id="@+id/con"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:inputType="number"/>
</TableRow>
</TableLayout>
<LinearLayout
android:id="@+id/linearlayoutNew02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center">
<TextView
android:id="@+id/baseHPLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hpLabel"
android:textSize="18dp"
android:textColor="@color/splash"/>
<EditText
android:id="@+id/baseHP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:inputType="number"/>
<TextView
android:id="@+id/baseACLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/acLabel"
android:textSize="18dp"
android:textColor="@color/splash"/>
<EditText
android:id="@+id/baseAC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:id="@+id/linearlayoutNew03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/randomButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/randomButton"
android:textSize="16dp"
android:clickable="true"/>
</LinearLayout>
</LinearLayout>
I have also tried setting the onClick in xml to setup a specific onClick method. Still the same error so I must have a problem elsewhere. Any suggestions would be great!
© Stack Overflow or respective owner