从java

时间:2015-07-09 22:31:21

标签: java android xml android-studio libgdx

我见过几个人们从java中访问.xml文件的例子。

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">YYYYYYYYY</string>
    <string name="app_id">999999999999</string>
    <string name="leaderboard_id">XXXXXXXXXXX</string>

</resources>

我已经看到android.R包的使用声称在LibGDX设置中获取字符串是成功的。但是,我一直遇到error: cannot find symbol variable leaderboard_id,leaderbord_id是我目前唯一尝试的变量。

我已经尝试清理我的项目而没有错误但是一旦再次构建我每次尝试访问它时都会收到上述错误。

我的strings.xml位于../android/res/values/,我试图像getString(R.string.leaderboard_id)那样访问它而没有成功。此外,我尝试了Sync project with gradle以及不同的API和构建。

我使用的官方Google BaseGameUtils提供了bootstrap warning about classpath not being set,但我没有看到任何问题。我开始怀疑这是可能的还是改变的,因为我发现的帖子很旧。

这是我的完整代码:

AndroidLauncher.java

package net.madmenyo.multiplayertest.android;

import android.content.Intent;
import android.os.Bundle;
import android.R;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.games.Games;
import com.google.example.games.basegameutils.GameHelper;

import net.madmenyo.multiplayertest.IGoogleServices;
import net.madmenyo.multiplayertest.MultiplayerTest;


public class AndroidLauncher extends AndroidApplication implements IGoogleServices {
    private GameHelper _gameHelper;
    private final static int REQUEST_CODE_UNUSED = 9002;

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


        // Create game helper
        _gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
        _gameHelper.enableDebugLog(false);

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MultiplayerTest(this), config);
    }

    @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 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 submitScore(long score) {
        if (isSignedIn() == true)
        {
            Games.Leaderboards.submitScore(_gameHelper.getApiClient(), getString(R.string.leaderboard_id), score); //Error cannot find symbol variable leaderboard_id
getApplicationContext().getResources().getString(R.string.leaderboard_id); //Error
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(_gameHelper.getApiClient(), getString(R.string.leaderboard_id)), REQUEST_CODE_UNUSED); //Error
        }
        else
        {
        }
    }

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

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

2 个答案:

答案 0 :(得分:3)

您似乎导入了错误的R课程。

import android.R;

您需要R文件指向您自己的资源,而不是Android SDK中定义的资源。您需要的是(假设您的应用程序包名为net.madmenyo.multiplayertest.android):

import net.madmenyo.multiplayertest.android.R;

答案 1 :(得分:0)

如果你在Activity,你可以: getApplicationContext().getResources().getString(R.string.leaderboeard_id)

Fragment中,执行: getActivity().getResources().getString(R.string.leaderboard_id)