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

时间:2014-09-06 22:13:31

标签: java android google-play-services

行中的错误:236 239和250 我不是用字符串R来解决这个问题 R.string函数我没找到它。我想知道一些提示以及如何解决问题。我已经尝试过使用强制播放服务的字符串函数而不是太多了

    package com.xxx.xxxx;

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

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.gikdew.swingcopters.ActionResolver;
import com.gikdew.swingcopters.SwingCopters;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.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() {
    }

}

3 个答案:

答案 0 :(得分:0)

确保在目录res/values/strings.xml中是否存在名为" leaderboard_id"的字符串。

之类的东西
<resources>
<string name="leaderboard_id">WriteYourId</string>
</resources>

现在似乎没有任何名为&#34; leaderboard_id&#34;在导致错误的strings.xml中。

答案 1 :(得分:0)

发生这种情况的原因有很多 -
 
1.项目建设不当。
Try cleaning your project
Check if there any problem in your XML file

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

3.您可能没有在String.xml中定义leaderboard_id

答案 2 :(得分:0)

你应该导入你自己的项目R class而不是orroids默认的R class ie。

your.app.package.R 。  这将使您的类中的res文件夹下定义的值。 当您需要引用Android R获取股票字符串或其他内容时,请使用其完全限定名称,例如android.R.string.yes,否则您的项目字符串将生效(您可能希望使用它) )

在刷新之后再清理你的项目。