标题很好地描述了它;弹出窗口打开,并用作我的游戏的菜单。但是,当我尝试使用按钮关闭菜单时,Eclipse 说“局部变量 pw 可能尚未初始化”:\
我的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.PopupWindow.OnDismissListener;
import android.widget.TextView;
public class MainClass extends Activity implements OnClickListener {
public float goldCount;
Button minionClick;
Button storeClick;
Button storeDismiss;
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);
storeDismiss = (Button) findViewById(R.id.menudismissid);
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);
storeDismiss.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),
300,
450,
true);
pw.showAtLocation(v, Gravity.CENTER, 0, 0);
case R.id.menudismissid:
pw.dismiss();
break;
}
}
@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");
}
}
有谁知道我可以如何设置按钮来关闭 PopupWindow?
编辑:现在产生的 LogCat 错误:
04-22 15:20:04.478: E/AndroidRuntime(3491): FATAL EXCEPTION: main
04-22 15:20:04.478: E/AndroidRuntime(3491): Process: com.bipbapapps.leagueclickerapp, PID: 3491
04-22 15:20:04.478: E/AndroidRuntime(3491): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bipbapapps.leagueclickerapp/com.bipbapapps.leagueclickerapp.MainClass}: java.lang.NullPointerException
04-22 15:20:04.478: E/AndroidRuntime(3491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-22 15:20:04.478: E/AndroidRuntime(3491): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-22 15:20:04.478: E/AndroidRuntime(3491): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-22 15:20:04.478: E/AndroidRuntime(3491): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-22 15:20:04.478: E/AndroidRuntime(3491): at android.os.Handler.dispatchMessage(Handler.java:102)
04-22 15:20:04.478: E/AndroidRuntime(3491): at android.os.Looper.loop(Looper.java:136)
04-22 15:20:04.478: E/AndroidRuntime(3491): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-22 15:20:04.478: E/AndroidRuntime(3491): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 15:20:04.478: E/AndroidRuntime(3491): at java.lang.reflect.Method.invoke(Method.java:515)
04-22 15:20:04.478: E/AndroidRuntime(3491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-22 15:20:04.478: E/AndroidRuntime(3491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-22 15:20:04.478: E/AndroidRuntime(3491): at dalvik.system.NativeStart.main(Native Method)
04-22 15:20:04.478: E/AndroidRuntime(3491): Caused by: java.lang.NullPointerException
04-22 15:20:04.478: E/AndroidRuntime(3491): at com.bipbapapps.leagueclickerapp.MainClass.onCreate(MainClass.java:66)
04-22 15:20:04.478: E/AndroidRuntime(3491): at android.app.Activity.performCreate(Activity.java:5231)
04-22 15:20:04.478: E/AndroidRuntime(3491): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-22 15:20:04.478: E/AndroidRuntime(3491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
编辑2:
因此,在一些帮助和编辑下,我们找到了代码:
//Initialize variables from popup window
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(
inflater.inflate(R.layout.storemenu, null, false));
storeDismiss = (Button) pw.getContentView().findViewById(R.id.menudismissid);
代替“storeDismiss = (Button) findViewById(R.id.menudismissid);”。现在唯一的问题是按钮实际上并没有关闭菜单:(