基本上,我单击的按钮是针对商店的,我希望它会出现一个 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
有谁知道我可以如何解决这个问题?