有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何解决R.string错误“无法解决或不是字段”

行中的错误:236 239和250 我没办法解决这个问题,是因为字符串R R.string函数我没有找到它。我想知道一些技巧和如何解决这个问题。我已经尝试过使用字符串函数,该函数接受强制播放服务,而不是获取太多

    package com.xxx.xxxx;

import Configuration.Configuration;
import 安卓.content.Intent;
import 安卓.graphics.Color;
import 安卓.net.Uri;
import 安卓.os.Bundle;
import 安卓.view.Gravity;
import 安卓.view.View;
import 安卓.view.ViewGroup.LayoutParams;
import 安卓.view.Window;
import 安卓.view.WindowManager;
import 安卓.widget.FrameLayout;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.安卓.AndroidApplication;
import com.badlogic.gdx.backends.安卓.AndroidApplicationConfiguration;
import com.gikdew.swingcopters.ActionResolver;
import com.gikdew.swingcopters.SwingCopters;
import com.google.安卓.gms.ads.AdListener;
import com.google.安卓.gms.ads.AdRequest;
import com.google.安卓.gms.ads.AdSize;
import com.google.安卓.gms.ads.AdView;
import com.google.安卓.gms.ads.InterstitialAd;
import com.google.安卓.gms.games.Games;
import com.google.example.games.basegameutils.GameHelper;
import com.google.example.games.basegameutils.GameHelper.GameHelperListener;

public class AndroidLauncher extends AndroidApplication implements
        ActionResolver, GameHelperListener {

    private static final String AD_UNIT_ID_BANNER = Configuration.AD_UNIT_ID_BANNER;
    private static final String AD_UNIT_ID_INTERSTITIAL = Configuration.AD_UNIT_ID_INTERSTITIAL;
    private static String GOOGLE_PLAY_URL = "https://play.google.com/store/apps/developer?id=";
    protected AdView adView;
    protected View gameView;
    private InterstitialAd interstitialAd;
    private GameHelper _gameHelper;
    private final static int REQUEST_CODE_UNUSED = 9002;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();

        cfg.useAccelerometer = false;
        cfg.useCompass = false;

        // Do the stuff that initialize() would do for you
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        GOOGLE_PLAY_URL += getPackageName();

        FrameLayout layout = new FrameLayout(this);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(params);

        AdView admobView = createAdView();

        View gameView = createGameView(cfg);
        layout.addView(gameView);
        layout.addView(admobView);
        _gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
        _gameHelper.enableDebugLog(false);

        GameHelperListener gameHelperListener = new GameHelper.GameHelperListener() {
            @Override
            public void onSignInSucceeded() {
            }

            @Override
            public void onSignInFailed() {
            }
        };

        _gameHelper.setup(gameHelperListener);

        setContentView(layout);
        startAdvertising(admobView);

        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {

            }

            @Override
            public void onAdClosed() {
            }
        });
        showOrLoadInterstital();

    }

    private AdView createAdView() {
        adView = new AdView(this);
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId(AD_UNIT_ID_BANNER);
        adView.setId(12345); // this is an arbitrary id, allows for relative
                                // positioning in createGameView()
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        if (Configuration.AD_POSITION.equals("UP")) {
            params.gravity = Gravity.TOP;
        } else {
            params.gravity = Gravity.BOTTOM;
        }

        adView.setLayoutParams(params);
        adView.setBackgroundColor(Color.TRANSPARENT);
        return adView;
    }

    private View createGameView(AndroidApplicationConfiguration cfg) {
        gameView = initializeForView(new SwingCopters(this), cfg);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        gameView.setLayoutParams(params);
        return gameView;
    }

    private void startAdvertising(AdView adView) {
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (adView != null)
            adView.resume();
    }

    @Override
    public void onPause() {
        if (adView != null)
            adView.pause();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        if (adView != null)
            adView.destroy();
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
        _gameHelper.onStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        _gameHelper.onStop();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        _gameHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void showOrLoadInterstital() {
        try {
            runOnUiThread(new Runnable() {
                public void run() {
                    if (interstitialAd.isLoaded()) {
                        interstitialAd.show();

                    } else {
                        AdRequest interstitialRequest = new AdRequest.Builder()
                                .build();
                        interstitialAd.loadAd(interstitialRequest);

                    }
                }
            });
        } catch (Exception e) {
        }
    }

    @Override
    public void signIn() {
        try {
            runOnUiThread(new Runnable() {
                // @Override
                public void run() {
                    _gameHelper.beginUserInitiatedSignIn();
                }
            });
        } catch (Exception e) {
            Gdx.app.log("MainActivity", "Log in failed: " + e.getMessage()
                    + ".");
        }
    }

    @Override
    public void signOut() {
        try {
            runOnUiThread(new Runnable() {
                // @Override
                public void run() {
                    _gameHelper.signOut();
                }
            });
        } catch (Exception e) {
            Gdx.app.log("MainActivity", "Log out failed: " + e.getMessage()
                    + ".");
        }
    }

    @Override
    public void rateGame() {
        // Replace the end of the URL with the package of your game

        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(GOOGLE_PLAY_URL)));
    }

    @Override
    public void submitScore(long score) {
        if (isSignedIn() == true) {
            Games.Leaderboards.submitScore(_gameHelper.getApiClient(),
                    **getString(R.string.leaderboard_id), score);**
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(
                    _gameHelper.getApiClient(),
                    **getString(R.string.leaderboard_id)), REQUEST_CODE_UNUSED);**
        } else {
            // Maybe sign in here then redirect to submitting score?
        }
    }

    @Override
    public void showScores() {
        if (isSignedIn() == true)
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(
                    _gameHelper.getApiClient(),
                    **getString(R.string.leaderboard_id)), REQUEST_CODE_UNUSED);**
        else {
            signIn();
        }
    }

    @Override
    public boolean isSignedIn() {
        return _gameHelper.isSignedIn();
    }

    @Override
    public void onSignInFailed() {
    }

    @Override
    public void onSignInSucceeded() {
    }

}

共 (2) 个答案

  1. # 1 楼答案

    您应该导入自己的项目R类,而不是androids默认的R类ie

    你的。应用程序。包裹R。 这将使您的类中的res文件夹下定义的值。 当你需要在Android R中查找股票字符串或其他东西时,请使用其完全限定名,例如Android。R.string。是的,否则您的项目字符串将生效(您可能希望使用)

    之后刷新,然后清理你的项目

  2. # 2 楼答案

    发生这种情况的原因有很多-

    1.项目建设不到位
    Try cleaning your project
    Check if there any problem in your XML file

    2.您尚未导入R文件
    import com.youpackage.name.R

    3.字符串中可能没有定义leaderboard_id。xml