如何从DialogFragment获取PackageInfo

时间:2014-08-07 19:02:21

标签: android android-dialogfragment

我正在尝试将包信息放入DialogFragment以使用OnClickListener中的一些数据,但我遇到了一些麻烦。获取“无法解析getPackageName()”

以下行是我在this.getPackageName()

遇到问题的地方
final PackageInfo packageInfo = getActivity().getPackageManager().getPackageInfo(this.getPackageName(), PackageManager.GET_ACTIVITIES);

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {


        final String packageName = getArguments().getString("packageName");

        final PackageInfo packageInfo = getActivity().getPackageManager().getPackageInfo(this.getPackageName(), PackageManager.GET_ACTIVITIES);

        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        final long lastVersionCode = prefs.getLong(LAST_VERSION_CODE_KEY, 0);
        final String title = this.getString(R.string.app_name) + " v" + packageInfo.versionName;

        AlertDialog.Builder b=  new  AlertDialog.Builder(getActivity())
                .setTitle(title)
                .setPositiveButton("Got it!",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {

                                // Mark this version as read
                                SharedPreferences.Editor editor = prefs.edit();
                                editor.putLong(LAST_VERSION_CODE_KEY, packageInfo.versionCode);
                                editor.commit();
                                dismiss();
                            }
                        }
                );

        LayoutInflater i = getActivity().getLayoutInflater();

        View v = i.inflate(R.layout.whatsnew,null);

        b.setView(v);
        return b.create();
    }

1 个答案:

答案 0 :(得分:1)

我想你想要:

getActivity().getPackageName()

而不是

this.getPackageName()