-5

In my application, after clicking the button it should be able to call the corresponding number (which was given by the user). But the problem is when I dial, it says unfortunately the project has stopped.Now I am running the app in emulator, and I know that without network it shouldn't work. Instead it was supposed to show an error message. But why it stopps application?

Activity 1:

    public class Activity1 extends ActionBarActivity {


    public static final String PREFS_TITLE = "SaveFile";

    TextView phnNum;
    public static final String Mob = "CallButton";

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

        //finting the properties by given ID
        final EditText title = (EditText) findViewById(R.id.title);
        final EditText name = (EditText) findViewById(R.id.name);
        final EditText text = (EditText) findViewById(R.id.text);

        phnNum = (TextView) findViewById(R.id.phone);
        final EditText price = (EditText) findViewById(R.id.price);


        //adding the button clickable functionality
        Button nextViewButton = (Button) findViewById(R.id.Next_View);
        nextViewButton.setOnClickListener(new View.OnClickListener() {


                                              public void onClick(View view) {


                                                  SharedPreferences saveValue = getSharedPreferences(PREFS_TITLE, 0);


                                                  SharedPreferences.Editor editor = saveValue.edit();

                                                  editor.putString("Title", title.getText().toString());
                                                  editor.putString("Name", name.getText().toString());
                                                  editor.putString("Text", title.getText().toString());
                                                  editor.putString("Phone", phnNum.getText().toString());
                                                  editor.putString("Price", price.getText().toString());

                                                  editor.commit();

                                                  Intent intent = new Intent((getApplicationContext()), Activity2.class);
                                                  startActivity(intent);

                                              }
                                          }


        );


    }

}

Activity 2:

public class Activity2 extends ActionBarActivity {

    public static String PREFS_TITLE = "SaveFile";

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


        TextView title_Avtivity2=(TextView)findViewById(R.id.title);
        TextView name_Avtivity2=(TextView)findViewById(R.id.name);
        TextView text_Avtivity2=(TextView)findViewById(R.id.text);
        TextView phon_Avtivity2=(TextView)findViewById(R.id.phone);
        TextView price_Avtivity2=(TextView)findViewById(R.id.price);

        Button callingButton=(Button)findViewById(R.id.call);
        SharedPreferences savedValue_Activity2=getSharedPreferences(PREFS_TITLE,0);
        title_Avtivity2.setText(savedValue_Activity2.getString("Title","No input"));
        name_Avtivity2.setText(savedValue_Activity2.getString("Name","No input"));
        text_Avtivity2.setText(savedValue_Activity2.getString("Text","No input"));
        phon_Avtivity2.setText(savedValue_Activity2.getString("Phone","No input"));
       price_Avtivity2.setText(savedValue_Activity2.getString("Price","No input"));
    }

public void call(View view){
    SharedPreferences getphonNum=getSharedPreferences(PREFS_TITLE,0);
    String warningMsg=getphonNum.getString("Phone","No phone number input");
    System.out.println(warningMsg);
    Intent intentForPhon=new Intent(Intent.ACTION_CALL,Uri.parse("Phon Num: "+warningMsg));
startActivity(intentForPhon);
}


}
Riyana
  • 241
  • 13
  • 29
  • `unfortunately the project has stopped` then add logcat result with question also use `Uri.parse(warningMsg)` – ρяσѕρєя K Feb 12 '15 at 16:15
  • i am quite new with Android studio and I don't know very much about debugging process. However I will show the entire logcat result when I run the application. And also know that I have two activities. – Riyana Feb 12 '15 at 16:21
  • I guess you requested the appropriate permission... did you? – Phantômaxx Feb 12 '15 at 16:21
  • @Riyana: please show full logcat result – ρяσѕρєя K Feb 12 '15 at 16:22
  • full logcat result is too big. it beyonds the character limit for posting here. – Riyana Feb 12 '15 at 16:24
  • 1
    possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Selvin Feb 12 '15 at 16:38
  • Selvin, possible doesn't mean "certain". And again my post has different issue, its not the duplicate of that one u linked. – Riyana Feb 12 '15 at 16:43

1 Answers1

1

make sure you parse the right Uri

public void call(View view){
    SharedPreferences getphonNum=getSharedPreferences(PREFS_TITLE,0);
    String phoneNumber = getphonNum.getString("Phone","No phone number input");
    PhoneNumberUtils phoneNumberUtils = new PhoneNumberUtils();
    if(phoneNumberUtils.isGlobalPhoneNumber(phoneNumber)){
        Intent intentForPhon=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneNumber));
        startActivity(intentForPhon);
    } else {
        // do something else (throw exception or something)
    }
}

and make sure you have this permission in you manifest

 <uses-permission android:name="android.permission.CALL_PHONE" />

and make sure you set the OnClickListener on you button

Button callingButton = (Button) findViewById(R.id.call);
callingButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        call();
    }
});
ColdFire
  • 6,764
  • 6
  • 35
  • 51
  • A.A, how can I make sue about that one u said to be make sure? – Riyana Feb 12 '15 at 16:44
  • Hi, thanks A.A, its very kind of u that u took step to help me. but this time nothing happens after I click Dial button. Seems calling method doesn't interact with the button. – Riyana Feb 12 '15 at 17:18
  • you should set a click listener for your button like you did in `Activity1` – ColdFire Feb 12 '15 at 17:22
  • OO, Thank u so much my friend A.A. Would be better if I knew your real name. However, my application is working now as I expected. XOXO – Riyana Feb 12 '15 at 18:34