NullPointerException in Android App on creating new Intent
Hi,
The NullPointerException occurs at the line :
    Intent callActivityIntent = new Intent(getActivity(), CallActivity.class);
of BasePreCallSegment.java file.
I have the following CallActivity.java file :
package kujo.app.ui.activecall;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentTransaction;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.WindowManager;import java.util.ArrayList;import java.util.Arrays;import co.kujo.contact.ContactService;import kujo.app.AuthManager;import kujo.app.BuildConfig;import kujo.app.CallManager;import kujo.app.CallState;import kujo.app.ICallStateListener;import kujo.app.PSTNCallRecordsHelper;import kujo.app.PSTNCallStateEventPublisher;import kujo.app.R;import kujo.app.SoundManager;import kujo.app.application.VoipApplication;import kujo.app.ui.BaseLoggedInActivity;import kujo.app.ui.home.ContactItem;import kujo.onboarding.UserProfile;import to.talk.error.ErrorReporter;import to.talk.logging.Logger;import to.talk.logging.LoggerFactory;public class CallActivity extends BaseLoggedInActivity { private static final String CURRENT_FRAGMENT_NAME = "current_fragment_name"; public static final String EXTRA_CALL_STATE = "extra_call_state"; private volatile ArrayList<CallItem> _currentCallList = new ArrayList<CallItem>(); //we use array list to pass in bundle and avoid type-cast private CallManager _callManager; private ICallStateListener _callListener; public static String EXTRA_CALL = "extra_call"; public static String EXTRA_CALL_LIST = "extra_call_list"; private Logger _logger = LoggerFactory.getTrimmer(CallActivity.class.getSimpleName()); private volatile boolean _isCallStatsFragmentOpened = false; private volatile String _currentCallFragmentName = null; private boolean _isSavedBundleInOnCreateNull = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); setContentView(R.layout.active_call_activity); _callManager = CallManager.getInstance(); _isSavedBundleInOnCreateNull = (savedInstanceState == null); if (savedInstanceState == null) //activity first time { CallItem callItem = getIntent().getParcelableExtra(EXTRA_CALL); _currentCallList.add(callItem); UserProfile userProfile = VoipApplication.getOnboardingService().getUserProfile(); ContactItem contact = callItem.getContact(); AuthManager.AuthParams params = AuthManager.generateAuthParams(userProfile.getGuid(), userProfile.getToken(), contact.getPhoneNumber()); callItem.setUniqueCallIdentifier(params.getCallId()); _callManager.call(callItem, params); SoundManager.getInstance().setIsSpeakerEnabled(false); SoundManager.getInstance().setIsMuteEnabled(false, false); ContactService.getInstance().addToRecents(contact.getPhoneNumber()); _currentCallFragmentName = getRelevantFragmentClassName(_currentCallList); Fragment fragment = getFragmentInstance(_currentCallFragmentName, _currentCallList); getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fragment).commit(); } else { _currentCallList = savedInstanceState.getParcelableArrayList(EXTRA_CALL_LIST); _currentCallFragmentName = savedInstanceState.getString(CURRENT_FRAGMENT_NAME); } _logger.debug("on create, currentCall List: {}, current fragment : {}", _currentCallList, _currentCallFragmentName); _callListener = getCallListener(); } private Fragment getFragmentInstance(String fragmentClassName, ArrayList<CallItem> callList) { Bundle bundle = getBundleWithCallList(callList); return Fragment.instantiate(CallActivity.this, fragmentClassName, bundle); } private Bundle getBundleWithCallList(ArrayList<CallItem> callList) { Bundle bundle = new Bundle(); bundle.putParcelableArrayList(EXTRA_CALL, callList); CallItem callItem = callList.get(0); String uniqueIdentifier = callItem.getUniqueIdentifier(); CallState callState = _callManager.getCallState(uniqueIdentifier); if (callState == null) { throw new NullPointerException("call state is null, isSavedBundleNullInOnCreate" + _isSavedBundleInOnCreateNull); } _logger.debug("call state name in bundle: {}", callState.name()); bundle.putString(EXTRA_CALL_STATE, callState.name()); return bundle; } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (outState == null) { outState = new Bundle(); } outState.putParcelableArrayList(EXTRA_CALL_LIST, _currentCallList); outState.putString(CURRENT_FRAGMENT_NAME, _currentCallFragmentName); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.active_call_activity_menu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onPrepareOptionsMenu(Menu menu) { boolean isVisible = !BuildConfig.BUILD_TYPE.equals("release") && isAnyCallConnected(_currentCallList) && !_isCallStatsFragmentOpened; menu.findItem(R.id.call_stats).setVisible(isVisible); return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int itemId = item.getItemId(); switch (itemId) { case android.R.id.home: onBackPressed(); return true; case R.id.call_stats: _isCallStatsFragmentOpened = true; setAppropriateFragment(_currentCallList); return true; default: return super.onOptionsItemSelected(item); } } private void setAppropriateFragmentOnUIThread(final ArrayList<CallItem> callList) { runOnUiThread(new Runnable() { @Override public void run() { setAppropriateFragment(callList); } }); } private void setAppropriateFragment(ArrayList<CallItem> callList) { String fragmentClassName = getRelevantFragmentClassName(callList); _logger.debug("appropriate class name: {}", fragmentClassName); if (!_currentCallFragmentName.equals(fragmentClassName)) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); Fragment fragment = getFragmentInstance(fragmentClassName, callList); transaction.replace(R.id.fragment_container, fragment); transaction.commitAllowingStateLoss();// getActionBar().setDisplayHomeAsUpEnabled(_isCallStatsFragmentOpened); invalidateOptionsMenu(); _currentCallFragmentName = fragmentClassName; } else { _logger.debug("refresh for instance class: {}", fragmentClassName); Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); if (fragment == null) { _logger.debug("fragment fetched is null.. should not happen!"); } else { Bundle bundle = getBundleWithCallList(callList); ((BaseCallFragment) fragment).refresh(bundle); } } } private String getRelevantFragmentClassName(ArrayList<CallItem> callList) { boolean isAnyCallConnected = isAnyCallConnected(callList); boolean isEveryCallCompleted = isEveryCallCompleted(callList); boolean anyCallHangRequested = isAnyCallHangRequested(callList); Class fragmentClass; if (isAnyCallConnected) { fragmentClass = _isCallStatsFragmentOpened ? CallStatsFragment.class : ActiveCallFragment.class; } else if (anyCallHangRequested) { fragmentClass = ActiveCallFragment.class; } else if (isEveryCallCompleted) { fragmentClass = PostCallFragment.class; } else { CallItem call = _currentCallList.get(0); boolean isIncomingCall = call.isIncomingCall(); fragmentClass = isIncomingCall ? IncomingCallFragment.class : OutgoingCallFragment.class; } return fragmentClass.getName(); } private boolean isAnyCallHangRequested(ArrayList<CallItem> callList) { for (CallItem callItem : callList) { boolean status = _callManager.isCallHangRequested(callItem.getUniqueIdentifier()); if (status) { return true; } } return false; } private boolean isEveryCallCompleted(ArrayList<CallItem> callList) { for (CallItem call : callList) { boolean status = _callManager.isCallCompleted(call.getUniqueIdentifier()); if (!status) { return false; } } return true; } private boolean isAnyCallConnected(ArrayList<CallItem> callList) { for (CallItem call : callList) { boolean status = _callManager.isCallConnected(call.getUniqueIdentifier()); if (status) { return true; } } return false; } private ICallStateListener getCallListener() { return new ICallStateListener() { @Override public void onCallAdded(CallItem callItem) { } @Override public void onCallStateChange(final CallItem call, final CallState callState) { runOnUiThread(new Runnable() { @Override public void run() { _logger.debug("new call: " + call + " callState: " + callState + " prev call list: " + _currentCallList); if (_currentCallList.contains(call)) { setAppropriateFragmentOnUIThread(_currentCallList); } else if (Arrays.asList(CallState.CONNECTING, CallState.EARLY).contains(callState) && isEveryCallCompleted(_currentCallList)) { _currentCallList = new ArrayList<CallItem>(); _currentCallList.add(call); setAppropriateFragmentOnUIThread(_currentCallList); } else if (callState.equals(CallState.COMPLETED)) { _currentCallList.remove(call); setAppropriateFragmentOnUIThread(_currentCallList); } else if (call.isIncomingCall()) { //todo: ui refresh for call waiting //_sipsdk.callQueued(call); } } }); } @Override public void onCallRemoved(CallItem callItem) { } }; } @Override protected void onResume() { super.onResume(); if (PSTNCallRecordsHelper.getInstance().isAnyPSTNCallActive()) { hangAllCalls(); finish(); } else { CallManager.getInstance().addListener(_callListener); setAppropriateFragment(_currentCallList); } } @Override protected void onPause() { super.onPause(); CallManager.getInstance().removeListener(_callListener); } @Override public void onBackPressed() { if (isEveryCallCompleted(_currentCallList)) { finish(); } else if (_isCallStatsFragmentOpened) { _isCallStatsFragmentOpened = false; setAppropriateFragment(_currentCallList); } } public void onEvent(PSTNCallStateEventPublisher.PhoneIncomingCallEvent event) { finish(); } private void hangAllCalls() { _callManager.hangUpAllcalls(); }}
Please help me find out the reason for this exception.
Thanks
AndroidJava
Last Comment
Rohit Bajaj
8/22/2022 - Mon
Rohit Bajaj
ASKER
Hi,
Following is my current Analysis :
The NullPointerException is occuring at line :
android.content.ComponentName.<init>(ComponentName.java:75)
I checked out source for ComponentName.java . The line corresponds to the following -
public ComponentName(Context pkg, Class<?> cls) {
    mPackage = pkg.getPackageName();
    mClass = cls.getName();  -- -line No. 75
  }
Here the line on which the Exception is being thrown is : mClass = cls.getName();
But that means the CallActivity.class is being passed Null in the following function :
 private void callContact(ContactItem contactItem) {
    Intent callActivityIntent = new Intent(getActivity(), CallActivity.class);
    CallItem callItem = new CallItem(contactItem, false);
    callActivityIntent.putExtra(CallActivity.EXTRA_CALL, callItem);
    startActivity(callActivityIntent);
  }
How could this be so?? Why its being passed as NULL.
CallActivity.java is defined in the package package kujo.app.ui.activecall
BasePreCallFragment.java is in package kujo.app.ui
Can this be causing any issue ?
Following is my AndroidManifest.xml file which is inside app folder :
Hi
But its a fragment and getactivity() returns the   activity of the current fragment. I think we cant use 'this' when accessing the current activity in a fragment
Following is my current Analysis :
The NullPointerException is occuring at line :
android.content.ComponentN
I checked out source for ComponentName.java . The line corresponds to the following -
public ComponentName(Context pkg, Class<?> cls) {
    mPackage = pkg.getPackageName();
    mClass = cls.getName();  -- -line No. 75
  }
Here the line on which the Exception is being thrown is : mClass = cls.getName();
But that means the CallActivity.class is being passed Null in the following function :
 private void callContact(ContactItem contactItem) {
    Intent callActivityIntent = new Intent(getActivity(), CallActivity.class);
    CallItem callItem = new CallItem(contactItem, false);
    callActivityIntent.putExtr
    startActivity(callActivity
  }
How could this be so?? Why its being passed as NULL.
CallActivity.java is defined in the package package kujo.app.ui.activecall
BasePreCallFragment.java is in package kujo.app.ui
Can this be causing any issue ?
Following is my AndroidManifest.xml file which is inside app folder :
Open in new window