タイトルはそれをかなりよく説明しています。PopupWindow が開き、ゲームのメニューとして使用されます。ただし、ボタンを使用してメニューを閉じようとすると、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);」の代わりに。唯一の問題は、ボタンが実際にメニューを閉じないことです:(