setText导致“Resources $ NotFoundException”

时间:2015-04-27 16:29:03

标签: java android android-edittext settext

我不知道为什么“tex.SetText(score1);”抛出Resources $ NotFoundException异常我的代码java如下:

package com.example.mehran.golyapooch;  

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu; 
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;


public class startwith2player extends ActionBarActivity {




private boolean playerone = true , playertwo = false ;
private Integer score1 = 0 , score2 = 0;
private Integer flag4 = 0 , flag5 = 0 , flag6 = 0 , flag7 = 0, flag8 = 0, flag9 = 0 , flag10 = 0, flag11 = 0, flag12 = 0;
private Integer counter1 = 0 , counter2 = 0;
  EditText tex ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_startwith2player);
     tex = (EditText) findViewById(R.id.editText3);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_startwith2player, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
public void refresh(){
    BlueAllBoxes();
    if(playerone){
        playerone = false;
        playertwo = true;
    }
    else{
    playerone = true;
    playertwo = false;
    }
    flag4 = 0 ; flag5 = 0 ; flag6 = 0 ; flag7 = 0; flag8 = 0; flag9 = 0 ; flag10 = 0; flag11 = 0; flag12 = 0;
    counter1 = 0 ; counter2 = 0;
}

public void BlueAllBoxes(){
    Button button4 = (Button) findViewById(R.id.button4);
    button4.setBackgroundColor(0xFF4B4BFF);
    Button button5 = (Button) findViewById(R.id.button5);
    button5.setBackgroundColor(0xFF4B4BFF);
    Button button6 = (Button) findViewById(R.id.button6);
    button6.setBackgroundColor(0xFF4B4BFF);
    Button button7 = (Button) findViewById(R.id.button7);
    button7.setBackgroundColor(0xFF4B4BFF);
    Button button8 = (Button) findViewById(R.id.button8);
    button8.setBackgroundColor(0xFF4B4BFF);
    Button button9 = (Button) findViewById(R.id.button9);
    button9.setBackgroundColor(0xFF4B4BFF);
    Button button10 = (Button) findViewById(R.id.button10);
    button10.setBackgroundColor(0xFF4B4BFF);
    Button button11 = (Button) findViewById(R.id.button11);
    button11.setBackgroundColor(0xFF4B4BFF);
    Button button12 = (Button) findViewById(R.id.button12);
    button12.setBackgroundColor(0xFF4B4BFF);
}
public void button4(View view) {
    if(counter1 == 3){
        if(flag4 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                    tex.setText(score1);
            }
            else {
                score2 = score2 + 30;
              //  tex.setText(score2);
            }
            if(counter2 == 3)
            refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            //decrease score
        }
    }
 else {
        if (flag4 == 0) {
            flag4 = 1;
            view.setBackgroundColor(0xFF67FF5A);
            counter1++;
            if(counter1 == 3)
              BlueAllBoxes();
        }
    }
}
public void button5(View view) {
    if(counter1 == 3){
        if(flag5 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

               if(playerone) {
                    score1 = score1 + 30;
                    tex.setText(score1);
                }
               else {
                   score2 = score2 + 30;
                //    tex.setText(score2);
                }
                if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            //decrease score
        }
    }
    else {
        if (flag5 == 0) {
            flag5 = 1;
            view.setBackgroundColor(0xFF67FF5A);
            counter1++;
            if(counter1 == 3)
                BlueAllBoxes();
        }
    }
}
public void button6(View view) {
    if(counter1 == 3){
        if(flag6 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
             //   tex.setText(score1);
            }
            else {
                score2 = score2 + 30;
             //   tex.setText(score2);
            }
                if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            //decrease score
        }
    }
    else {
        if (flag6 == 0) {
            flag6 = 1;
            view.setBackgroundColor(0xFF67FF5A);
            counter1++;
            if(counter1 == 3)
                BlueAllBoxes();
        }
    }
}
public void button7(View view) {
    if(counter1 == 3){
        if(flag7 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
             //   tex.setText(score1);
            }
            else {
                score2 = score2 + 30;
             //   tex.setText(score2);
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            //decrease score
        }
    }
    else {
        flag7 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button8(View view) {
    if(counter1 == 3){
        if(flag8 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex.setText(score1);
            }
            else {
                score2 = score2 + 30;
                tex.setText(score2);
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            //decrease score
        }
    }
    else {
        flag8 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button9(View view) {
    if(counter1 == 3){
        if(flag9 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex.setText(score1);
            }
            else {
                score2 = score2 + 30;
                tex.setText(score2);
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            //decrease score
        }
    }
    else {
        flag9 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button10(View view) {
    if(counter1 == 3){
        if(flag10 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
               tex.setText(score1);
            }
            else {
                score2 = score2 + 30;
                tex.setText(score2);
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            //decrease score
        }
    }
    else {
        flag10 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button11(View view) {
    if(counter1 == 3){
        if(flag11 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex.setText(score1);
            }
            else {
                score2 = score2 + 30;
               tex.setText(score2);
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            //decrease score
        }
    }
    else {
        flag11 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button12(View view) {
    if(counter1 == 3){
        if(flag12 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex.setText(score1);
            }
            else {
                score2 = score2 + 30;
                tex.setText(score2);
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            //decrease score
        }
    }
    else {
        flag12 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}

}

在函数“button4”tex.SetText(score1);我得到了“资源$ NotFoundException”,这个问题是在button5,button6,button7,button8,button9,button10,button11,按钮12中,我也无法理解为什么这个异常抛出,可以每一个修复我的代码吗? 下面是我的XML代码::

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/button4"
    android:layout_alignParentTop="true"
    android:layout_marginTop="55dp"
    android:background="#ff4b4bff"
    android:onClick="button4"/>

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/button5"
    android:layout_alignTop="@+id/button4"
    android:layout_centerHorizontal="true"
    android:background="#ff4b4bff"
    android:onClick="button5"/>

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/button6"
    android:layout_alignTop="@+id/button5"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="#ff4b4bff"
    android:onClick="button6"/>

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/button7"
    android:layout_marginTop="67dp"
    android:layout_below="@+id/button4"
    android:layout_toStartOf="@+id/button5"
    android:layout_alignRight="@+id/button4"
    android:layout_alignEnd="@+id/button4"
    android:background="#ff4b4bff"
    android:onClick="button7"/>

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/button8"
    android:layout_alignTop="@+id/button7"
    android:layout_centerHorizontal="true"
    android:background="#ff4b4bff"
    android:onClick="button8"/>

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/button9"
    android:layout_alignTop="@+id/button8"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="#ff4b4bff"
    android:onClick="button9"/>

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/button10"
    android:layout_marginTop="63dp"
    android:layout_below="@+id/button7"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="#ff4b4bff"
    android:onClick="button10"/>

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/button11"
    android:layout_alignTop="@+id/button10"
    android:layout_centerHorizontal="true"
    android:background="#ff4b4bff"
    android:onClick="button11"/>

<Button
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/button12"
    android:layout_alignTop="@+id/button11"
    android:layout_alignLeft="@+id/button9"
    android:layout_alignStart="@+id/button9"
    android:background="#ff4b4bff"
    android:onClick="button12"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="score1"
    android:id="@+id/textView3"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText3"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/button4"
    android:layout_toEndOf="@+id/button4"
    android:text="AA" />

请帮帮我:)

2 个答案:

答案 0 :(得分:5)

您需要将得分转换为字符串并设置它,否则它将尝试从 R 类设置资源,但由于它不存在,它会抛出该异常。例如

tex.setText(Integer.toString(score1));

tex.setText(String.valueOf(score1));

tex.setText(String.format("%d", score1));

 tex.setText("" + score1);

还有一些,但更喜欢第一个,因为它似乎是最快的

答案 1 :(得分:1)

setText中的方法TextView尝试获取资源字符串,如果您向其传递一个整数。

抛出异常是因为它找不到任何带有你传递的id的资源。

要将score1设置为TextView中的sting,您必须将其转换为字符串。

选择是使用String.valueOf(score1),或将其与空字符串连接,这将隐式地"" + score1投射它。