基本上,我点击的按钮是针对商店的,我希望它能够显示一个PopUpWindow,你可以从中购买升级版(很像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
有谁知道如何解决这个问题?
答案 0 :(得分:0)
在我看来你在这一行有一个空指针(第86行?):
pw.showAtLocation(this.findViewById(R.id.storemenuid), Gravity.CENTER, 0, 0);
尝试用v替换this.findViewById(R.id.storemenuid),因为从技术上来说它们是相同的,除非我们知道v不是null或者否则不会到达此行。其他值也是常数,所以我们知道它们也不是空的。
告诉我这是否有帮助,或者它是否为您提供了更多信息。
修改强>
其他原因也可能在构造函数中:
PopupWindow pw = new PopupWindow(
inflater.inflate(R.layout.storemenu, null, false),
100,
100,
true);
但我发现这不太可能,因为它应该通知你指向该行的空指针。
答案 1 :(得分:0)
尝试按以下步骤操作:
PopupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);