错误:尝试在空对象引用上调用虚拟方法...

时间:2019-06-26 01:30:27

标签: java android xml

尽管已经多次问过这个问题,但没有一个解决方案对我有用。我突然突然收到此错误,我想我可能会遇到与其他人不同的问题。

这是负责的班级:


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

预先感谢,我希望答案不会只是我犯的一个愚蠢的错误。

3 个答案:

答案 0 :(得分:1)

很难从您的代码片段中看出来,但是PriceCalculator似乎是从AppCompatActivity扩展而来的。您永远不要在活动中使用新运算符。这样做将导致活动设置的上下文不正确,并且您将无法使用诸如findViewById()之类的方法。我不确定MenuActivity为什么在其中包含另一个活动的实例。这也是一个坏主意,活动永远不应相互引用,而应通过意图进行交流。解决这两个问题应该可以使事情更好地工作。

答案 1 :(得分:0)

我认为TextView tv1 = findViewById(R.id.textView6);应该在onCreate方法中...

尝试将它们放在onCreate方法中吗?

答案 2 :(得分:0)

只需将所有标识放入Activity onCreate函数中即可。意思是说

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_car);
    TextView tv1 = findViewById(R.id.textView6);
    TextView tv2 = findViewById(R.id.textView5);
    TextView tv3 = findViewById(R.id.textView3);
}