缺少字符串资源ID

时间:2018-12-19 08:31:52

标签: java android

大家好,我希望有人能指出为什么我有一个特定的错误。我的程序告诉我,我缺少一个字符串资源ID,但是,至少在我所知的范围内,我没有删除任何字符串,也不需要添加任何字符串。我仍然是一个初学者程序员,我还不太清楚哪里出了问题。在我添加了sharedpreferences类以保存和加载来自editTexts的数据之前,我所能说的一切都已适当放置并开始了活动。 (这还不是我们要做的事情,但我正在尝试将数据保存并加载到editText中。)

我需要一个用户能够减轻重量并在打开activity时重新加载该数据。谢谢高级的人!

我的错误:

12-19 03:18:33.746 32007-32007/hbratcher1992.buccaneer.atlantic.edu.fightingforfitness E/AndroidRuntime: FATAL EXCEPTION: main
Process: hbratcher1992.buccaneer.atlantic.edu.fightingforfitness, PID: 32007
java.lang.RuntimeException: Unable to start activity ComponentInfo{hbratcher1992.buccaneer.atlantic.edu.fightingforfitness/hbratcher1992.buccaneer.atlantic.edu.fightingforfitness.Main.UInformation.UserInformation}: android.content.res.Resources$NotFoundException: String resource ID #0x0
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
    at android.content.res.Resources.getText(Resources.java:299)
    at android.widget.TextView.setText(TextView.java:4132)
    at hbratcher1992.buccaneer.atlantic.edu.fightingforfitness.Main.UInformation.UserInformation.onCreate(UserInformation.java:50)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

我的字符串:

<resources>
<string name="app_name">FightingForFitness</string>
<string name="title">   Fighting For Fitness</string>

<string name="routine_title_hint"> Enter the title of your Exercise.</string>
<string name="routine_title_label"> Title</string>
<string name="routine_details_label"> Date routine was started </string>

<string name="workout_button"> Your Exercises</string>
<string name="create_workout_button"> Create a New Routine</string>
<string name="user_info_button"> User Information</string>
<string name="useful_links_button"> Useful Links</string>
<string name="quick_notes_button"> Quick Notes</string>

<string name="recipes_button"> Recipes</string>
<string name="r_stuff"> This is a short list of links with various recipes For you to look through and use:\n
    \n
                        -https://www.commonbytes.org/#/recipes\n
    \n
                        -https://www.fruitsandveggiesmorematters.org\n
    \n
                        -http://www.eatingwell.com/\n
    \n
                        -https://www.skinnytaste.com/\n
    \n
                        -http://www.eatyourselfskinny.com/\n
    \n
                        -https://thehealthyfoodie.com/\n
</string>


<string name="youtube_button"> Youtube Guides</string>
<string name="sub_joe">PowerfulJRE: https://www.youtube.com/user/PowerfulJRE</string>
<string name="explanation_1">Joe Rogan is an amazing artist with an extremely informative channel. He has content on every subject imaginable and his conversations can always teach people something; he does have interviews about different types of diets that can help you in your decision.</string>
<string name="sub_keto">Keto Connect: https://www.youtube.com/channel/UCzRYivTpUQ0r2qPPjfLoQiA</string>
<string name="explanation_2">This couple has various recipes and other useful information targeted towards the ketogenic diet.</string>
<string name="sub_berg">Dr. Eric Berg (DC): https://www.youtube.com/user/drericberg123</string>
<string name="explanation_3">This doctor posts informative videos about the ketogenic diet.</string>
<string name="sub_berry">Ken D Berry MD: https://www.youtube.com/user/KenDBerry </string>
<string name="explanation_4">This doctor also posts informative videos and explanations about the ketogenic diet.</string>
<string name="sub_pill">Improvement Pill: https://www.youtube.com/channel/UCBIt1VN5j37PVM8LLSuTTlw</string>
<string name="explanation_5">This channel is amazing in helping you improve your life in general, his videos are very informative.</string>
<string name="sub_tran">Gravity Transformation: https://www.youtube.com/user/GravityTrainingSol</string>
<string name="explanation_6">This channel is very useful in teaching me different diets, exercises, and proper posture in the gym.</string>
<string name="sub_prime">Primal Edge Health: https://www.youtube.com/channel/UC03j2jcKwm0wTJpDsu-9u4g</string>
<string name="explanation_7">This family is my personal favorite and who got me started on the ketogenic diet. They have very useful information to help get you started.</string>

<string name="story_button"> Personal Story</string>


<string name="start_weight"> Starting Weight: </string>
<string name="current_weight"> Current Weight: </string>
<string name="change_weight"> Your Weight Change: </string>
<string name="goal_weight"> Your Goal Weight: </string>
<string name="title_activity_note_pad">NotePad</string>
<string name="title_activity_note_select">Saved Notes</string>
<string name="type_here">Type here...</string>


<string name="reps_title_hint">Reps</string>
<string name="reps_title_label">Amount of repetitions</string>

<string name="weight_title_hint">Weight</string>
<string name="weight_title_label">Amount of weight</string>


<string name="routine_done_label">Completed</string>
<string name="date_picker_title">Date routine started</string>


<string name="new_routine">Start a new routine</string>
<string name="delete_routine">Delete this exercise</string>
<string name="title_dialog_delete">Delete this item</string>
<string name="question_delete">Are you sure you want to delete this item?</string>

<string name="show_subtitle">Show Subtitle</string>
<string name="hide_subtitle">Hide Subtitle</string>
<string name="subtitle_format">%1$d routines</string>

<string name ="no_routines"> THere are no exercises on the list </string>
<string name="your_goal_weight">Your Goal Weight</string>

我的班级:

    package     hbratcher1992.buccaneer.atlantic.edu.fightingforfitness.Main.UInformation;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import hbratcher1992.buccaneer.atlantic.edu.fightingforfitness.R;

public class UserInformation extends AppCompatActivity
{

private int mStartWeight;
private int mCurrentWeight;
private int mWeightChange;
private int mGoalWeight;

private Button save;

int sWeightInt, cWeightInt, gWeightInt;


EditText startWeight, currentWeight, goalWeight;
//TextView weightChange;




@Override
protected void onCreate(Bundle savedInstanceState)

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_information);

    save = findViewById(R.id.btn_save);
    startWeight = findViewById(R.id.start_weight_entry);
    currentWeight = findViewById(R.id.current_weight_entry);
    goalWeight = findViewById(R.id.goal_weight_entry);
   // weightChange = findViewById(R.id.weight_change_show);

    //retrieve the data from shared preferences

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    int sWeightInt1 = prefs.getInt("sWeightInt", 0);
    startWeight.setText(sWeightInt1);

    int cWeightInt1 = prefs.getInt("cWeightInt", 0);
    startWeight.setText(cWeightInt1);

    int gWeightInt1 = prefs.getInt("gWeightInt", 0);
    startWeight.setText(gWeightInt1);

    save.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            sWeightInt = Integer.parseInt(startWeight.getText().toString());
            cWeightInt = Integer.parseInt(currentWeight.getText().toString());
            gWeightInt = Integer.parseInt(goalWeight.getText().toString());

            // this saves my data
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(UserInformation.this);
            SharedPreferences.Editor editor = prefs.edit();

            editor.putInt("sWeightInt", sWeightInt);
            editor.putInt("cWeightInt", cWeightInt);
            editor.putInt("gWeightInt", gWeightInt);
            editor.apply();

        }
    });

}


//these methods are unused as of now
   /* public int getStartWeight()
{
    return mStartWeight;
}

public void setStartWeight(int startWeight)
{
    mStartWeight = startWeight;
}

public int getCurrentWeight()
{
    return mCurrentWeight;
}

public void setCurrentWeight(int currentWeight)
{
    mCurrentWeight = currentWeight;
}

public int getWeightChange()
{
    return mWeightChange;
}

public void setWeightChange(int weightChange)
{
    mWeightChange = weightChange;
}

public int getGoalWeight()
{
    return mGoalWeight;
}

public void setGoalWeight(int goalWeight)
{
    mGoalWeight = goalWeight;
}

public void weightChangeMath(int c, int s)
{
    mWeightChange = mCurrentWeight - mStartWeight;
}*/


}

我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main.UInformation.UserInformation">

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="@string/title"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="@string/user_info_button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/title" />

<TextView
    android:id="@+id/start_weight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="72dp"
    android:text="@string/start_weight"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.093"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/title" />

<TextView
    android:id="@+id/current_weight"
    android:layout_width="125dp"
    android:layout_height="21dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="52dp"
    android:text="@string/current_weight"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.102"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/start_weight" />

<TextView
    android:id="@+id/change_weight"
    android:layout_width="145dp"
    android:layout_height="26dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="52dp"
    android:text="@string/change_weight"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.089"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/current_weight" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="52dp"
    android:text="@string/goal_weight"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.078"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/change_weight" />

<EditText
    android:id="@+id/start_weight_entry"
    android:layout_width="141dp"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="52dp"
    android:ems="10"
    android:hint="Start Weight"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.964"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/current_weight_entry"
    android:layout_width="141dp"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="40dp"
    android:ems="10"
    android:hint="Current Weight"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.964"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/start_weight_entry" />

<TextView
    android:id="@+id/weight_change_show"
    android:layout_width="141dp"
    android:layout_height="34dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="40dp"
    android:text="Change In Weight"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.964"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/current_weight_entry"
    tools:text="Change in weight" />

<EditText
    android:id="@+id/goal_weight_entry"
    android:layout_width="141dp"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="36dp"
    android:ems="10"
    android:hint="@string/your_goal_weight"
    android:inputType="number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.964"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/weight_change_show" />

<Button
    android:id="@+id/btn_save"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Save"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

 </android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

尝试

int sWeightInt1 = prefs.getInt("sWeightInt", 0);
    startWeight.setText(String.valueOf(WeightInt1));

    int cWeightInt1 = prefs.getInt("cWeightInt", 0);
    startWeight.setText(String.valueOf(cWeightInt1));

    int gWeightInt1 = prefs.getInt("gWeightInt", 0);
    startWeight.setText(String.valueOf(gWeightInt1));

您不能setText一个int的值,这会导致您的问题