NoobProgrammer
NoobProgrammer

Reputation: 61

Android application keeps on stopping

I am new to android programming. I need an enlightenment on my android application. I don't know what's wrong but when I pressed my "calculate" button, it will stop working (crash). My program is about the FLAMES game where in you count each instance a character of a string intersects in another string. I am hoping to learn more, each help will be greatly appreciated. Thanks!

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Button button1 = (Button) findViewById(R.id.button);
    button1.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
         EditText edittext1 =  (EditText) findViewById(R.id.editText);
         final String name1 = edittext1.toString().replaceAll("\\s+","");
         EditText edittext2 = (EditText) findViewById(R.id.editText2);
         final String name2 = edittext2.toString().replaceAll("\\s+","");
         final TextView result = (TextView) findViewById(R.id.textView);

         int common = 0;
         int resultS = common % 6;

         if (name1.length() > name2.length()){

             for (int i = 0; i < name1.length(); i++) {
                 for (int j = 0; j < name2.length(); j++) {
                     if (name1.charAt(i) == name2.charAt(j)) {
                         common++;

                     }
                         result.setText(resultS);
                 }
             }


         }

         if(name2.length() > name1.length())
         {

             for (int i = 0; i < name2.length(); i++) {
                 for (int j = 0; j < name1.length(); j++) {
                     if (name2.charAt(i) == name1.charAt(j)) {
                         common++;

                     }
                     result.setText(resultS);


                 }


             }

         }

    }
});

Logcat:

07-09 14:53:06.852 8302-8302/com.example.genesis.flames E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.genesis.flames, PID: 8302
                                                                      android.content.res.Resources$NotFoundException: String resource ID #0x0
                                                                          at android.content.res.Resources.getText(Resources.java:338)
                                                                          at android.widget.TextView.setText(TextView.java:5480)
                                                                          at com.example.genesis.flames.MainActivity$1.onClick(MainActivity.java:56)
                                                                          at android.view.View.performClick(View.java:6219)
                                                                          at android.view.View$PerformClick.run(View.java:24482)
                                                                          at android.os.Handler.handleCallback(Handler.java:769)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                          at android.os.Looper.loop(Looper.java:164)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6540)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Upvotes: 0

Views: 161

Answers (1)

Basil Battikhi
Basil Battikhi

Reputation: 2668

  result.setText(String.valueOf(resultS));

because of textview expecting a string and you send the parameter as integer also result.setText(""+resultS); should work (this one is not tested)

Upvotes: 2

Related Questions