尽管这个问题已被问过多次,但没有一个解决方案对我有用。我总是突然收到此错误,我想我可能遇到了与其他人不同的问题。
这是负责的类:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car);
}
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
PageDownloader PDL = new PageDownloader();
TextView tv1 = findViewById(R.id.textView6);
TextView tv2 = findViewById(R.id.textView5);
TextView tv3 = findViewById(R.id.textView3);
public int Distance;
public int Mileage;
float GasPriceMid;
float GasPricePre;
float GasPriceReg;
public float Toll;
public float Parking;
String StringPriceReg;
String StringPriceMid;
String StringPricePre;
还有这个:
public class MenuActivity extends AppCompatActivity {
PriceCalculator PC = new PriceCalculator();
//PageDownloader PDL = new PageDownloader();
EditText et1 = findViewById(R.id.et1);
EditText et2 = findViewById(R.id.et2);
EditText et3 = findViewById(R.id.et3);
EditText et4 = findViewById(R.id.et4);
它只是应该创建对这些对象的引用,但它收到此错误:
Process: com.example.ojd.pricecalculatorapp, PID: 5067
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ojd.pricecalculatorapp/com.example.ojd.pricecalculatorapp.MenuActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3135)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3418)
at android.app.ActivityThread.access$1100(ActivityThread.java:231)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1823)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at com.example.ojd.pricecalculatorapp.PriceCalculator.<init>(PriceCalculator.java:19)
at com.example.ojd.pricecalculatorapp.MenuActivity.<init>(MenuActivity.java:9)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1096)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3125)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3418)
at android.app.ActivityThread.access$1100(ActivityThread.java:231)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1823)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
它说问题是 (PriceCalculator.java:19)
TextView tv1 = findViewById(R.id.textView6);
和(MenuActivity.java:9)
PriceCalculator PC = new PriceCalculator();
提前致谢,我希望答案不仅仅是我犯的一个愚蠢的错误。
最佳答案
很难从您的代码 fragment 中看出,但 PriceCalculator 似乎是从 AppCompatActivity 扩展的。您永远不应该在 Activity 中使用 new 运算符。这样做将导致 Activity 没有正确的上下文设置,并且您将无法使用 findViewById() 等方法。我不确定为什么 MenuActivity 内部有另一个 Activity 的实例。这也是一个坏主意, Activity 永远不应该相互引用,而应该通过 Intent 进行通信。解决这两个问题应该会让事情变得更好。
关于java - 空对象引用上的错误 : Attempt to invoke virtual method . ..,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56763971/