使用getApplicationContext()时出现NullPointerException

时间:2016-01-30 00:31:28

标签: java android

我正在编写我的第一个Android应用程序,但我仍然无法处理此错误:

01-29 23:28:06.503 2135-2135/com.example.centrum.doradcazakupuv1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.centrum.doradcazakupuv1, PID: 2135
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.centrum.doradcazakupuv1/com.example.centrum.doradcazakupuv1.StronaGlowna}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
    at com.example.centrum.doradcazakupuv1.StronaGlowna.<init>(StronaGlowna.java:63)
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我的代码是:

package com.example.centrum.doradcazakupuv1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;


public class StronaGlowna extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_strona_glowna);
    }

    boolean cSoja = false;
    boolean cOA = false;
    boolean cWege = false;
    boolean cFullWege = false;
    IntentResult wr;

    String wynik;


    public void onCheckBoxClicked(View view) {
        boolean checked = ((CheckBox) view).isChecked();
        switch (view.getId()) {
            case R.id.soja:
                if (checked) {
                    cSoja = true;
                } else {
                    cSoja = false;
                }
                break;
            case R.id.oa:
                if (checked) {
                    cOA = true;
                } else {
                    cOA = false;
                }
                break;
            case R.id.wege:
                if (checked) {
                    cWege = true;
                } else {
                    cWege = false;
                }
                break;
            case R.id.fullWege:
                if (checked) {
                    cFullWege = true;
                } else {
                    cFullWege = false;
                }
                break;
        }
    }

    public int bts(boolean p) {
        if (p) return 1;
        else return 0;
    }


    public class ScanActivity extends Activity {
        public void onActivityResult(int requestCode, int resultCode, Intent intent) {
            IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
            wr = scanningResult;
        }

        protected void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.activity_strona_glowna);
            final Button button = (Button) findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // moja akcja;
                    IntentIntegrator Skaner = new IntentIntegrator(new ScanActivity());
                    Skaner.initiateScan();
                    wynik = wr.getContents();
                    int[] tablica = new int[4];
                    for (int i = 0; i < 4; i++) {
                        tablica[i] = wynik.charAt(i) - 48;
                    }
                    boolean fTest = true;
                    if (tablica[0] != bts(cSoja)) fTest = false;
                    else if (tablica[1] != bts(cOA)) fTest = false;
                    else if (tablica[2] != bts(cWege)) fTest = false;
                    else if (tablica[3] != bts(cFullWege)) fTest = false;
                    final Toast fail = Toast.makeText(getApplicationContext(), "Ten produkt nie odpowiada twoim preferencjom", Toast.LENGTH_SHORT);
                    final Toast good = Toast.makeText(getApplicationContext(), "Możesz to zjeść", Toast.LENGTH_SHORT);
                    if (fTest) good.show();
                    else fail.show();

                    // koniec akcji
                }
            });
        }
    }
}

一般来说,是否还有解决此类问题的提示?

2 个答案:

答案 0 :(得分:0)

变化:

final Toast fail = Toast.makeText(getApplicationContext(), "Ten produkt nie odpowiada twoim preferencjom", Toast.LENGTH_SHORT);
final Toast good = Toast.makeText(getApplicationContext(), "Możesz to zjeść", Toast.LENGTH_SHORT);

final Toast fail = Toast.makeText(ScanActivity.this, "Ten produkt nie odpowiada twoim preferencjom", Toast.LENGTH_SHORT);
final Toast good = Toast.makeText(ScanActivity.this, "Możesz to zjeść", Toast.LENGTH_SHORT);

并避免使用扩展Activity的嵌套类。

答案 1 :(得分:0)

Damian Kozlak说的正是如此。将getApplicationContext()更改为ScanActivity.this

为什么呢?因为当您调用getApplicationContext()时,您正在调用StronaGlowna Activity的上下文而不是ScanActivity上下文。

是的,尽量避免使用扩展Activity的嵌套类。这不是一个好的做法,因为它可能会造成冲突并导致问题