2012-07-09 24 views
0

好吧,我有一個android按鈕,我試圖用來更新8 EditText視圖與不同的隨機數。一切工作,直到我點擊按鈕。它似乎根據調試器錯過了一個資源,但我不確定是什麼。我嘗試了幾種不同的方式來實現按鈕。這是我看過幾篇文章後的結果。編程Android按鈕來更新EditText視圖

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


} 

這裏是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> 

我自己也嘗試在XML中設置的onClick設置一個特定的onClick方法。還是一樣的錯誤,所以我必須在其他地方遇到問題。任何建議都會很棒!

+1

發佈錯誤以及未找到 – Tim 2012-07-09 03:16:40

+0

類文件編輯器 來源 /從資源編譯.java(版本1.5:49.0,超級位) public class android.content.res.Resources { //方法描述符#13(Landroid/content/res/AssetManager; Landroid/util/DisplayMetrics; Landroid/content/res/Configuration;)V // Stack:3 ,當地人:4 public Resources(android.content.res.AssetManager assets,android.util.DisplayMetrics metrics,android.content.res.Configuration config); 0 aload_0 [此] 1 invokespecial java.lang.Object中()[1] 4新了java.lang.RuntimeException [2] 8 LDC <字符串 「存根!」> 它不斷去 – bergler77 2012-07-09 03:34:31

回答

1

你想的setText爲int,並it must be String.

str.setText(STR+" "); 

str.setText(String.valueof(STR)); 
+0

這解決了問題, 謝啦! – bergler77 2012-07-09 03:21:53