Question

I'm trying to make the button (R.id.buttonring) call any phone number entered in the textview called tvTelefon. However, when i click that button, my phone is trying to call "w4126848130,511290,549" instead of the phone number entered in the textview. Any ideas how come? I don't receive any error messages so I'm clueless! Thanks!

public class Activity2 extends Activity {

    public static final String SPARAD_DATA = "sparadData";

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

        SharedPreferences sp = getSharedPreferences(SPARAD_DATA, Context.MODE_PRIVATE);
        TextView tvRubrik = (TextView)findViewById(R.id.textViewrubrik);
        tvRubrik.setText(sp.getString("rubrik", "Rubrik saknas"));
        TextView tvNamn = (TextView)findViewById(R.id.textViewnamn);
        tvNamn.setText(sp.getString("namn", "Namn saknas"));
        TextView tvText = (TextView)findViewById(R.id.textViewtext);
        tvText.setText(sp.getString("text", "Text saknas"));
        final TextView tvTelefon = (TextView)findViewById(R.id.textViewtelefon);
        tvTelefon.setText(sp.getString("telefon", "Telefon saknas"));
        TextView tvPris = (TextView)findViewById(R.id.textViewpris);
        tvPris.setText(sp.getString("pris", "Pris saknas"));

    Button r = (Button)findViewById(R.id.buttonring);
    r.setOnClickListener (new View.OnClickListener() {
        Intent call = new Intent(Intent.ACTION_DIAL);
        @Override
        public void onClick(View v){
            call.setData(Uri.parse("tel:" +tvTelefon));
            startActivity(call);
        }
        });
    }
}
Was it helpful?

Solution 2

Actually, how I understand your posted code you can just take the phone number straight from your preferences. Try this:

call.setData(Uri.parse("tel:" + sp.getString("telefon", "")));

Instead of:

call.setData(Uri.parse("tel:" +tvTelefon));

OTHER TIPS

Slight change might help you:

call.setData(Uri.parse("tel:" +tvTelefon));

It should be:

call.setData(Uri.parse("tel:" +tvTelefon.getText().toString()));

you are passing an object reference, get the string you put into it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top