1
public class ShowActivity extends Activity  implements OnClickListener{

    private LinearLayout llaouyBase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboardactivity);
        llaouyBase = (LinearLayout) findViewById(R.id.llaouyBase);
        Button t;

        for(int i=0;i<5;i++) {
            t= new Button(ShowActivity.this);
            t.setId(i);
            t.setOnClickListener(this);
            llaouyBase.addView(t, i);
        }
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(),
                v.getId(), 8000).show();
    }
}

This is my activity, In the layout i have a blank linear layout with orientation vertical.Im trying to add 5 buttons in which i could do successfully.How can i add click events to these buttons?While running this, im getting Resource Not found exception.

07-04 12:22:07.535: E/AndroidRuntime(20957): FATAL EXCEPTION: main
07-04 12:22:07.535: E/AndroidRuntime(20957): android.content.res.Resources$NotFoundException: String resource ID #0x3
07-04 12:22:07.535: E/AndroidRuntime(20957):    at android.content.res.Resources.getText(Resources.java:233)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at android.widget.Toast.makeText(Toast.java:265)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at com.example.testapp.ShowActivity.onClick(ShowActivity.java:55)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at android.view.View.performClick(View.java:4103)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at android.view.View$PerformClick.run(View.java:17117)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at android.os.Handler.handleCallback(Handler.java:615)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at android.os.Looper.loop(Looper.java:137)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at android.app.ActivityThread.main(ActivityThread.java:4744)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at java.lang.reflect.Method.invokeNative(Native Method)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at java.lang.reflect.Method.invoke(Method.java:511)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-04 12:22:07.535: E/AndroidRuntime(20957):    at dalvik.system.NativeStart.main(Native Method)

I followed this link give below.What am i missing in this code?

How to identify the button clicked from a dynamically generated table

4 Answers 4

3

Change

Toast.makeText(getApplicationContext(),
                    v.getId(), 8000).show();

with

Toast.makeText(getApplicationContext(),
                    String.valueOf(v.getId()), 8000).show();

If you pass an int value as second parameter to makeText android will look for a String with that id inside R.string. If it does not exist your app will crash for android.content.res.Resources$NotFoundException:

Sign up to request clarification or add additional context in comments.

2 Comments

always quick. beat me to it.:)
:) I thinked the same thing
2

This is the problem

     Toast.makeText(getApplicationContext(),
                v.getId(), 8000).show();

Change it to

      Toast.makeText(getApplicationContext(),
                ""+v.getId(), 8000).show();

or

        Toast.makeText(getApplicationContext(),
            String.valueOf(v.getId()), 8000).show();

You are using the below

public static Toast makeText (Context context, int resId, int duration)

Make a standard toast that just contains a text view with the text from a resource.

Parameters

context The context to use. Usually your Application or Activity object.
resId   The resource id of the string resource to use. Can be formatted text.
duration    How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

if the resource can't be found Throws

Resources.NotFoundException

Its expecting a resource which is an int which does not exist. Hence the Exception.

You should use the below

public static Toast makeText (Context context, CharSequence text, int duration)

The above method expects CharacterSequence so use String.valueOf(v.getId()).

Comments

1

Change

Toast.makeText(getApplicationContext(),
                    v.getId(), 8000).show();

to

Toast.makeText(getApplicationContext(),
                    v.getId().toString(), 8000).show();

Comments

1

I think I found it here:

public static Toast makeText (Context context, int resId, int duration)

Added in API level 1 Make a standard toast that just contains a text view with the text from a resource.

Parameters context The context to use. Usually your Application or Activity object. resId The resource id of the string resource to use. Can be formatted text. duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG Throws if the resource can't be found. Resources.NotFoundException public static Toast makeText (Context context, CharSequence text, int duration)

Added in API level 1 Make a standard toast that just contains a text view.

Parameters context The context to use. Usually your Application or Activity object. text The text to show. Can be formatted text. duration How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

Change

    Toast.makeText(getApplicationContext(),
            v.getId(), 8000).show();

To

    Toast.makeText(getApplicationContext(),
            String.valueOf(v.getId()), Toast.LENGTH_LONG).show();

2 Comments

and why does He get a ResNotFoundException?
You're right, it should be String in the second parameter. I'll edit it.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.