尝试在空对象引用上调用虚拟方法 'android.content.Context android.view.View.getContext()'
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();
}
}
}
我的 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:无法启动 activity 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(本机方法)
在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
由以下原因引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法 'android.content.Context android.view.View.getContext()'
在 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(本机方法)
在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
那是因为您尝试在您不存在的对象上调用 getContext() 方法,如果它是 view View
这里的视图还不存在:
view.getContext().getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE);
在 activity 中,您可以使用它来代替:
getSystemService(LAYOUT_INFLATER_SERVICE);
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();
}
}
}
我的 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:无法启动 activity 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(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 由以下原因引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法 'android.content.Context android.view.View.getContext()' 在 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(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
那是因为您尝试在您不存在的对象上调用 getContext() 方法,如果它是 view View
这里的视图还不存在:
view.getContext().getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE);
在 activity 中,您可以使用它来代替:
getSystemService(LAYOUT_INFLATER_SERVICE);