You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发求助:如何从resource directory声明string array?

Hey there! Let's break down why your string array is causing crashes and how to fix it properly.

First, Let's Cover the Correct Way to Get a String Array from Resources

In Android, you must use a valid Context to access your app's resources. The standard, crash-safe call looks like this:

String[] x = yourValidContext.getResources().getStringArray(R.array.your_array_name);

Before that, make sure you've properly defined your array in res/values/arrays.xml (if you haven't already):

<!-- res/values/arrays.xml -->
<string-array name="your_array_name">
    <item>First Item</item>
    <item>Second Item</item>
    <item>Third Item</item>
</string-array>

Why Your First Declaration Crashed

The most common culprits here are:

  • Invalid/Null Context: If you used getApplicationContext() (which isn't tied to your activity's lifecycle) or a context that was already destroyed to fetch the array, you'll hit a NullPointerException. Custom adapters should always use the activity context passed into their constructor.
  • Wrong Resource Reference: If you misspelled the array name in R.array.your_array_name, Android will throw a Resources.NotFoundException because it can't find the resource you're asking for.
  • Early Initialization: If you tried to fetch the array before the context was fully initialized (like in a field initializer instead of the adapter's constructor), you'll run into issues too.

Example of a Fixed Custom Adapter

Here's how to structure your adapter to safely get the string array:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
    private String[] mStringArray;
    private Context mContext;

    // Pass a valid activity context into the constructor
    public CustomAdapter(Context context) {
        mContext = context;
        // Now safely fetch the array using the valid context
        mStringArray = mContext.getResources().getStringArray(R.array.your_array_name);
    }

    // Rest of your adapter code...
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.your_item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.itemTextView.setText(mStringArray[position]);
    }

    @Override
    public int getItemCount() {
        return mStringArray.length;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView itemTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            itemTextView = itemView.findViewById(R.id.item_text);
        }
    }
}

Using Your Logcat to Narrow It Down

Check your Logcat for specific errors:

  • If you see NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()', your context is null. Double-check how you're passing the context to your adapter.
  • If you see Resources.NotFoundException: String array resource ID #0x..., you either misspelled the array name in your code or forgot to define it in arrays.xml.

内容的提问来源于stack exchange,提问作者abc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:37:41