This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
                                
                                    (6个答案)
                                
                        
                                3年前关闭。
            
                    
我的应用程序因错误而崩溃:

android.content.res.Resources$NotFoundException: String resource ID #0x0


我只是不知道我做错了!

public class WithdrawPoints extends AppCompatActivity {

private Firebase myRef;
private SharedPreferences.Editor accountEditor;
private static final String Firebase_URL = "https://myfirstproject-545a7.firebaseio.com/";
private int withdrawPoints;
private int originalPoints;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gain_rewards);
    ImageView view = (ImageView) findViewById(R.id.imageView5);
    view.setAlpha(127);
    myRef = new Firebase(Firebase_URL);
    TextView points = (TextView) findViewById(R.id.textView9);
    originalPoints = User.getInstance().getPoints();
    points.setText(originalPoints);

    EditText editText = (EditText) findViewById(R.id.editText4);
    withdrawPoints = Integer.valueOf(editText.getText().toString());

    final Button withdrawButton = (Button) findViewById(R.id.button7);
    withdrawButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            User.getInstance().setPoints(originalPoints-withdrawPoints);
            myRef.child("users").child(User.getInstance().getPhone()).setValue(User.getInstance());
        }
    });
}


}

FATAL EXCEPTION: main
Process: com.jingweizhang.coffeeshop, PID: 10540
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jingweizhang.coffeeshop/com.jingweizhang.coffeeshop.WithdrawPoints}:
android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:312)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at com.jingweizhang.coffeeshop.WithdrawPoints.onCreate(WithdrawPoints.java:37)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

最佳答案

您的问题很可能是由于originalPoints是整数并且您在points.setText(originalPoints);中使用它而引起的。 setText(int)构造函数需要一个资源ID,因此要显示实际值,必须先将其转换为字符串:

points.setText(String.valueOf(originalPoints));

09-27 18:29