微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试在空对象引用上调用虚拟方法“android.content.Context android.view.View.getContext()”

如何解决尝试在空对象引用上调用虚拟方法“android.content.Context android.view.View.getContext()”

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);

    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();
    }
  }
}

我的 NewHomeFragment 包含:

    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);

我的堆栈跟踪:

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.fpt.mypackage.view.PopUpClass}:java.lang.NullPointerException:尝试调用虚拟方法 'android.content.Context android.view.View.getContext ()' 在空对象引用上 在 android.app.ActivityThread.performlaunchActivity(ActivityThread.java:3431) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:223) 在 android.app.ActivityThread.main(ActivityThread.java:7660) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 引起:java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.View.getContext()' on a null object reference 在 com.fpt.mywirelesscamera.view.PopUpClass.onCreate(PopUpClass.java:39) 在 android.app.Activity.performCreate(Activity.java:8000) 在 android.app.Activity.performCreate(Activity.java:7984) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 在 android.app.ActivityThread.performlaunchActivity(ActivityThread.java:3404) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:223) 在 android.app.ActivityThread.main(ActivityThread.java:7660) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

解决方法

你想用那个视图做什么。如果您调试代码,您会发现视图对象为空。

,

那是因为您尝试在不存在的对象上调用 getContext() 方法,如果它是 params = {'batch_size' : 8,'shuffle' : True,'num_workers': 0,'drop_last' : True } dataset = ChemicalFractions() train_dataloader = DataLoader(dataset,**params) 此处视图尚不存在:

view View

在 Activity 中,您可以改用它:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。