1

I am making an application that provides the facility of a dialer. I have created a keypad and provided functionality of a keypad behind it. It should detect incoming call, dial a call using telephonyManager etc. But when I run the code it gives the following error:

Sorry, com.maju.yourApp has stopped unexpectidly

I don't have any errors in the code and I don't have any idea about the reason. Can someone please identify the cause of the problem? The code I am trying is as follows:

public class MainActivity extends Activity {

EditText ed =(EditText) findViewById(R.id.editText1);
String number;
Intent intent;

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

    PhoneCallListener listenerObj = new PhoneCallListener();
    TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(listenerObj,PhoneStateListener.LISTEN_CALL_STATE);
}

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


public void one_btn(View v)
{
    number = number+"1";
    ed.setText(number);
}

public void two_btn(View v)
{
    number = number+"2";
    ed.setText(number);
}

public void three_btn(View v)
{
    number = number+"3";
    ed.setText(number);
}

public void four_btn(View v)
{
    number = number+"4";
    ed.setText(number);
}

public void five_btn(View v)
{
    number = number+"5";
    ed.setText(number);
}

public void six_btn(View v)
{
    number = number+"6";
    ed.setText(number);
}

public void seven_btn(View v)
{
    number = number+"7";
    ed.setText(number);
}

public void eight_btn(View v)
{
    number = number+"8";
    ed.setText(number);
}

public void nine_btn(View v)
{
    number = number+"9";
    ed.setText(number);
}

public void esterisk_btn(View v)
{
    number = number+"*";
    ed.setText(number);
}

public void zero_btn(View v)
{
    number = number+"0";
    ed.setText(number);
}

public void hash_btn(View v)
{
    number = number+"#";
    ed.setText(number);
}

public void call_btn(View v)
{
    intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:" + number));
    startActivity(intent);
}

public void back_btn(View v)
{
    if(number.length() > 0)
    {
        String temp = "";

        for(int i=0; i<number.length() - 1; i++)
        {
            temp += number.charAt(i);
        }

        number = temp;
    }
}

private class PhoneCallListener extends PhoneStateListener
{
    long start;
    long end;
    Context context = getApplicationContext();
    Toast toast;
    boolean flag = false;

    public void onCallStateChanged(int state, String incomingNumber)
    {
        if(TelephonyManager.CALL_STATE_OFFHOOK == state)
        {
            start = System.currentTimeMillis();
            Log.i("Log" , "Phone State Calling");
            toast = Toast.makeText(context, "Phone State Calling" , Toast.LENGTH_LONG);
            toast.show();
            flag = true;
        }

        else if(TelephonyManager.CALL_STATE_IDLE == state)
        {
            Log.i("Log" , "Phone State Idle");
            toast = Toast.makeText(context, "Phon State Idle", Toast.LENGTH_LONG);
            toast.show();

            if(flag)
            {
                end = System.currentTimeMillis();
                long duration = (end - start)/1000;
                Log.i("Log" , "Call Ended, Duration: "+ duration);

                toast = Toast.makeText(context, "Call Ended\n Duration: "+ duration, Toast.LENGTH_LONG);
                toast.show();

                flag = false;
            }
        }

        else if(TelephonyManager.CALL_STATE_RINGING == state)
        {
            Log.i("Log", "Incoming Call From " + incomingNumber);
            toast = Toast.makeText(context, "Incoming Call: "+ incomingNumber, Toast.LENGTH_LONG);
            toast.show();
        }


    }

}

}

4

1 回答 1

1

Your problem is here:

EditText ed =(EditText) findViewById(R.id.editText1);

You cannot use findViewById until after you use setContentView.

Move this line into onCreate, after setContentView().

In he future, don't think "I don't have any errors in the code". What you actually mean is that the compiler doesn't show you any errors but you do have (at least) one error. It's an important point because no compiler errors is just the first step in working out if you have any errors. Please also post the logcat stack trace as a matter of habit when asking about crashes. I bet yours shows a "null pointer exception".

于 2012-11-11T15:47:41.167 回答