简体   繁体   English

当我单击按钮时,我的PopUpWindow没有打开

[英]My PopUpWindow doesn't open when I click on the button

Basically, the button I click is for a store, and I'd like it to cause a PopUpWindow to appear from which you can buy upgrades (much like the Cookie Clicker menu). 基本上,我单击的按钮用于商店,并且我希望它导致出现一个PopUpWindow,您可以从中购买升级(非常类似于Cookie Clicker菜单)。 However, when I click the button, the app crashes. 但是,当我单击按钮时,应用程序崩溃。 Someone's similar post said that the error produced was as a result of the contentView not being set, so I set this, but it only works if I set the contentView to the very layout that I'm using as a popup, which means I have a popup, but not over the original, main layout. 有人的类似帖子说,产生此错误是由于未设置contentView所致,所以我对此进行了设置,但仅当将contentView设置为我用作弹出窗口的布局时,此方法才有效弹出式窗口,但不在原始主布局上。

My Java code: 我的Java代码:

package com.bipbapapps.leagueclickerapp;


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainClass extends Activity implements OnClickListener {


public float goldCount;
Button minionClick;
Button storeClick;
TextView textGoldCount;
String textTotal;
private SharedPreferences prefs;

@Override
public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Set full-screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.mainlayout);

    prefs = getSharedPreferences("LeagueClicker", Context.MODE_PRIVATE);


    goldCount = prefs.getFloat("goldCount", 0.0f);

    //Linking the variables
    minionClick = (Button) findViewById(R.id.minioncentreid);
    storeClick = (Button) findViewById(R.id.storeimageid);
    textGoldCount = (TextView) findViewById(R.id.textviewtop);

    //String which will display at the top of the app
    textTotal = goldCount + " Gold";

    //Setting TextView to the String
    textGoldCount.setText(textTotal);
    textGoldCount.setGravity(Gravity.CENTER);
    Typeface tf = Typeface.createFromAsset(getAssets(), "mechanical.ttf");
    textGoldCount.setTypeface(tf);
    textGoldCount.setTextSize(35);

    //Setting onClickListener
    minionClick.setOnClickListener(this);
    storeClick.setOnClickListener(this);

}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.minioncentreid:
    goldCount += 1.0;
    prefs.edit().putFloat("goldCount", goldCount).commit();
    textTotal = goldCount + " Gold";
    textGoldCount.setText(textTotal);
    textGoldCount.setGravity(Gravity.CENTER);
    break;

    case R.id.storeimageid:
        LayoutInflater inflater = (LayoutInflater)
           this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        PopupWindow pw = new PopupWindow(
           inflater.inflate(R.layout.storemenu, null, false), 
           100, 
           100, 
           true);
        pw.showAtLocation(this.findViewById(R.id.storemenuid), Gravity.CENTER, 0, 0);

    }


}


@Override
public void onPause(){
    super.onPause();
    prefs.edit().putFloat("goldCount", goldCount).commit();
}

@Override
public void onResume(){
    super.onResume();
    goldCount = prefs.getFloat("goldCount", 0.0f);
}

@Override
public void onStop(){
    super.onStop();
    prefs.edit().putFloat("goldCount", goldCount).commit();
    Log.d(prefs.getFloat("goldCount", 0.0f)+"derprolw", "ejwfjbrea");
}




}

LogCat errors: LogCat错误:

04-22 06:24:52.408: D/gralloc_goldfish(2931): Emulator without GPU emulation detected.
04-22 06:24:57.808: D/dalvikvm(2931): GC_FOR_ALLOC freed 2121K, 10% free 20151K/22340K, paused 111ms, total 114ms
04-22 06:24:58.368: D/AndroidRuntime(2931): Shutting down VM
04-22 06:24:58.368: W/dalvikvm(2931): threadid=1: thread exiting with uncaught exception (group=0xb2a1cba8)
04-22 06:24:58.418: E/AndroidRuntime(2931): FATAL EXCEPTION: main
04-22 06:24:58.418: E/AndroidRuntime(2931): Process: com.bipbapapps.leagueclickerapp, PID: 2931
04-22 06:24:58.418: E/AndroidRuntime(2931): java.lang.NullPointerException
04-22 06:24:58.418: E/AndroidRuntime(2931):     at android.widget.PopupWindow.showAtLocation(PopupWindow.java:814)
04-22 06:24:58.418: E/AndroidRuntime(2931):     at com.bipbapapps.leagueclickerapp.MainClass.onClick(MainClass.java:86)
04-22 06:24:58.418: E/AndroidRuntime(2931):     at android.view.View.performClick(View.java:4438)
04-22 06:24:58.418: E/AndroidRuntime(2931):     at android.view.View$PerformClick.run(View.java:18422)
04-22 06:24:58.418: E/AndroidRuntime(2931):     at android.os.Handler.handleCallback(Handler.java:733)
04-22 06:24:58.418: E/AndroidRuntime(2931):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-22 06:24:58.418: E/AndroidRuntime(2931):     at android.os.Looper.loop(Looper.java:136)
04-22 06:24:58.418: E/AndroidRuntime(2931):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-22 06:24:58.418: E/AndroidRuntime(2931):     at java.lang.reflect.Method.invokeNative(Native Method)
04-22 06:24:58.418: E/AndroidRuntime(2931):     at java.lang.reflect.Method.invoke(Method.java:515)
04-22 06:24:58.418: E/AndroidRuntime(2931):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-22 06:24:58.418: E/AndroidRuntime(2931):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-22 06:24:58.418: E/AndroidRuntime(2931):     at dalvik.system.NativeStart.main(Native Method)
04-22 06:25:01.358: I/Process(2931): Sending signal. PID: 2931 SIG: 9

Does anyone know how I may fix this issue? 有谁知道我该如何解决这个问题?

Looks to me like you have a null pointer at this line (line 86?) : 在我看来,您在此行(第86行?)具有空指针:

pw.showAtLocation(this.findViewById(R.id.storemenuid), Gravity.CENTER, 0, 0);

Try replacing this.findViewById(R.id.storemenuid) with v, as technically they are the same except we know that v is not null or else this line would not be reached. 尝试用v替换this.findViewById(R.id.storemenuid),因为从技术上讲,它们是相同的,除了我们知道v不为null否则将无法到达该行。 Also the other values are constant so we know they are not null either. 另外,其他值也是常量,因此我们知道它们也不为空。

let me know if this helps, or if it provides you with any more information. 让我知道这是否有帮助,或者是否为您提供了更多信息。


edit 编辑

Also other cause could be in the constructor: 还有其他原因可能在构造函数中:

 PopupWindow pw = new PopupWindow(
       inflater.inflate(R.layout.storemenu, null, false), 
       100, 
       100, 
       true);

But I find this less likely as it should have notified you of the null pointer referring to that line. 但是我发现这种可能性较小,因为它应该已通知您有关该行的空指针。

尝试执行以下操作:

PopupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM