java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法”

时间:2019-05-08 03:20:10

标签: java android popup

我想/尝试在首次启动的应用程序上显示弹出窗口,但出现此错误

  

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.PopupWindow.showAtLocation(android.view.View,int,int,int)'

我进行叠加活动,以在首次启动时使用Popup启动应用程序。

MainActivity.java

public class MainActivity extends AppCompatActivity implements NetworkStateReceiver.NetworkStateReceiverListener {

    //PopUp
    Context popupContext;
    Activity popupActivity;
    PopupWindow popupWindow;
    RelativeLayout popupLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        popupWindow.showAtLocation(popupLayout, Gravity.CENTER, 0, 0);

        (***)

        //Start PopUp
        popupContext = getApplicationContext();
        popupActivity =  MainActivity.this;
        LayoutInflater inflater = (LayoutInflater) popupContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.activity_start, null);
        popupWindow = new PopupWindow(popupView,
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        if (Build.VERSION.SDK_INT >= 21){
            popupWindow.setElevation(5.0f);
        }
    }
}

错误消息

05-08 09:48:18.612 7398-7398/id.nax.androidstate E/AndroidRuntime: FATAL EXCEPTION: main
    Process: id.nax.androidstate, PID: 7398
    java.lang.RuntimeException: Unable to start activity ComponentInfo{id.nax.androidstate/id.nax.androidstate.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.PopupWindow.showAtLocation(android.view.View, int, int, int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
        at android.app.ActivityThread.access$1100(ActivityThread.java:229)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7406)
        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 'void android.widget.PopupWindow.showAtLocation(android.view.View, int, int, int)' on a null object reference
        at id.nax.androidstate.MainActivity.onCreate(MainActivity.java:53)
        at android.app.Activity.performCreate(Activity.java:6904)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:7406) 
        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) 

是否有解决我代码的建议或制作弹出窗口的更好方法?

3 个答案:

答案 0 :(得分:1)

您尝试在实际创建showAtLocation(...)之前致电PopupWindow

    popupWindow.showAtLocation(popupLayout, Gravity.CENTER, 0, 0);

    (***)

    //Start PopUp
    popupContext = getApplicationContext();
    popupActivity =  MainActivity.this;
    LayoutInflater inflater = (LayoutInflater) popupContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.activity_start, null);
    popupWindow = new PopupWindow(popupView,
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    if (Build.VERSION.SDK_INT >= 21){
        popupWindow.setElevation(5.0f);
    }

您可以改为这样做,我还对代码进行了重构,以使您的代码更简洁。

    // The View you're making the popup in regards to.
    View parent = findViewById(R.id....);

    View popupView = LayoutInflater.from(this).inflate(R.layout.activity_start, null);
    popupWindow = new PopupWindow(popupView,
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);

    if (Build.VERSION.SDK_INT >= 21){
        popupWindow.setElevation(5.0f);
    }
    popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);

答案 1 :(得分:0)

在空对象上调用此方法,需要在创建popupwindow之后而不是之前调用它。 将呼叫置于行后: popupWindow = new PopupWindow(popupView,                 RelativeLayout.LayoutParams.MATCH_PARENT,                 RelativeLayout.LayoutParams.MATCH_PARENT);

答案 2 :(得分:0)

解决方案::假设这是您的主要活动布局。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"/>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private PopupWindow popupWindow;

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

        final View activityRootView = findViewById(R.id.rootView);
        activityRootView.post(new Runnable() {

            @Override
            public void run() {
                LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = inflater.inflate(R.layout.activity_start, null);

                popupWindow = new PopupWindow(popupView,
                        RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.MATCH_PARENT);

                if (Build.VERSION.SDK_INT >= 21) {
                    popupWindow.setElevation(5.0f);
                }

                popupWindow.showAtLocation(activityRootView, Gravity.CENTER, 0, 0);
            }
        });
    }
}