gpt4 book ai didi

java - 当我单击按钮时,我的 PopUpWindow 未打开

转载 作者:行者123 更新时间:2023-12-02 09:49:10 25 4
gpt4 key购买 nike

基本上,我单击的按钮是针对商店的,我希望它会出现一个弹出窗口,您可以从中购买升级(很像 Cookie Clicker 菜单)。但是,当我单击该按钮时,应用程序崩溃了。有人类似的帖子说产生的错误是由于未设置 contentView 的结果,所以我设置了这个,但只有当我将 contentView 设置为我用作弹出窗口的布局时它才有效,这意味着我有弹出窗口,但不在原始主布局之上。

我的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 错误:

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

有谁知道我该如何解决这个问题?

最佳答案

在我看来,这一行(第 86 行?)有一个空指针:

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

尝试用 v 替换 this.findViewById(R.id.storemenuid) ,因为从技术上讲,它们是相同的,除非我们知道 v 不为空,否则将无法到达此行。另外,其他值也是恒定的,因此我们知道它们也不为空。

请告诉我这是否有帮助,或者是否为您提供了更多信息。

<小时/>

编辑

其他原因也可能在构造函数中:

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

但我发现这种情况不太可能发生,因为它应该通知您引用该行的空指针。

关于java - 当我单击按钮时,我的 PopUpWindow 未打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23220568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com