请帮我解决错误com.maju.your App意外停止了

时间:2012-11-11 15:40:35

标签: android telephony mobile-application phone-call

我正在制作一个提供拨号器功能的应用程序。我创建了一个键盘,并在其后面提供了键盘功能。它应该检测来电,使用telephonyManager拨打电话等。但是当我运行代码时,它会出现以下错误:

抱歉,com.maju.yourApp已经无意中停止了

我的代码中没有任何错误,我不知道原因。有人可以确定问题的原因吗?我正在尝试的代码如下:

公共类MainActivity扩展了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();
        }


    }

}

}

1 个答案:

答案 0 :(得分:1)

你的问题在这里:

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

在使用setContentView之前,不能使用findViewById。

在setContentView()之后将此行移动到onCreate。

在他的未来,不要思考&#34;我在代码中没有任何错误&#34;。你的意思是编译器没有向你显示任何错误,但你确实有(至少)一个错误。这是一个重要的观点,因为如果您有任何错误,编译错误只是解决问题的第一步。在询问崩溃时,请发布logcat堆栈跟踪作为习惯问题。我打赌你的人会显示一个&#34;空指针异常&#34;。