2

my app always crashes when I press a button. I'm new to android, so could you help me?

When the user presses the button, it is supposed to increment an int and then display it in a textView.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendMessage"
        android:text="@string/button_send" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/text"
        android:textSize="30sp"
        tools:context=".DisplayMessageActivity" />

    <!-- android:visibility="invisible" -->

    <TextView
        android:id="@+id/val"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:text="            " />

</RelativeLayout>

MainActivity.java

package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    public final static String  EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    public int mult = 1;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    /** Called when the user clicks on the Send button */
    public void sendMessage(View view)
    {
        //Intent intent = new Intent(this, DisplayMessageActivity.class);
        //TextView text = (TextView) findViewById(R.id.text);

        TextView val = (TextView) findViewById(R.id.val);
        val.setText(mult);
        mult+=1;
        //EditText editText = (EditText) findViewById(R.id.edit_message);
        //String message = editText.getText().toString();
        //intent.putExtra(EXTRA_MESSAGE, message);
        //startActivity(intent);
    }
}

here is the crash log

11-07 14:23:05.663: I/Choreographer(1126): Skipped 32 frames!  The application may be doing too much work on its main thread.
11-07 14:23:05.898: D/gralloc_goldfish(1126): Emulator without GPU emulation detected.
11-07 14:23:10.543: I/Choreographer(1126): Skipped 51 frames!  The application may be doing too much work on its main thread.
11-07 14:23:11.076: W/ResourceType(1126): No package identifier when getting value for resource number 0x00000001
11-07 14:23:11.083: D/AndroidRuntime(1126): Shutting down VM
11-07 14:23:11.083: W/dalvikvm(1126): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-07 14:23:11.263: E/AndroidRuntime(1126): FATAL EXCEPTION: main
11-07 14:23:11.263: E/AndroidRuntime(1126): java.lang.IllegalStateException: Could not execute method of the activity
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.view.View$1.onClick(View.java:3591)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.view.View.performClick(View.java:4084)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.view.View$PerformClick.run(View.java:16966)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.os.Handler.handleCallback(Handler.java:615)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.os.Looper.loop(Looper.java:137)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at java.lang.reflect.Method.invoke(Method.java:511)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at dalvik.system.NativeStart.main(Native Method)
11-07 14:23:11.263: E/AndroidRuntime(1126): Caused by: java.lang.reflect.InvocationTargetException
11-07 14:23:11.263: E/AndroidRuntime(1126):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at java.lang.reflect.Method.invoke(Method.java:511)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.view.View$1.onClick(View.java:3586)
11-07 14:23:11.263: E/AndroidRuntime(1126):     ... 11 more
11-07 14:23:11.263: E/AndroidRuntime(1126): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.content.res.Resources.getText(Resources.java:229)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.widget.TextView.setText(TextView.java:3620)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at com.example.myfirstapp.MainActivity.sendMessage(MainActivity.java:34)
11-07 14:23:11.263: E/AndroidRuntime(1126):     ... 14 more
11-07 14:23:15.013: I/Process(1126): Sending signal. PID: 1126 SIG: 9
3
  • Can you show the crash logs, please?
    – Cat
    Nov 7, 2012 at 19:33
  • 2
    Instead of adding "(solved)" to the title, click the checkbox next to the correct answer.
    – Cat
    Nov 7, 2012 at 19:52
  • 2
    If you found an answer that works click the empty check mark box below the voting arrows on the answer that you found most helpful
    – jcw
    Nov 7, 2012 at 19:52

2 Answers 2

2

You cannot set the content of a text view to an int. Instead you have to change it to a string first

Replace the line

val.setText(mult);

With

val.setText(Integer.toString(mult));
1
  • Sorry, I am having trouble editing my answer, it is NOT mult.toString() , it is val.setText(Integer.toString(mult));
    – jcw
    Nov 7, 2012 at 19:40
1

I believe the setText method looks for a resource if you pass in an int. You need to change the variable mult to a String before you can use setText. Either of the following will work:

val.setText("" + mult);

Or

val.setText(Integer.toString(mult));
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.