Search code examples
androidviewpopupandroid-contextruntimeexception

Attempt to invoke virtual method 'android.content.Context android.view.View.getContext()' on a null object reference


public class PopUpClass extends AppCompatActivity implements IAsyncResponse {

View view;
private String agreementContent;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Log.e("Before view inflated","popupclass");
    agreementContent = getIntent().getStringExtra("AgreementContent");
    Log.e("pop up before error",agreementContent);
    LayoutInflater inflater = (LayoutInflater) 
    view.getContext().getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.activity_agreement_consent_popup, 
    null);

    ScrollView sView = popupView.findViewById(R.id.sView);
    WebView agreementText = popupView.findViewById(R.id.agreement_textView);
    Button buttonEdit = popupView.findViewById(R.id.btn);

    //agreementContent = getIntent().getStringExtra("AgreementContent");
    
    int width = LinearLayout.LayoutParams.MATCH_PARENT;
    int height = LinearLayout.LayoutParams.MATCH_PARENT;

    boolean focusable = true;
    final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
    popupWindow.showAtLocation(view, Gravity.FILL, 0, 0);

    sView.setVerticalScrollBarEnabled(true);
    sView.setHorizontalScrollBarEnabled(false);

    agreementText.loadDataWithBaseURL(null, agreementContent, "text/html", "UTF-8", null);

    buttonEdit.setOnClickListener(v -> {

        WirelessAPI_Task task = new WirelessAPI_Task(this);
        task.callback = this;
        task.execute(WirelessAPI_Task.INSERT_USER_CONSENT);
    });
    popupView.setOnTouchListener((v, event) -> true);
}


  @Override
  public void processFinish(int request, int result, String data) {
    try{
        JSONObject json = new JSONObject(data);
        Log.e("process finish json",""+json);

    }catch (JSONException e) {
        e.printStackTrace();
    }
  }
}

My NewHomeFragment contains:

    JSONObject json = new JSONObject(data);
                agreementContent = json.getString("mobile_agreement_text");
                if(agreementContent != null){
                    PopUpClass popUpClass = new PopUpClass();
                    Log.e("before oncreate","new fragment");
                    Intent gIntent = new Intent(NewHomeFragment.this.getActivity(), 
    PopUpClass.class);
                    gIntent.putExtra("AgreementContent",agreementContent);
                    startActivity(gIntent);

My Stacktrace:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fpt.mypackage.view.PopUpClass}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.View.getContext()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3431) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7660) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.View.getContext()' on a null object reference at com.fpt.mywirelesscamera.view.PopUpClass.onCreate(PopUpClass.java:39) at android.app.Activity.performCreate(Activity.java:8000) at android.app.Activity.performCreate(Activity.java:7984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3404) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:223)  at android.app.ActivityThread.main(ActivityThread.java:7660)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 


Solution

  • That's because you try to call getContext() method on the object that is not exist in you case it's a view View here view has not existed yet:

    view.getContext().getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE);
    

    inside an activity, you can use this, instead:

    getSystemService(LAYOUT_INFLATER_SERVICE);