Android Build an Interactive Story App Finishing the User Interface Formatting Strings

Hala Almaimoni
Hala Almaimoni
332 Points

The interactiveStory page doesn't work

I got this error:

2019-08-27 11:32:55.314 6121-6121/com.example.intractivestory E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.intractivestory, PID: 6121 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.intractivestory/com.example.intractivestory.ui.StoryActivity}: android.content.res.Resources$NotFoundException: Drawable com.example.intractivestory:string/page0 with resource ID #0x7f0b0029 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: android.content.res.Resources$NotFoundException: Drawable com.example.intractivestory:string/page0 with resource ID #0x7f0b0029 Caused by: android.content.res.Resources$NotFoundException: File On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. Even stranger, it's calling you by name: "Help me, %1$s, you're my only hope." from drawable resource ID #0x7f0b0029 at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:876) at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:659) at android.content.res.Resources.getDrawableForDensity(Resources.java:906) at android.content.res.Resources.getDrawable(Resources.java:845) at android.content.Context.getDrawable(Context.java:687) at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:463) at com.example.intractivestory.ui.StoryActivity.pageLoad(StoryActivity.java:46) at com.example.intractivestory.ui.StoryActivity.onCreate(StoryActivity.java:41) at android.app.Activity.performCreate(Activity.java:7802) at android.app.Activity.performCreate(Activity.java:7791) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: java.io.FileNotFoundException: On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. Even stranger, it's calling you by name: "Help me, %1$s, you're my only hope." at android.content.res.AssetManager.nativeOpenNonAsset(Native Method) at android.content.res.AssetManager.openNonAsset(AssetManager.java:941) at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:865) at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:659)  at android.content.res.Resources.getDrawableForDensity(Resources.java:906)  at android.content.res.Resources.getDrawable(Resources.java:845)  at android.content.Context.getDrawable(Context.java:687)  at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:463)  at com.example.intractivestory.ui.StoryActivity.pageLoad(StoryActivity.java:46)  at com.example.intractivestory.ui.StoryActivity.onCreate(StoryActivity.java:41)  at android.app.Activity.performCreate(Activity.java:7802)  at android.app.Activity.performCreate(Activity.java:7791)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)  at android.os.Handler.dispatchMessage(Handler.java:107)  at android.os.Looper.loop(Looper.java:214)  at android.app.ActivityThread.main(ActivityThread.java:7356)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)  2019-08-27 11:32:55.337 6121-6121/com.example.intractivestory I/Process: Sending signal. PID: 6121 SIG: 9

1 Answer

Please provide your code or we will not be able to see what is wrong.

Check this out though. I think you have mistake in creating String resource

Caused by: android.content.res.Resources$NotFoundException: Drawable com.example.intractivestory:string/page0 with resource ID #0x7f0b0029 Caused by: android.content.res.Resources$NotFoundException: File On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. Even stranger, it's calling you by name: "Help me, %1$s, you're my only hope." from drawable resource ID #0x7f0b0029
Hala Almaimoni
Hala Almaimoni
332 Points

This is the Story class:

package com.example.intractivestory.model;

import com.example.intractivestory.R;

public class Story { private Page[] pages;

public Story() {
    pages = new Page[7];

    pages[0] = new Page(R.drawable.page0,
            R.string.page0,
            new Choice(R.string.page0_choice1, 1),
            new Choice(R.string.page0_choice2, 2));

    pages[1] = new Page(R.drawable.page1,
            R.string.page1,
            new Choice(R.string.page1_choice1, 3),
            new Choice(R.string.page1_choice2, 4));

    pages[2] = new Page(R.drawable.page2,
            R.string.page2,
            new Choice(R.string.page2_choice1, 4),
            new Choice(R.string.page2_choice2, 6));

    pages[3] = new Page(R.drawable.page3,
            R.string.page3,
            new Choice(R.string.page3_choice1, 4),
            new Choice(R.string.page3_choice2, 5));

    pages[4] = new Page(R.drawable.page4,
            R.string.page4,
            new Choice(R.string.page4_choice1, 5),
            new Choice(R.string.page4_choice2, 6));

    pages[5] = new Page(R.drawable.page5, R.string.page5);

    pages[6] = new Page(R.drawable.page6, R.string.page6);
}

public Page getPage(int pageNo) {
    if (pageNo >= pages.length) {
        pageNo = 0;
    }
    return pages[pageNo];
}

}


This is StoryActivity page:

package com.example.intractivestory.ui;

import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView;

import com.example.intractivestory.R; import com.example.intractivestory.model.Page; import com.example.intractivestory.model.Story;

public class StoryActivity extends AppCompatActivity { private Story story; private String name; private ImageView storyImageView; private TextView storyTextView; private Button choice1Button; private Button choice2Button;

public static final String TAG = StoryActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);

    storyImageView = findViewById(R.id.storyImageView);
    storyTextView = findViewById(R.id.storyTextView);
    choice1Button = findViewById(R.id.choice1Button);
    choice2Button = findViewById(R.id.choice2Button);

    Intent intent = getIntent();
    name = intent.getStringExtra(getString(R.string.key_name));
    Log.d(TAG, name);

    story = new Story();
    pageLoad(0);
}

private void pageLoad(int pageNo) {
    Page page = story.getPage(pageNo);
    Drawable image = ContextCompat.getDrawable(this, page.getImageId());
    storyImageView.setImageDrawable(image);

    String pageText = getString(page.getTextId());
    pageText = String.format(pageText, name);
    storyTextView.setText(pageText);
    choice1Button.setText(page.getChoice1().getTextId());
    choice2Button.setText(page.getChoice2().getTextId());
}

}