0

标题很好地描述了它;弹出窗口打开,并用作我的游戏的菜单。但是,当我尝试使用按钮关闭菜单时,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);”。现在唯一的问题是按钮实际上并没有关闭菜单:(

4

1 回答 1

0

我认为您不能在另一个案例块中使用在一个案例块中定义的变量。因为 case 块没有特定的执行顺序。更好的方法是,声明PopupWindow为类的成员变量。

public class MainClass extends Activity implements OnClickListener {

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

@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);

        if(pw == null) {
        pw = new PopupWindow(
           inflater.inflate(R.layout.storemenu, null, false), 
           300, 
           450, 
           true);
        }
        pw.showAtLocation(v, Gravity.CENTER, 0, 0);
         break;

    case R.id.menudismissid:
        if (pw != null) {
        pw.dismiss();
        }
        break;

    }

也不要忘记将break语句放在第一个案例块的末尾:)

编辑 1:PopUpWindow您可以访问by 的 视图元素

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

//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 = pw.getContentView().findViewById(R.id.menudismissid);

编辑2:

功能的官方文档dismiss

公共无效解雇()

在 API 级别 1 Dispose 中添加了弹出窗口。只有在 showAsDropDown(android.view.View) 执行后才能调用此方法。否则,调用此方法将无效。

所以你的案例 2 的代码应该是

case R.id.storeimageid:
        LayoutInflater inflater = (LayoutInflater)
           this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if(pw == null) {
        pw = new PopupWindow(
           inflater.inflate(R.layout.storemenu, null, false), 
           300, 
           450, 
           true);
        }
        pw.showAsDropDown(v, 0, 0, Gravity.CENTER);
         break;
于 2014-04-22T18:25:27.320 回答