我正在使用C#为Unity编写代码.目前,我正在处理一些较小的类和结构,以便快速序列化随机生成的地图.在这样做时,我处理了一些构造函数,这些构造函数也将一些较小的类和结构作为参数.
在过去,我通常会在设置方法和构造函数时尝试考虑合理的选项.虽然这种做法受到了质疑,但没有人能够给我任何合理的理由,说明为什么我不应该这样做.
考虑以下类和结构:
public class Tile
{
public GridCoordinates position;
public TileCode prefabID;
}
public struct GridCoordinates
{
public int x;
public int y;
}
public struct TileCode
{
public int categoryID;
public int individuaID;
}
Run Code Online (Sandbox Code Playgroud)
通常,在创建构造函数时,我会涵盖所有struct和int替代方案.Tile看起来像这样:
public class Tile
{
public GridCoordinates position;
public TileCode prefabID;
public Tile(TileCode prefabID, GridCoordinates position)
{
this.prefabID = new TileCode(prefabID);
this.position = new GridCoordinates(position);
}
public Tile(TileCode prefabID, int xPosition, int …Run Code Online (Sandbox Code Playgroud) 我一直试图在过去两天内设置一个RecyclerView,其中大部分似乎按预期工作.但是,每当我尝试运行我的应用程序时,它都会发生致命的异常崩溃:
E/AndroidRuntime:FATAL EXCEPTION:main进程:com.mad.exercise5,PID:16634 android.content.res.Resources $ NotFoundException:android.content.res.Resources.getText中的字符串资源ID#0x1(Resources.java:342 )在android.widget.TextView.setText(TextView.java:4244)的android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)at (...)
我一直在关注YouTube的幻灯片教程,第1-4部分.在教程的上下文中,RecyclerView正在一个片段中设置,我还没有进入.为了防止错误来自在主活动中创建RecyclerView,我也尝试在StackTips上学习教程.
尽我所能,我已经能够通过注释掉设置布局管理器(mRecyclerView.setLayoutManager(new LinearLayoutManager(this));)的行来停止错误,但错误似乎来自于onBindViewHolder.具体来说,将文本设置为我的ViewHolder TextViews.
谷歌搜索错误,并进行一些研究,我发现很多情况下,其他问题和问题突然出现此错误.但是,问题似乎总是用户正在调用资源,而是直接将其作为id引用.也就是说,他们会使用' String string = R.string.string'代替' String string = getString(R.string.string)'.我已经三次检查我的标准资源实现,并且我相信我没有错过任何东西.至于教程让我相信,我正确地实例化我的TextViews,正确的实现findViewById.主要活动上下文在初始化时传递,因此找到相关的ID应该没有问题.
为什么我在RecyclerView示例活动中收到了Resources $ NotFoundException?两个相关的java类如下:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ArrayList<Train> mTrains;
private TrainAdapter mTrainAdapter;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the Recycler View
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mTrains = FillTrainData();
mTrainAdapter = new …Run Code Online (Sandbox Code Playgroud) java android compiler-errors recycler-adapter android-recyclerview
我正从数据库中检索一些记录,我追加到一些字符串 - 例如:
spouse counter=1;
counter=1+counter;
Run Code Online (Sandbox Code Playgroud)
然后我得到customerID以下方式:
customerID = "AGP-00000" + counter;
Run Code Online (Sandbox Code Playgroud)
这样customerID就可以了AGP-000001.
对于计数器值2- customerID字符串将是AGP-000002.
我的问题是,如果计数器10那么customer ID会 AGP-0000010,我希望它是AGP-000010.
我怎样才能确保customer ID总是如此AGP-000010?