How to count the number of characters in the string?

I need to after clicking the button in the TextView (Output) output the number of characters entered in EditText (Input)
public void Count(View View) {
 String S = Input.getText().toString();
Output.setText(S. length());
 }

For some reason it's not working, what am I doing wrong?
July 8th 19 at 11:26
2 answers
July 8th 19 at 11:28
Solution
Debugger in hands and forward to look that you have in the variable S which is Output and if Count is invoked at all.
July 8th 19 at 11:30
Solution
It's all right, the problem is not here, you have not called yourself Count, you have it wrong to "hang up" button, it is still possible that Input or Output, you get wrong, show the complete code.
(changed the names input, output, and btnCount)
import android.renderscript.ScriptGroup;
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;

public class AboutActivity extends AppCompatActivity {

 private EditText input;
 private TextView output;

@Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R. layout.activity_about);
 input = (EditText) findViewById(R. id.input);
 output = (TextView) findViewById(R. id.output);
}

 public void btnCount(View View) {
 String S = input.getText().toString();
output.setText(S. length());
}
}
- camron.Herzog commented on July 8th 19 at 11:33
As said - where's the challenge btnCount() You need to get your button (findViewByID) and he setOnClickListener transfer listener, which will cause btnCount. - nicklaus commented on July 8th 19 at 11:36
import android.renderscript.ScriptGroup;
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;

public class AboutActivity extends AppCompatActivity {

 private EditText input;
 private TextView output;

@Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R. layout.activity_about);
 input = (EditText) findViewById(R. id.input);
 output = (TextView) findViewById(R. id.output);
 Button btnCount = (Button) findViewById(R. id.btnCount);
 btnCount.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View View) {
onButtonClick((Button)view);
}

});
}

 public void onButtonClick(view Button){
 String S = input.getText().toString();
output.setText(S. length());
}
}

In the end I got confused, nothing works :( - camron.Herzog commented on July 8th 19 at 11:39
: Could in XML using onClick to do, but I do not see it, and then it would have been called not btnCount, and btnCount_onClick, etc. - mazie.Towne commented on July 8th 19 at 11:42
: Like done as needed, but still error
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;

public class MainActivity extends AppCompatActivity {

 private EditText input;
 private TextView output;

@Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R. layout.activity_main);
 input = (EditText) findViewById(R. id.input);
 output = (TextView) findViewById(R. id.output);
 Button Count = (Button) findViewById(R. id.btnCount);
 Count.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View View) {
onButtonClick();
}

});
}
 public void onButtonClick(){
 String S = input.getText().toString();
output.setText(S. length());
}

}
- camron.Herzog commented on July 8th 19 at 11:45
: What's the error? When you execute? See LogCat. Maybe the layout is confused, you have a MainActivity, that About. - mazie.Towne commented on July 8th 19 at 11:48
When you press the button
06-28 23:21:40.394 1499-1499/EN.m9ru.test W/ResourceType: No package identifier when getting value for resource number 0x00000006
06-28 23:21:40.394 1499-1499/EN.m9ru.test D/AndroidRuntime: Shutting down VM
06-28 23:21:40.394 1499-1499/EN.m9ru.test W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4164fce0)
06-28 23:21:40.424 1499-1499/EN.m9ru.test E/AndroidRuntime: FATAL EXCEPTION: main
 Process: ru.m9ru.test, PID: 1499
 android.content.Res. Resources$NotFoundException: String resource ID #0x6
 at android.content.Res. Resources.getText(Resources.java:265)
 at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
 at android.widget.TextView.setText(TextView.java:3888)
 at ru.m9ru.test.MainActivity.onButtonClick(MainActivity.java:32)
 at ru.m9ru.test.MainActivity$1.onClick(MainActivity.java:25)
 at android.view.View.performClick(View.java:4445)
 at android.view.View$PerformClick.run(View.java:18446)
 at android.os.Handler.handleCallback(Handler.java:733)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:136)
 at android.app.ActivityThread.main(ActivityThread.java:5146)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:515)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
 at dalvik.system.NativeStart.main(Native Method)
- camron.Herzog commented on July 8th 19 at 11:51
: Confused activity_main show. - mazie.Towne commented on July 8th 19 at 11:54
:
<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="parent" android:layout_height="parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="ru.m9ru.test.MainActivity">

 <edittext android:layout_width="parent" android:layout_height="wrap content" android:id="@+id/input" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true">

 <textview android:layout_width="wrap content" android:layout_height="wrap content" android:text="New Text" android:id="@+id/output" android:layout_below="@+id/input" android:layout_alignparentleft="true" android:layout_alignparentstart="true">

 </textview></edittext></relativelayout>
- camron.Herzog commented on July 8th 19 at 11:57
Here is interesting. See setText in my opinion has two overloads - when a string is passed, and when passed the id of the string resource. But - id is in fact int. Accordingly, when you are trying to convey length(), it calls the overload for id. Of course at the specified id (which is actually the length of the string) is nothing, and collapses the exception. What you need to do is convert the number to a string. For example: output.setText(S. length().toString()); - nicklaus commented on July 8th 19 at 12:00
: output.setText(String.valueOf(S. length())); - mazie.Towne commented on July 8th 19 at 12:03
: S. length().toString() will not work, there is not Integer, and int is a basic type, it has no methods. - mazie.Towne commented on July 8th 19 at 12:06
Yes, I agree. - nicklaus commented on July 8th 19 at 12:09
: I tried as you wrote and so, the result is the same
public void onButtonClick(){
 String S = input.getText().toString();
 int D = S. length();
output.setText(D);
 }
- camron.Herzog commented on July 8th 19 at 12:12
I did not write. Read carefully. You need the result - the length of the string to cause the string type, not numeric. - nicklaus commented on July 8th 19 at 12:15
: This making any sense at all, more like this:
public void onButtonClick(){
String S = input.getText().toString();
String D = String.valueOf(S. length());
output.setText(D);
} - mazie.Towne commented on July 8th 19 at 12:18
: : Thank you very much for the help! - camron.Herzog commented on July 8th 19 at 12:21

Find more questions by tags Android