customPhoneStateListener的startactivity重复活动intent.getextras()NullPointerException

时间:2013-07-08 13:17:10

标签: android nullpointerexception android-activity phone-state-listener

public void onCallStateChanged(int state, String incomingNumber) {

    super.onCallStateChanged(state, incomingNumber);

    switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
        // when Idle i.e no call
        if (lastCallState == TelephonyManager.CALL_STATE_OFFHOOK
                || lastCallState == TelephonyManager.CALL_STATE_RINGING) {
            lastCallState = TelephonyManager.CALL_STATE_IDLE;
                            CallsMainFragment.first=true;
            Intent intent = context.getPackageManager()
                    .getLaunchIntentForPackage("com.tabactionbar");
            Bundle b=new Bundle();
            b.putString("TAB", "2");
            intent.putExtras(b);
            intent.putExtra("key", "2");
            context.startActivity(intent);
        }

        lastCallState = TelephonyManager.CALL_STATE_IDLE;
        break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
        lastCallState = TelephonyManager.CALL_STATE_OFFHOOK;
        break;
    case TelephonyManager.CALL_STATE_RINGING:
        lastCallState = TelephonyManager.CALL_STATE_RINGING;
        break;

    default:
        break;
    }

}

那是CustomPhone状态侦听器,在调用之后需要在com.tabactionbar中启动我的活动,这是一个主要的活动,但是

  • 不能像Intent intent=new Intent(getApplicationContex(),MainActivity.class);那样打电话我真不知道为什么?
  • 如果我的活动开始,那就重复那个

任何解决方案?

这是我想要的额外内容

try {
        Bundle bundle=getIntent().getExtras();
        if (bundle.isEmpty()){
            Log.e("Bundle", "eMPTY");
        }
        else {
            Log.e("Bundle", bundle.getInt("TAB")+" ... ");
            selectedTab=getIntent().getExtras().getInt("TAB");
            Log.e("Eroor", " don`t contains key");
        }

    } catch (Exception e1) {
        // TODO Auto-generated catch block
        Log.e(e1.getLocalizedMessage()+""+e1.toString(), "Eroor intent");
    }

    try {
        getActionBar().setSelectedNavigationItem(selectedTab);
    } catch (Exception e) {
        Log.e(e.getLocalizedMessage()+"", "Error tab 2");
        // TODO: handle exception
    }

捕获NullPointerException,因为我的包是空的,但为什么?

0 个答案:

没有答案