1

I have a button action that calls the share intent. I would like to construct a string from strings.xml

Below I have my strings:

<string name="app_name">NowIcons</string> <string name="market">market://details?id=com.nmiltner.nowicon</string>

My string constructor is here:

    public String share = "Check out " + getResources().getString(R.string.app_name) + getResources().getString(R.string.market);

By button click is here:

        btn_share.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // Launching Share Intent
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, share);
            sendIntent.setType("text/plain");
            startActivity(sendIntent);


        }
    });

When I test the app, I get a force close, and the error log has a null pointer exception.

I am also using a similar method to send the user to the play store to rate the app.

public String marketurl = getResources().getString(R.string.market);

        btn_rate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // Go to market Listing

            Uri uri1 = Uri.parse(marketurl);
            startActivity(new Intent(Intent.ACTION_VIEW, uri1));

        }
    });

FIXED!!!

EDIT:

I was able to work it out, here is the fix:

    public String marketurl; 
public String share;

        btn_rate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // Go to market Listing
            String marketurl = getResources().getString(R.string.market);
            Uri uri1 = Uri.parse(marketurl);
            startActivity(new Intent(Intent.ACTION_VIEW, uri1));

        }
    });

        btn_share.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // Launching Share Intent
            String share = "Check out " + getResources().getString(R.string.app_name) + " " + getResources().getString(R.string.market) + " on the Play Store";
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, share);
            sendIntent.setType("text/plain");
            startActivity(sendIntent);

        }
    });
4

0 回答 0