Home>

Android super beginner.
I am trying to make an Android application that creates a string repeatedly until it reaches the specified number of characters using Java, but when the activity that displays the result is started using Intent, the application stops working The

Error message

No error message when writing or building.
Addendum: When ADB LogCat Error is displayed

at runtime
08-30 03: 58: 30.247 6374-6381 /? E/zygote: Failed writing handshake bytes (-1 of 14): Broken pipe</code></pre >
<p>When clicking the "Create" button</p>
<pre><code data-language = "Log">08-30 03: 58: 44.024 6374-6374/com.sample.stringloop E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sample.stringloop, PID: 6374
    java.lang.RuntimeException: Unable to start activity ComponentInfo {com.sample.stringloop/com.sample.stringloop.DisplayResultActivity}: android.content.res.Resources $NotFoundException: String resource ID # 0x64
        at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11 (Unknown Source: 0)
        at android.app.ActivityThread $H.handleMessage (ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage (Handler.java:106)
        at android.os.Looper.loop (Looper.java:164)
        at android.app.ActivityThread.main (ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke (Native Method)
        at com.android.internal.os.RuntimeInit $MethodAndArgsCaller.run (RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)
     Caused by: android.content.res.Resources $NotFoundException: String resource ID # 0x64
        at android.content.res.Resources.getText (Resources.java:339)
        at android.widget.TextView.setText (TextView.java:5496)
        at com.sample.stringloop.DisplayResultActivity.onCreate (DisplayResultActivity.java:36)
        at android.app.Activity.performCreate (Activity.java:7009)
        at android.app.Activity.performCreate (Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11 (Unknown Source: 0)
        at android.app.ActivityThread $H.handleMessage (ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage (Handler.java:106)
        at android.os.Looper.loop (Looper.java:164)
        at android.app.ActivityThread.main (ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke (Native Method)
        at com.android.internal.os.RuntimeInit $MethodAndArgsCaller.run (RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)
The error log

has appeared. (I'm really sorry for the ignorance, but I still don't understand the meaning, so please cooperate.)

Applicable source code

I don't know which code has the problem, so there may be some code that is not needed, sorry.

  1. MainActivity.java
package com.sample.stringloop;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "com.sample.stringloop.MESSAGE1";
    public static final String EXTRA_MESSAGE3 = "com.sample.stringloop.MESSAGE3";
    public static final String EXTRA_BOOLEAN = "com.sample.stringloop.BOOLEAN";
    @Overrideprotected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
    }
    / * Behavior when pressing the create button * /
    public void sendMessage (View view) {
        Intent intent = new Intent (this, DisplayResultActivity.class);
        EditText editText = (EditText) findViewById (R.id.editText);
        EditText editText3 = (EditText) findViewById (R.id.editText3);
        CheckBox checkBox = (CheckBox) findViewById (R.id.checkBox);
        String mojiretsu = editText.getText (). ToString ();
        int mojisuu = Integer.parseInt (editText3.getText (). toString ());
        boolean kaigyo = checkBox.isChecked ();
        intent.putExtra (EXTRA_MESSAGE, mojiretsu);
        intent.putExtra (EXTRA_MESSAGE3, mojisuu);
        intent.putExtra (EXTRA_BOOLEAN, kaigyo);
        startActivity (intent);
    }
}

2.DisplayResultActivity.java

package com.sample.stringloop;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayResultActivity extends AppCompatActivity {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_display_result);
        / * Get Intent first * /
        Intent intent = getIntent ();
        String mojiretsu = intent.getStringExtra (MainActivity.EXTRA_MESSAGE);
        int mojisuu = intent.getIntExtra (MainActivity.EXTRA_MESSAGE3,0);
        boolean kaigyo = intent.getBooleanExtra (MainActivity.EXTRA_BOOLEAN, false);
        int kaigyoi = kaigyo? 1: 0;
        / * Create repeated string * /
        StringBuilder sb = new StringBuilder ();
        while (sb.length () + mojiretsu.length () + kaigyoi&lt;= mojisuu) {
            sb.append (mojiretsu);
            if (kaigyo) {
                sb.append (mojiretsu);
            }
        }
        /*display*/
        TextView textView3 = findViewById (R.id.textView3);
        TextView textView4 = findViewById (R.id.textView4);
        TextView editText4 = findViewById (R.id.editText4);
        textView3.setText (mojiretsu);
        textView4.setText (mojisuu);
        editText4.setText (new String (sb));
    }
}
Tried

When I looked up on the Internet, there seemed to be a few people who were in this situation, but for the first time I made an app on Android, I couldn't understand well.

Supplemental information (FW/tool version etc.)

The production environment is Android Studio for Windows (target is Android4.4), and the execution environment is ADB of Nexus5X (Android8.1) and the actual machine of GalaxyS8 (Android8.0).

  • Answer # 1

    It looks like the same symptom.
    http://ksoichiro.blogspot.com/2011/05/android-resourcesnotfoundexception.html

    DisplayResultActivity.java Line 36
    textView4.setText (mojisuu);to
    TrytextView4.setText (String.valueOf (mojisuu));

  • Answer # 2

    I'm sorry for the partial answer, but about the problem of screen transition.
    Integer.parceInt of the image is supposed to be converted to int type when the target string is a number, but if something other than numbers is in it, it can not be converted to int type, so an exception occurs and the application Will fall.
    The screen transition succeeded when I replaced it with "12" as shown in the image.
    However, it was good while commenting out the place of StringBuilder, but when it is removed, the screen goes dark.

    Nobody will tell you how to check for errors, but if you don't know what to look at the error log or look at it, leave a part like the image and comment out everything else, There are ways to determine if you are experiencing the problem.
    In this case, since the screen transition is a problem, if you add a star and say that there is a problem in the onCreate or layout file near the intent, and comment out from one end, it will be pretty much.

  • Answer # 3

    How to read the error log. At first glance, you may be confused when a large number of strings are displayed. This is called a backtrace, which shows the trace of the calling method from the point where the program ended abnormally due to an error. From this, you can get the star of the location where the error occurred just by extracting what you created and its surroundings.

    In this case,

    at android.widget.TextView.setText (TextView.java:5496)
            at com.sample.stringloop.DisplayResultActivity.onCreate (DisplayResultActivity.java:36)


    From this part, it can be inferred that an error has occurred in the processing of the setText method called from the 36th line of DisplayResultActivity.java. The answer has already been given, but it may be because an int type number was given to the argument of this setText.

    If an int number is given to the setText method, it will be processed as a resource ID. A resource ID is created in an automatically generated file called R.java. A new ID is added when a layout is created or a string is added to string.xml. R.layout.activity_main is one of them. These entities are int type numbers. If an int type number is given to the argument of setText, it will be processed as such a resource ID. If the target doesn't exist, an error occurs and the app drops.